How to auto merge Git branches prior to a Jenkins build?
I have 2 builds, one for branch master
and one for production.
I would like to do Git merge origin/master
when I do the production build.
Enable automatic branch merging for a single repositoryGo to Repository settings > Branches. Under Automatic merging, select the On status and then select Save.
It is not possible at the moment because GitPublisher plugin, the plugin previously responsible for tagging/merging/pushing in freestyle jobs, has not been updated to be compatible with Jenkins pipelines.
20201029 To re-synchronise a branch with updates that have been made to the main branch on the repository, first ensure the local main branch has been updated using a checkout and pull for the main branch. Then checkout the branch of interest and merge from the updated local main.
Recursive is the default merge strategy when pulling or merging one branch. Additionally this can detect and handle merges involving renames, but currently cannot make use of detected copies.
That's supported by the latest Git Plugin on Jenkins. Just set Checkout/merge to local branch
to production
under the Advanced settings for Git in the Job configuration.
Then set the Branches to build
to master, or just leave it blank to have Jenkins try and merge and build each other branch it finds to production.
It will do one merge/build
for each branch. It can also push the merged branch back to the source it pulled from.
Check this out:
https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-AdvancedFeatures
You can do premerging via extensions block in pipelines (assuming you configured checkout: [ $class: 'GIT_SCM' ]
already.)
[ $class: "PreBuildMerge", options: [ mergeTarget: "master", fastForwardMode: "FF", mergeRemote: "origin", mergeStrategy: "OURS" ] ]
I found at least on current Jenkins dragas' solution is almost correct. The merge strategy has to be theirs (or RECURSIVE_THEIRS since the plugin doesn't offer THEIRS) since the plugin checks out origin/master and merges the branch onto it, instead of merging origin/master onto the branch.
Also I found a bit of context missing. And the merge command needs email and username to be set:
checkout(
[
$class: 'GitSCM',
extensions: [
[
$class: "PreBuildMerge",
options: [
mergeTarget: "master",
fastForwardMode: "FF",
mergeRemote: "origin",
mergeStrategy: "RECURSIVE_THEIRS"
],
],
[
$class: 'UserIdentity',
email: '[email protected]',
name: 'user123'
],
],
]
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With