Is it possible to Git merge / push using Jenkins pipeline
GitJenkinsGroovyBitbucketGit Problem Overview
I am trying to create a Jenkins workflow using a Jenkinsfile. All I want it to do is monitor the 'develop' branch for changes. When a change occurs, I want it to git tag and merge to master. I am using the GitSCM Step but the only thing that it appears to support is git clone. I don't want to have to shell out to do the tag / merge but I see no way around it. Does anyone know if this is possible? I am using BitBucket (on-prem) for my Git server.
Git Solutions
Solution 1 - Git
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 - Git
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 - Git
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 - Git
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:$password@git.corp.mycompany.com/repo")
}
The username and password are both obscured in the log:
+ git push http://****:****@git.corp.mycompany.com/repo
Solution 5 - Git
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!!
Solution 6 - Git
I've had to do a similar task and managed to get it to work with a variation of this: https://issues.jenkins-ci.org/browse/JENKINS-28335?focusedCommentId=320383&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-320383
withCredentials([sshUserPrivateKey(credentialsId: 'ci', keyFileVariable: 'SSH_KEY')]) {
sh 'echo ssh -i $SSH_KEY -l git -o StrictHostKeyChecking=no \\"\\$@\\" > local_ssh.sh'
sh 'chmod +x local_ssh.sh'
withEnv(['GIT_SSH=./local_ssh.sh']) {
sh 'git push origin develop'
}
}
Whereas ci
is the id of the credential you setup within Jenkins. The path to the ssh key becomes available as the environment variable SSH_KEY
and local_ssh.sh is added to current directory.
Solution 7 - Git
In my case, I want to push to a CodeCommit repository through SSH. sshagent
doesn't work because it does not set User
. This eventually did the job:
withCredentials([sshUserPrivateKey(credentialsId: CODECOMMIT_CREDENTIALS_ID, keyFileVariable: 'SSH_KEY', usernameVariable: 'SSH_USER')]) {
withEnv(["GIT_SSH_COMMAND=ssh -o StrictHostKeyChecking=no -o User=${SSH_USER} -i ${SSH_KEY}"]) {
sh 'git push ${CODECOMMIT_URL} ${COMMIT_ID}:refs/heads/${BRANCH}'
}
}
Solution 8 - Git
Found the solution with help of basic Jenkins and git functionality
stage('Git Push'){
steps{
script{
GIT_CREDS = credentials(<git creds id>)
sh '''
git add .
git commit -m "push to git"
git push https://${GIT_CREDS_USR}:${GIT_CREDS_PSW}@bitbucket.org/<your repo>.git <branch>
'''
}
}
}
https://www.jenkins.io/doc/book/using/using-credentials/ https://stackoverflow.com/questions/29776439/username-and-password-in-command-for-git-push
Solution 9 - Git
Got this working with sshagent on blue ocean (which uses https authorisation)
sshagent(credentials: ["406ef572-9598-45ee-8d39-9c9a227a9227"]) {
def repository = "git@" + env.GIT_URL.replaceFirst(".+://", "").replaceFirst("/", ":")
sh("git remote set-url origin $repository")
sh("git tag --force build-${env.BRANCH_NAME}")
sh("git push --force origin build-${env.BRANCH_NAME}")
}
Solution 10 - Git
Lately, the git plugin add this command:
stage('git push') {
steps {
withCredentials([
gitUsernamePassword(credentialsId: 'github', gitToolName: 'Default')
]) {
sh "git push"
}
}
}
Could not find the related documentation. I found that out in the "Pipeline Syntax" helper found in any Pipeline job.
Solution 11 - Git
Litty Philips' answer got me most of the way but I also needed to define GIT_SSH_COMMAND.
withCredentials([sshUserPrivateKey(credentialsId: '<credential-id>', keyFileVariable: 'SSH_KEY')]) {
sh """
GIT_SSH_COMMAND = "ssh -i $SSH_KEY"
git push origin <local-branch>:<remote-branch>
"""
}