5
votes

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 - (e.g. '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!

1
did you achieve this in the end?lqbweb

1 Answers

0
votes

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