Is it possible to Git merge / push using Jenkins pipeline
Solution 1:
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. You can follow that issue on both the pipeline plugins compatibility page and the dedicated GitPublisher Jira issue.
So it seems the only option you have is to actually shell out your tag/merge commands... However, note that you can still benefit from some Jenkins built-in capabilities such as the use of credentials for your Git repo, which make it pretty straightforward to then tag / merge following your needs.
Example check-out :
git url: "ssh://jenkins@your-git-repo:12345/your-git-project.git",
credentialsId: 'jenkins_ssh_key',
branch: develop
Then the tag / merge / push will be pretty straightforward :
sh 'git tag -a tagName -m "Your tag comment"'
sh 'git merge develop'
sh 'git commit -am "Merged develop branch to master'
sh "git push origin master"
I hope that one day GitPublisher will be released in a pipeline-compatible version, but for now this workaround should do.
Solution 2:
If what you're after are the git credentials you can use the SSH Agent plugin like in this link: https://issues.jenkins-ci.org/browse/JENKINS-28335?focusedCommentId=260925&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-260925
sshagent(['git-credentials-id']) {
sh "git push origin master"
}
Solution 3:
In my case I was forced to work with HTTPS. I solved it by:
- Creating a username/password credential bitbucketUsernamePassword.
- Using that credential to checkout.
- Setting credential.helper before doing checkout.
- Doing a git checkout branch to get a local branch tracking remote.
Then I am able to push things with git push after that.
Like this:
sh 'git config --global credential.helper cache'
sh 'git config --global push.default simple'
checkout([
$class: 'GitSCM',
branches: [[name: branch]],
extensions: [
[$class: 'CloneOption', noTags: true, reference: '', shallow: true]
],
submoduleCfg: [],
userRemoteConfigs: [
[ credentialsId: 'bitbucketUsernamePassword', url: cloneUrl]
]
])
sh "git checkout ${branch}" //To get a local branch tracking remote
Then I can do things like:
sh 'git push'
Solution 4:
This thread was really helpful. My Jenkins credentials are username/password so I used:
withCredentials([usernamePassword(credentialsId: 'fixed',
usernameVariable: 'username',
passwordVariable: 'password')]){
sh("git push http://$username:[email protected]/repo")
}
The username and password are both obscured in the log:
+ git push http://****:****@git.corp.mycompany.com/repo
Solution 5:
Yes, it is!! After struggling for days, I ended up with these simple code block for scripted pipeline script which worked for me.
withCredentials([sshUserPrivateKey(credentialsId: '<credential-id>', keyFileVariable: 'SSH_KEY')]) {
sh("git push origin <local-branch>:<remote-branch>")
}
Enjoyy!!