我正在尝试使用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项目配置更改为通过轮询触发构建。
Jenkins可以基于固定的内部或URL进行投票。如果该分支没有更改,则您希望跳过后者。具体细节如下 在文档中。实际上,您只需要选中“Poll SCM”选项,将日程部分留空,并设置远程URL为JENKINS_URL/job/name/polling。
如果您有一个安全的Jenkins环境,那么与/build不同,/polling URL需要身份验证。这里有详细说明。例如,我有一个GitHub Post-Receive钩子到用户名:apiToken@JENKIS_URL/job/name/polling。
与Git无关,但下面我将帮助使用Mercurial详细配置Jenkins作业。它可能会帮助其他有类似问题的人。
安装URL触发器插件 进入作业配置页面,选择Poll SCM选项。设置为* * * * * 检查选项:[URLTrigger] -使用URL轮询。 现在您可以选择一些选项,如修改日期更改,URL内容等。 在选项中,选择“URL内容更改”,选择第一个选项“监控内容更改” 保存更改。
现在,通过一些测试签入触发对Mercurial存储库的一些更改。
查看Jenkins作业现在通过检测SCM更改来运行。当构建由于Mercurial的更改而运行时,您将看到文本Started by an SCM发生更改。Else,手动启动它的用户。
正如gezzed在他的评论中已经提到的,同时有一个很好的解决方案(在Polling must die中描述:从Git钩子触发Jenkins构建):
将Jenkins作业的构建触发器设置为Poll SCM,但不要指定时间表。 创建一个GitHub接收后触发器来通知URL http://yourserver/jenkins/git/notifyCommit?url=< Git存储库的URL >?Token =<从git获取Token来远程构建> 这将触发轮询指定Git存储库的所有构建。 但是,轮询实际上检查是否有任何内容已推送到已使用的分支。
它工作得很完美。
我希望这篇文章对您有所帮助:如何在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工作区设置为删除工作区,以便获得新代码的新副本。
使用pull request builder插件: https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin
这真的很简单。然后你可以设置GitHub webhook来触发构建。
与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.
一般Webhook触发器插件可以配置过滤器来实现这一点。
当配置为
名为ref的变量和表达式$.ref。 一个带有文本$ref的过滤器和像^refs/heads/master$这样的过滤器表达式。
那这项工作就会触发每一次突破。没有投票。
你可能需要webhook提供更多的值来实际执行构建。只需使用JSONPath添加更多变量,以选择所需的变量。
这里有一些用例:https://github.com/jenkinsci/generic-webhook-trigger-plugin/tree/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd
在我目前的组织中,我们不在master中这样做,但在开发和发布/分支中都这样做(我们使用Git Flow),以生成快照构建。
当我们使用多分支管道时,我们在Jenkinsfile中使用when{}语法…
stage {
when {
expression {
branch 'develop'
}
}
}
这篇博客文章详细介绍了这一点: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/#longer-pipeline
以上答案是正确的,但我是针对他们谁是新手这里的简单
特别是为管道设置构建触发器:
假设你有两个Github分支:主人,2.开发, 和Jenkinsfile(编写管道脚本的地方)以及其他文件在每个分支上都可用
配置新的Pipeline项目(用于开发分支)
# # 1。基于git-plugin和cron的代码集成方法 前提条件git插件应该安装和配置它与您的姓名和电子邮件
General section.Check checkbox - 'This project is parameterized’ and add Name-SBRANCH Default Value-'refs/remotes/origin/dev' Build triggers section" Check checkbox - 'Poll SCM' and schedule as per need for checking commits e.g. '*/1 * * * *' to check every minute Pipeline definition section.Select - Pipeline script from SCM—> select git—> addRepository URL—>add git credentials—> choose advanced—> add Name- origin, RefSpec- '+refs/heads/dev:refs/remotes/origin/dev'(dev is github branch )—> Branches to build - ${SBRANCH} (Parameter name from ref 1st point)—> Script Path—> Jenkinsfile —> Uncheck Lightweightcheckout Apply—> save
# # 2。代码集成:github-plugin和webhook方法 前提条件安装了Github插件,配置了Github服务器,如果不考虑下面的配置,需要测试连接
配置Github插件与帐户詹金斯
GitHub section Add Github server if not present API URL: https://api.github.com Credentials: Add secret text (Click add button: select type secret text) with value Personal Access Token (Generate it from your Github accounts—> settings—> developer setting—> personal access token—> add token—> check scopes—> copy the token) Test Connection—> Check whether it is connected to your Github account or not Check checkbox with Manage Hooks In advance sub-section just select previous credential for 'shared secret'
添加webhook,如果没有添加到您的存储库
去Github存储库设置- >添加webhook - >添加URL http://Public_IP:Jenkins_PORT/github-webhook/ 如果没有Public_IP,请使用ngrok。安装,认证,从命令./ngrok获取公共IP http 80(使用你的jenkins_port),然后添加webhook - >添加URL http://Ngrok_IP/github-webhook/ 通过从webhook页面传递有效载荷来测试它,并检查是否获得200状态。
如果你有Github拉请求插件配置它也与发布的Jenkins URL。
General section.Check checkbox - 'Github project' add project URL -(github link ending with '.git/') General section.Check checkbox - 'This project is parameterized' and add Name-SBRANCH Default Value-'refs/remotes/origin/dev' Build triggers.section.Check checkbox - 'GitHub hook trigger for GITScm polling' Pipeline def'n section: Select - Pipeline script from SCM—> select git—> addRepository URL—> add git credentials—>choose advanced —>add Name- origin, RefSpec- '+refs/heads/dev:refs/remotes/origin/dev' (dev is github branch ) —> Branches to build - ${SBRANCH} (Parameter name from ref 1.st point)—> Script Path—> Jenkinsfile—> Uncheck Lightweightcheckout Apply—> save
我对本地git服务器的解决方案: 转到本地git服务器钩子目录,忽略现有的更新。示例并创建一个名为“update”的新文件,例如:
gituser@me:~/project.git/hooks$ pwd
/home/gituser/project.git/hooks
gituser@me:~/project.git/hooks$ cat update
#!/bin/sh
echo "XXX from update file"
curl -u admin:11f778f9f2c4d1e237d60f479974e3dae9 -X POST http://localhost:8080/job/job4_pullsrc_buildcontainer/build?token=11f778f9f2c4d1e237d60f479974e3dae9
exit 0
gituser@me:~/project.git/hooks$
echo语句将显示在你的git推送结果下,令牌可以从你的jenkins作业配置中获取,浏览找到它。如果没有调用文件“update”,请尝试其他具有相同名称但没有扩展名为“sample”的文件。
这就是你所需要的
管理Jenkins/配置系统/GitHub服务器 在jenkins job / git凭证和分支说明符上(给你想要查找的分支推送)
在github上的Webhook
我也有同样的问题。这救了我
要根据有效负载'ref'标签进行过滤,jenkins pipeline job中设置了可选过滤器。
然后对'ref'变量应用可选过滤器,只允许主分支
或者表达可以很简单
参考/头/大师
现在我的詹金斯工作是触发只有当有一个提交到主