Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform a git merge in a Jenkins pipeline?

Tags:

git

jenkins

When Jenkins builds a non-master git branch, I want to merge in master automatically at a specific point. I don't want to use the git plugin's auto merge feature.

However, I've added the stage:

stage('Merge master') {
  sh 'git merge master'
}

But I get an error:

merge: master - not something we can merge

Jenkins seems to be checking out my branch in a weird way that prevents me being able to then merge master. What do I need to do to merge master into my branch?

like image 252
jbrown Avatar asked Oct 29 '25 03:10

jbrown


2 Answers

When Jenkins cloned your repo, it didn't checkout a local master branch

The following script should allow you to test a merge of master.

git fetch --all
git checkout master
git checkout branch-name
git merge master
like image 84
Edmund Dipple Avatar answered Oct 30 '25 18:10

Edmund Dipple


Jenkins is probably just setting its head to the remote branch reference rather than creating a local branch.

Before I get into how to get around this, have you considered what should happen if the merge runs into conflicts? Because probably what will happen is that it will put your build server into an unexpected state that you'll have to manually clean up. This is why it is not typical to do merges other than locally (and interactively).


UPDATE : The following is retained from my original answer for completeness, but did not work:

If you want to try in spite of that, you might need to say something like

git merge origin/master

So it seems that Jenkins may be doing one of three things:

It may be using a remote name other than origin. If that's all it is, then Edmund Dipple's answer should help.

It may be doing its clone with options like --single-branch, or otherwise configuring the remote's refspec so that master is not fetched. In that case you would have to either reconfigure the repo yourself, or include an explicit refspec to fetch.

It may not be cloning at all. This is mostly similar to the second case as far as how you'd address it; I guess the main thing I'd add is that it's conceivable the remote isn't even configured.

So the next step in my mind would be to have your build script show the local repo's configuration (git config -l) looking for remote and fetch settings, and figure out next steps from there.

like image 42
Mark Adelsberger Avatar answered Oct 30 '25 18:10

Mark Adelsberger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!