Is it possible to Git merge / push using Jenkins pipeline

119,233

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:

  1. Creating a username/password credential bitbucketUsernamePassword.
  2. Using that credential to checkout.
  3. Setting credential.helper before doing checkout.
  4. 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!!

Share:
119,233
Progger
Author by

Progger

All around technologist.

Updated on July 09, 2022

Comments

  • Progger
    Progger almost 2 years

    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.

  • phil swenson
    phil swenson over 7 years
    Really? When I try the above answer (using ssh credential just live above) and try to do anything that communicates with the remote I 'Permission denied (publicly).' it is able to checkout the code from git as above, but the credentials are not cached
  • Pom12
    Pom12 over 7 years
    Let's continue with your specific problem on your dedicated question
  • vinay
    vinay about 6 years
    this is still asking for username and password
  • andrzej.szmukala
    andrzej.szmukala about 6 years
    Make sure you have login via SSH enabled on your git and you have added the correct SSH keys to your Jenkins. This has worked for me and 6 other people without need for any additional config.
  • Sebastian Lenartowicz
    Sebastian Lenartowicz over 4 years
    Ensure you've actually cloned over ssh as well. If you're cloning over HTTPs, you need to use user/pass on the trip back as well.
  • Marko Kohtala
    Marko Kohtala over 4 years
    SSH_KEY is not used by git nor ssh. This does not work.
  • Litty Philip
    Litty Philip over 4 years
    With respect to any SCM, there are two ways of authentication, https and ssh. For ssh authentication, keys are required. May be you are using this in some other manner.
  • Marko Kohtala
    Marko Kohtala over 4 years
    For ssh it is of no use to have private key available in file named by SSH_KEY environment variable when nothing uses SSH_KEY within the withCredentials. This question has other answers that make use of SSH_KEY by sh("GIT_SSH_COMMAND='ssh -i ${SSH_KEY}' git ...") or such. Those can work. This answer contains no working solution.
  • Litty Philip
    Litty Philip over 4 years
    The credentialId mentioned is a credential created in Jenkins as 'SSH Username with private key'. This is a pre-requisite and the rest is internally handled by Jenkins. I expected that this is already known to the answer seeker. And it has worked for me and an upvote of 4, I think means that it has worked for 4 of us. :)
  • mohifleur
    mohifleur over 3 years
    Thanks, this works great for https! I also had to add sh "git config --global user.email" && sh "git config --global user.name" to be able to commit and push.
  • a.ha
    a.ha about 3 years
    @LittyPhilip, are you sure? In my case, the command uses the key under "/var/jenkins_home/.ssh/id_rsa", which is the default one on the server and not the mentioned one in the Credentials Plugin. Is it possible that in your case both keys are equal?
  • Andi
    Andi over 2 years
    don't use force push. you could kill your entire git history
  • Gerold Broser
    Gerold Broser over 2 years
  • simbo1905
    simbo1905 over 2 years
    this worked for me on "CloudBees Jenkins Enterprise 2.263.2.3-rolling" when nothing else here worked. hero!
  • AnjanaAK
    AnjanaAK almost 2 years
    This snippet is also giving me an error that GIT_SSH_COMMAND: not found in the Jenkins declarative pipeline. But replacing the line of specifying the environment variable with the following line fixed that issue: export GIT_SSH_COMMAND='ssh -i $SSH_KEY' . It is working correctly.