我正在尝试使用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 ?
当前回答
从0.5版开始,针对Jenkins的GitHub插件可以在将更改推送到GitHub时触发构建。
其他回答
管理Jenkins/配置系统/GitHub服务器 在jenkins job / git凭证和分支说明符上(给你想要查找的分支推送)
在github上的Webhook
以上答案是正确的,但我是针对他们谁是新手这里的简单
特别是为管道设置构建触发器:
假设你有两个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
一般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
您需要指定分支。默认情况下,它会监听任何内容。请参阅博客文章Hudson: Git和Maven插件。
从0.5版开始,针对Jenkins的GitHub插件可以在将更改推送到GitHub时触发构建。