Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto merge Git branches prior to a Jenkins build?

Tags:

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.

like image 757
Rpj Avatar asked Jul 17 '13 09:07

Rpj


People also ask

How do I merge two branches automatically?

Enable automatic branch merging for a single repositoryGo to Repository settings > Branches. Under Automatic merging, select the On status and then select Save.

Is it possible to git merge push using Jenkins pipeline?

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.

How do I force merge a branch?

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.

What git strategies are used to merge two branches?

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.


3 Answers

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

like image 158
penguin359 Avatar answered Sep 21 '22 13:09

penguin359


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"         ] ] 
like image 36
Dragas Avatar answered Sep 19 '22 13:09

Dragas


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'
            ],
        ],
    ]
)
like image 27
CodeMonkey Avatar answered Sep 18 '22 13:09

CodeMonkey