我正在尝试使用GitHub为项目设置Jenkins-ci。我已经给Jenkins安装了合适的插件。我希望Jenkins只在项目中有人推动master时运行构建脚本。到目前为止,我已经能够设置一个构建将触发任何时候任何人推到任何地方,但这太广泛了。我已经用Git上的post-receive service钩子做到了这一点。

我读过Jenkins的维基和一些教程,但这个细节缺失了……也许这和民意调查有关?或者应该在Git端进行工作,以便Git只在master更改时触发Jenkins ?


当前回答

一般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插件。

在我目前的组织中,我们不在master中这样做,但在开发和发布/分支中都这样做(我们使用Git Flow),以生成快照构建。

当我们使用多分支管道时,我们在Jenkinsfile中使用when{}语法…

stage {
    when { 
        expression { 
            branch 'develop'
        }
    }
}

这篇博客文章详细介绍了这一点: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/#longer-pipeline

与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.

使用pull request builder插件: https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin

这真的很简单。然后你可以设置GitHub webhook来触发构建。

我对本地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”的文件。

这就是你所需要的