Is it possible to Git merge / push using Jenkins pipeline

感情迁移 提交于 2019-12-02 22:08:35

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.

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"
}

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'

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!!

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

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.

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}'
    }
}

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}")
                }
J Blackburn

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>
   """
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!