是否有可能只从一个特定分支触发Jenkins? [英] Is it possible to trigger Jenkins from one specific branch only?

查看:1486
本文介绍了是否有可能只从一个特定分支触发Jenkins?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况如下:
我在repo有三个分支:master,dev和staging。
我为这些分支中的每一个都有一个工作,在Jenkins的Branches to build部分中配置。



Bitbucket会在存储库通过存储库挂钩进行更改时触发该作业。(https://confluence.atlassian.com/display/BITBUCKET/Jenkins+hook+management )。



但是,当我推送给主人时,所有的工作开始建立,并与其他两个一样。



我想要Jenkins主的工作,只有当我推到主分支。 Jenkinsdev工作到dev分支。 Jenkins分段工作到开发阶段。



是否有办法控制此行为?

解决方案

刚刚发现Bitbucket不允许选择特定的钩子推到任何分支。它只是调用所有的钩子,然后启动所有的Jenkins工作。



我的解决方案是在我的机器上创建一个特定的文件,Jenkins安装并设置一个Bitbucket钩子文件。 (例如http:// {jenkins url}:{apache port} /check.php)



请注意,这个apache端口与Jenkins不一样, 。在我的情况下,Jenkins运行在8080和Apache在7777.它这样做运行php脚本,但不是在Jenkins的目录。



由于Bitbucket钩子发送一个json文件,我能够验证在check.php哪个分支已推送。
参考: POST挂钩管理

在使用一个简单的'if'验证后,我只是调用正确的url用exec_curl启动正确的工作,例如:



$ ch = curl_init();

curl_setopt($ ch,CURLOPT_URL,http:// {jenkins url}:{jenkins port} / job / {job name} = {job token});

curl_setopt($ ch,CURLOPT_HEADER,0);

curl_exec($ ch);

curl_close ch);



和voilà。


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

However, when I push to master, all jobs starts to build, and the same with the other two ones.

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?

解决方案

I just discovered that Bitbucket does not allow to choose specific hook on pushing to any branch. It just calls all the hooks, then starts all Jenkins jobs.

My solution was to create an specific file on my machine which Jenkins is installed and set a Bitbucket hook to this file. (e.g. http://{jenkins url}:{apache port}/check.php)

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.

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

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);

And voilà.

这篇关于是否有可能只从一个特定分支触发Jenkins?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆