我正在尝试使用GitHub为项目设置Jenkins-ci。我已经给Jenkins安装了合适的插件。我希望Jenkins只在项目中有人推动master时运行构建脚本。到目前为止,我已经能够设置一个构建将触发任何时候任何人推到任何地方,但这太广泛了。我已经用Git上的post-receive service钩子做到了这一点。
我读过Jenkins的维基和一些教程,但这个细节缺失了……也许这和民意调查有关?或者应该在Git端进行工作,以便Git只在master更改时触发Jenkins ?
我正在尝试使用GitHub为项目设置Jenkins-ci。我已经给Jenkins安装了合适的插件。我希望Jenkins只在项目中有人推动master时运行构建脚本。到目前为止,我已经能够设置一个构建将触发任何时候任何人推到任何地方,但这太广泛了。我已经用Git上的post-receive service钩子做到了这一点。
我读过Jenkins的维基和一些教程,但这个细节缺失了……也许这和民意调查有关?或者应该在Git端进行工作,以便Git只在master更改时触发Jenkins ?
当前回答
与Jenkins的持续集成,在代码从Git命令/ GUI推送到存储库后:
Create a job in Jenkins with only job name and select type of the project freestyle. Click OK. The next page doesn't add anything - just click Save. Go to the your local Git repository where you have the source code and navigate to the .git/hooks folder. The hooks folder contains the few files. Check for the "post-commit". If not present, create a file, "post-commit" without a file extension: C:\work\test\\.git\hooks\post-commit Edit the "post-commit" file with the below command. Make sure it is present in your local source code hooks folder. curl -u userName:apiToken -X POST http://localhost:8080/jenkins/job/jobName/build?token=apiToken Example: curl -u admin:f1c55b3a07bb2b69b9dd549e96898384 -X POST http://localhost:8080/jenkins/job/Gitcommittest/build?token=f1c55b3a07bb2b69b9dd549e96898384 5. userName: Jenkins user name jobName: Job name of the build apiToken: To get your API token, go to your Jenkins user page (top right in the interface). It is available in the "Configure" menu on the left of the page: "Show API token" Make changes in your source code and commit the code to repository. Your job, http://localhost:8080/jenkins/job/Gitcommittest/, should be building.
其他回答
从0.5版开始,针对Jenkins的GitHub插件可以在将更改推送到GitHub时触发构建。
我希望这篇文章对您有所帮助:如何在Git提交上触发Jenkins构建
这只是使用curl使用Git提供的Git钩子触发Jenkins作业的问题。
命令curl http://localhost:8080/job/someJob/build?delay=0sec可以运行Jenkins作业,其中someJob是Jenkins作业的名称。
在隐藏的。git文件夹中搜索“hooks”文件夹。重命名“post-commit”。样本文件,以“后提交”。用记事本打开它,删除“:Nothing”行,并将上面的命令粘贴进去。
就是这样。无论何时执行提交,Git都会触发文件中定义的提交后命令。
对于GitLab,使用以下步骤:
转到项目的设置→Web钩子 从Jenkins项目中输入“Build Now”URL作为推送事件URL: 例如http://server.com/jenkins/job/project_name/build?delay=0sec 单击“添加Web钩子”,然后测试钩子
然后,每当你提交到存储库时,web钩子就会被触发,并创建一个构建。确保在每次构建之前将Jenkins工作区设置为删除工作区,以便获得新代码的新副本。
与其远程触发构建,不如将Jenkins项目配置更改为通过轮询触发构建。
Jenkins可以基于固定的内部或URL进行投票。如果该分支没有更改,则您希望跳过后者。具体细节如下 在文档中。实际上,您只需要选中“Poll SCM”选项,将日程部分留空,并设置远程URL为JENKINS_URL/job/name/polling。
如果您有一个安全的Jenkins环境,那么与/build不同,/polling URL需要身份验证。这里有详细说明。例如,我有一个GitHub Post-Receive钩子到用户名:apiToken@JENKIS_URL/job/name/polling。
与Jenkins的持续集成,在代码从Git命令/ GUI推送到存储库后:
Create a job in Jenkins with only job name and select type of the project freestyle. Click OK. The next page doesn't add anything - just click Save. Go to the your local Git repository where you have the source code and navigate to the .git/hooks folder. The hooks folder contains the few files. Check for the "post-commit". If not present, create a file, "post-commit" without a file extension: C:\work\test\\.git\hooks\post-commit Edit the "post-commit" file with the below command. Make sure it is present in your local source code hooks folder. curl -u userName:apiToken -X POST http://localhost:8080/jenkins/job/jobName/build?token=apiToken Example: curl -u admin:f1c55b3a07bb2b69b9dd549e96898384 -X POST http://localhost:8080/jenkins/job/Gitcommittest/build?token=f1c55b3a07bb2b69b9dd549e96898384 5. userName: Jenkins user name jobName: Job name of the build apiToken: To get your API token, go to your Jenkins user page (top right in the interface). It is available in the "Configure" menu on the left of the page: "Show API token" Make changes in your source code and commit the code to repository. Your job, http://localhost:8080/jenkins/job/Gitcommittest/, should be building.