Java 是否可以仅从一个特定分支触发 Jenkins?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20713157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to trigger Jenkins from one specific branch only?
提问by Leonardo Cardoso
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.
我的情况如下:我在一个 repo 中有三个分支:master、dev 和 staging。对于这些分支中的每一个,我都有一份工作,在 Jenkins 的“要构建的分支”部分中进行配置。原点/主、原点/开发、原点/暂存。
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).
只要通过存储库挂钩对存储库进行了更改,Bitbucket 就会触发作业来构建。(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.
但是,当我推送到 master 时,所有作业都开始构建,其他两个作业也是如此。
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.
我希望只有当我推送到 master 分支时才构建 Jenkins “master” 作业。詹金斯“开发”工作到开发分支。詹金斯“分期”工作到开发分期。
Is there a way to control this behaviour?
有没有办法控制这种行为?
采纳答案by Leonardo Cardoso
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.
我刚刚发现 Bitbucket 不允许在推送到分支时选择特定的钩子。它只是调用所有钩子,然后启动 Jenkins 的所有工作。
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)
我的解决方案是在我的机器上创建一个特定文件,在该文件上安装了 Jenkins 并为该文件设置了一个 Bitbucket 挂钩。(例如 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.
请注意,这个 apache 端口与 Jenkins 不同,而是 Apache 的。就我而言,Jenkins 在 8080 上运行,Apache 在 7777 上运行。它这样做是为了运行 php 脚本,但不在 Jenkins 的目录中。
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
由于 Bitbucket hook 发送了一个 json 文件,我能够在 check.php 中验证哪个分支已被推送。参考:POST钩子管理
After the verification using a simple 'if', I just called the right url to start the right job with exec_curl, like:
使用简单的“if”进行验证后,我只是调用了正确的 url 以使用 exec_curl 开始正确的工作,例如:
$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à.
瞧。
回答by Bert F
Did you set up polling?
你设置投票了吗?
https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-Pushnotificationfromrepository
https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-Pushnotificationfromrepository
... This will scan all the jobs that's configured to check out the specified URL, the optional branches, and if they are also configured with polling, it'll immediately trigger the polling (and if that finds a change worth a build, a build will be triggered in turn.) We require the polling configuration on the job so that we only trigger jobs that are supposed to be kicked from changes in the source tree.
...这将扫描所有配置为检查指定 URL、可选分支的作业,如果它们也配置了 polling,它将立即触发轮询(如果发现值得构建的更改, build 将依次触发。)我们需要对作业进行轮询配置,以便我们只触发应该从源树中的更改中踢出的作业。
回答by salcosand
To have different Jenkis projects for different branches I did the following:
为了让不同的分支有不同的 Jenkis 项目,我做了以下事情:
- Install Bitbucket Pluginat your Jenkins
- Add a normal Post as Hook to your Bitbucket repository (Settings -> Hooks) and use following url:
- 在您的 Jenkins 上安装Bitbucket 插件
- 将一个普通的 Post 作为 Hook 添加到您的 Bitbucket 存储库(Settings -> Hooks)并使用以下 URL:
https://YOUR.JENKINS.SERVER:PORT/bitbucket-hook
https: //YOUR.JENKINS.SERVER:PORT/bitbucket-hook
- Configure your Jenkins project as follows:
- under build triggerenable Build when a change is pushed to BitBucket
- under Source Code Managementselect GIT; enter your credentials and define Branches to build(like **feature/*) <- this is where you define different branches for each project
- 按如下方式配置您的 Jenkins 项目:
- 在构建触发器下,在将更改推送到 BitBucket 时启用构建
- 在源代码管理下选择GIT;输入您的凭据并定义要构建的分支(如**feature/*)<-这是您为每个项目定义不同分支的地方
By this way I have three build projects, one for all features, one for develop and one for release branch.
通过这种方式,我有三个构建项目,一个用于所有功能,一个用于开发,一个用于发布分支。
And best of it, you don't have to ad new hooks for new Jenkins projects.
最棒的是,您不必为新的 Jenkins 项目添加新的钩子。
回答by GangaRam Dewasi
Yes!! You can trigger the jenkins build whenever there is a commit or merge into a specific branch of the git repo.
是的!!只要有提交或合并到 git 存储库的特定分支,您就可以触发 jenkins 构建。
STEPS:
脚步:
Configure the webhook for your jenkins instance in the Webhook section of the github Repository, the Payload URl will look similar to,
http://jenkinsinstance:8080/github-webhook/
In the Jenkins Job configuration just enable,
GitHub hook trigger for GITScm polling
Then in the SCM section add the below configuration available in the image, assuming the branch you want to build being the Hotfix branch. The Below image defines the exact configuration of SCM section.
在 github 存储库的 Webhook 部分为您的 jenkins 实例配置 webhook,Payload URl 将类似于,
http://jenkinsinstance:8080/github-webhook/
在 Jenkins 作业配置中只需启用,
GitHub hook trigger for GITScm polling
然后在 SCM 部分添加图像中可用的以下配置,假设您要构建的分支是 Hotfix 分支。下图定义了 SCM 部分的确切配置。
- Once done, try to commit code changes to the Hotfix branch, which in return should trigger the jenkins job.
- 完成后,尝试将代码更改提交到 Hotfix 分支,这将触发 jenkins 作业。