[英]Trigger Jenkins job on GitHub PUSH to specific branch
我试图触发基于GitHub PUSH到某个分支的工作。
我将JENKINS_URL/git/notifyCommit?url=REPO_URL
设置为JENKINS_URL/git/notifyCommit?url=REPO_URL
和没有计划的Poll SCM
。
每次push
都会触发该作业,但我无法过滤出分支-我希望仅在有主控推送时才发生。
为此,您必须使用git hook。
git挂钩获取分支名称作为参数。
Hook code
#!/bin/sh
while read oldrev newrev refname
do
branch=`echo $refname | cut -d'/' -f3`
###
# Do whatever you want to do here with the branch name
###
done
您可以参考以下答案: 在bitbucket的特定分支上的jenkins中触发构建
我刚刚发现Bitbucket不允许选择推送到任何分支的特定挂钩。 它只是调用所有钩子,然后启动所有Jenkins作业。
My solution was to create an specific file on my machine which Jenkins is installed and set a Bitbucket hook to this file. (eg
http:// {jenkins网址}:{apache端口} /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.
它这样做是为了运行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
管理
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à.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.