How to merge a successful build of a pull request using a Jenkinsfile

时光怂恿深爱的人放手 提交于 2019-12-08 13:32:44

问题


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.


回答1:


Use git commands to merge as written

#Checkout main branch
git checkout master 

#Merge it with pull request (insert your pull id)
git merge --no-ff pull/ID/head:develop

#TEST Local Merge Branch Compilation If Desired
#msbuild ...

#Push merged information back to git
git push origin master



回答2:


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
      }
    }
}


来源:https://stackoverflow.com/questions/42664863/how-to-merge-a-successful-build-of-a-pull-request-using-a-jenkinsfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!