Skip to content

Instantly share code, notes, and snippets.

@martinda
Created April 6, 2016 23:40
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save martinda/b2ece95c2c71ddb4d4a762f0a02561b3 to your computer and use it in GitHub Desktop.
Save martinda/b2ece95c2c71ddb4d4a762f0a02561b3 to your computer and use it in GitHub Desktop.
Jenkins Pipeline DSL code to demonstrate git merge before build
// Jenkins Pipeline DSL to demonstrate git merge before build
node {
String path = '/tmp/jenkins/upstream-repo'
sh "rm -rf ${path}"
ws(path) {
sh 'git --version'
sh 'git init'
sh 'touch README.md; git add README.md; git commit -m "init"'
sh 'git checkout -b pull-requests/1/from'
sh 'touch file.txt; git add file.txt; git commit -m "Add file"'
}
// sh "git clone ${path} ."
sh 'pwd; tree; ls;'
checkout([
$class: 'GitSCM',
branches: [[name: 'refs/heads/master']],
userRemoteConfigs: [[
name: 'origin',
refspec: 'pull-requests/1/from',
url: path
]],
extensions: [
[
$class: 'PreBuildMerge',
options: [
fastForwardMode: 'NO_FF',
mergeRemote: 'origin',
mergeStrategy: 'MergeCommand.Strategy',
mergeTarget: 'master'
]
],
[
$class: 'LocalBranch',
localBranch: 'master'
]]
])
sh 'git log -n 10 --graph --pretty=oneline --abbrev-commit --all --decorate=full'
}
@ernetas
Copy link

ernetas commented Mar 2, 2017

It is even better for Jenkins pipeline job itself have the prebuildmerge extension setup and use Jenkinsfile from SCM. This way even your whole Jenkinsfile gets merged before being executed.

@mrooney
Copy link

mrooney commented Oct 26, 2017

@ernetas, better or worse depending on your use-case. If you define it that way and there's a merge conflict, it will happen before getting into your Jenkinsfile and you can't perform any failure notifications.

Copy link

ghost commented Apr 27, 2018

could please help me out, git-merge-after-build.dsl help me

@gandadil
Copy link

I need to merge develop to master branch and I am using this approach. Do I need to push to git or this is done automatically at the end of my pipeline?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment