我的情况如下:我在一个 repo 中有三个分支:master、dev 和 staging.我为这些分支中的每一个都有一份工作,在 Jenkins 的要构建的分支"部分中配置.源/主、源/开发、源/暂存.
My situation is the following: I have three branches in a repo: master, dev and staging. And I have one job for each one of these branches, configured in 'Branches to build' section in Jenkins. origin/master, origin/dev, origin/staging.
只要通过存储库挂钩对存储库进行更改,Bitbucket 就会触发构建作业.(https://confluence.atlassian.com/display/BITBUCKET/Jenkins+hook+management).
Bitbucket triggers the job to build whenever there are changes to the repository via a repository hook .(https://confluence.atlassian.com/display/BITBUCKET/Jenkins+hook+management).
但是,当我推送到 master 时,所有作业都开始构建,其他两个作业也是如此.
However, when I push to master, all jobs starts to build, and the same with the other two ones.
我希望只有在我推送到主分支时才能构建 Jenkins主"作业.詹金斯开发"工作到开发分支.詹金斯登台"工作到开发登台.
I want Jenkins "master" job to be built only when I push to master branch. Jenkins "dev" job to dev branch. Jenkins "staging" job to dev staging.
有没有办法控制这种行为?
Is there a way to control this behaviour?
我刚刚发现Bitbucket在推送到分支时不允许选择特定的钩子.它只是调用所有的钩子,然后启动所有 Jenkins 的工作.
I just discovered that Bitbucket does not allow to choose a specific hook when pushing to branches. It just calls all the hooks, then it starts all Jenkins' jobs.
我的解决方案是在我的机器上创建一个特定的文件,在该文件上安装了 Jenkins,并为此文件设置了一个 Bitbucket 挂钩.(例如 http://{jenkins url}:{apache 端口}/check.php)
My solution was to create a specific file on my machine, on which Jenkins is installed and set a Bitbucket hook to this file. (e.g. http://{jenkins url}:{apache port}/check.php)
请注意,这个 apache 端口不是 Jenkins 的,而是 Apache 的.在我的例子中,Jenkins 在 8080 和 Apache 在 7777 运行.它这样做是为了运行 php 脚本,但不在 Jenkins 的目录中.
Note that this apache port is not the same of Jenkins', but Apache's. In my case, Jenkins was running at 8080 and Apache at 7777. It did this to run php script, but not in Jenkins' directory.
由于 Bitbucket 钩子发送一个 json 文件,我能够在 check.php 中验证哪个分支已被推送.参考:POST 挂钩管理
Since Bitbucket hook sends a json file, I was able to verify in check.php which branch has been pushed on. Reference: POST hook management
使用简单的if"验证后,我只是调用了正确的 url 以使用 exec_curl 开始正确的工作,例如:
After the verification using a simple 'if', I just called the right url to start the right job with exec_curl, like:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, http://{jenkins url}:{jenkins port}/job/{job name}/build?token={job token});
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
然后瞧.
这篇关于是否可以仅从一个特定分支触发 Jenkins?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!