如何使用Jenkinsfile合并拉取请求的成功构建 [英] How to merge a successful build of a pull request using a Jenkinsfile
问题描述
在GitHub中,我有一个Web钩子,用于监听 pull request
和 push $ c $事件。
编辑:我尝试了以下操作,但目前失败,出现语法错误:
def gitPullRequestNumber = null
...
...
stage(合并请求){
steps {
gitPullRequestNumber = shgit ls-remote origin'pull / * / head:develop'
shcurl -X PUT -d'{\'commit_title \':\'Merge pull request \\''''<<< GIT REPO URL>> / pulls / $ {gitPullRequestNumber} / merge?access_token =<< ACCESS-TOKEN>
}
}
错误:
org.codehaus.groovy.control .MultipleCompilationErrorsException:启动失败:
WorkflowScript:58:预计将有一个步骤@第58行,第13列。
gitPullRequestNumber = shgit ls-remote originpull / * / head:develop'
^
1错误
背景:
我们有一个 master
分支和一个 develop
分支。开发人员将针对要运行测试的开发
分支执行pull请求。如果测试通过,则拉取请求应合并到分支中。代码审查是在拉开请求打开之前完成的。决定后,将 develop
分支合并到 master
分支中。在未来,甚至可能只有一个分支。
据我所知,有人可能会说这是一件好事,但这不会发生,但这就是我们所知正如所提到的,这一切都很好,工作开始,并使用Jenkinsfile来检查基于直接推送和/或请求的回购,开始构建,测试运行等等......但是缺少的部分是将请求合并回来。
After还检查构建原始PR(与基本分支合并)复选框,这启用了Jenkins提供的环境变量 $ CHANGE_ID
拉请求,是拉请求号。这可以在阶段中使用:
stage(合并请求){
steps {
withCredentials([usernamePassword(credentialsId:'credential-value',usernameVariable:'ACCESS_TOKEN_USERNAME',passwordVariable:'ACCESS_TOKEN_PASSWORD',)]){
shcurl -X PUT -d'{\commit_title \\:\合并拉请求\\}'https://github.ibm.com/api/v3/repos/org-name/repo-name/pulls/$CHANGE_ID/merge?access_token=$ACCESS_TOKEN_PASSWORD
}
}
}
更新:验证合并实际上是成功的:
脚本{
shcurl -o - -s -w \\\\
%{http_code} \\\
\-X PUT -d'{\commit_title \:\合并拉请求\\}'https://github.ibm.com/api/v3/ repos / **** / **** / pulls / $ CHANGE_ID / merge?access_token = $ JENKINSBOT_PSW | tail -1> mergeResult.txt
def mergeResult = readFile( mergeResult.txt)。修剪()
如果(mergeResult!= 200){
错误 无法合并!
} else {
//发送Slack消息等
}
}
}
TL;DR: Given the following declarative Jenkinsfile, how can I merge the pull request following a successful build? http://pastebin.com/uivA0MqF
In the (Multibranch) Job configuration, under Branch Source > Advanced I have the following setup:
And in GitHub I have a web hook that listens to pull request
and push
events.
Edit: I have attempted the following, but it currently fails with a syntax error:
def gitPullRequestNumber = null
...
...
stage ("Merge pull request") {
steps {
gitPullRequestNumber = sh "git ls-remote origin 'pull/*/head:develop'"
sh "curl -X PUT -d '{\'commit_title\': \'Merge pull request\'}' <<GIT REPO URL>>/pulls/${gitPullRequestNumber}/merge?access_token=<<ACCESS-TOKEN"
}
}
The error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 58: Expected a step @ line 58, column 13.
gitPullRequestNumber = sh "git ls-remote origin 'pull/*/head:develop'"
^
1 error
Background:
We have a master
branch and a develop
branch. Developers will be doing their pull requests against the develop
branch where tests will be run. If the tests pass, the pull request should be merged into the branch. Review of the code is done before a pull request is opened. When decided, the develop
branch is merged to the master
branch. In the future there may even be only a single branch.
I understand that some may say that it is a good thing this doesn't happen but this is what we are currently trying to achieve.
As mentioned, this all works fine and the job starts and uses the Jenkinsfile for checking out the repo based on direct pushes and/or pull requests, the build starts, the tests run and so on... however the missing piece is merging the pull requests back.
After also checking the checkbox for Build origin PRs (merged with base branch) this enabled a Jenkins-provided environment variable, $CHANGE_ID
that in the case of a pull request, is the pull request number. This could then be used in the stage:
stage ("Merge pull request") {
steps {
withCredentials([usernamePassword(credentialsId: 'credential-value', usernameVariable: 'ACCESS_TOKEN_USERNAME', passwordVariable: 'ACCESS_TOKEN_PASSWORD',)]) {
sh "curl -X PUT -d '{\"commit_title\": \"Merge pull request\"}' https://github.ibm.com/api/v3/repos/org-name/repo-name/pulls/$CHANGE_ID/merge?access_token=$ACCESS_TOKEN_PASSWORD"
}
}
}
Update: to verify the merge was actually successful:
script {
sh "curl -o - -s -w \"\n%{http_code}\n\" -X PUT -d '{\"commit_title\": \"Merge pull request\"}' https://github.ibm.com/api/v3/repos/****/****/pulls/$CHANGE_ID/merge?access_token=$JENKINSBOT_PSW | tail -1 > mergeResult.txt"
def mergeResult = readFile('mergeResult.txt').trim()
if (mergeResult != "200") {
error "Unable to merge!"
} else {
// Send a Slack message, etc
}
}
}
这篇关于如何使用Jenkinsfile合并拉取请求的成功构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!