简体   繁体   中英

Jenkins with Git - Merge another branch *into* the current branch before build

I have a jenkins CI setup where development work is done in feature branches and when a pull request is made against the master branch, Jenkins runs a build against that pull request to confirm that all tests are passing. This is then synced back to our repo so that the person reviewing the pull request knows that the tests aren't goofed.

I would like to update this setup so that before Jenkins builds the branch from the pull request, it merges master into the pull request branch and builds the result. This merge should not be pushed back because it is still pending review, but this will ensure that tests are running against what the actual post-pull-request result will be.

I have found info on and tried the Merge before build action but this appears to merge the pull request branch into master , then if that succeeds, check the pull request branch back out and build. This is great for catching future merge conficts, but still does not catch the pull request branch up to master before building.

In the documentation for the Git Plugin ( https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin ) it mentions a feature that appears to do exactly what I want: "Next, pick a particular branch name as the integration target in the 'Advanced' section - (eg 'master', or 'stable'), and select 'Merge before build'", but this option is not available to me in Jenkins. I have the latest plugin so I am not sure if they removed this and didn't update their docs or what.

Does anyone know how to achieve the "merge another branch into my current branch and build the result" action?

Thanks!

You should check if PR Branch and master are in sync using SHA. If they are not in sync, git rebase before running the build stage.

stage ("Rebase Master") {
    pr_branch_name = sh(script: "curl https://github.com/api/v3/repos/${git_org}/${git_repo}/pulls/${CHANGE_ID} -H \"Content-Type: application/json\" -H \"authorization: token ${authentication_token}\" | jq -r .head.ref", returnStdout: true).trim()
    rebaseability = sh(script: "curl https://github.com/api/v3/repos/${git_org}/${git_repo}/pulls/${CHANGE_ID} -H \"Content-Type: application/json\" -H \"authorization: token ${authentication_token}\" | jq -r 'select(.base.sha==.head.sha) | \"up-to-date\"'", returnStdout: true).trim()

    if (rebaseability != "up-to-date" ) {
        git checkout ${pr_branch_name}
        git rebase master
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM