Can Git be merged / clicked using Jenkins pipeline

I am trying to create a Jenkins workflow using the Jenkinsfile. All I want to do is keep an eye on the development of “development” for change. When a change occurs, I want the git tag and the merge to be done by the wizard. I am using GitSCM Step, but the only thing that seems to support is git clone. I do not want to lay out to do the tag / merge, but I do not see the path around it. Does anyone know if this is possible? I am using BitBucket (on-prem) for my git server.

+24
git bitbucket jenkins groovy
source share
9 answers

This is not possible at the moment, because the GitPublisher plugin, the plugin previously responsible for tagging / merging / clicking on freestyle, has not been updated to be compatible with Jenkins pipelines. You can follow this issue on the page with the plug-ins for pipeline connections , and the highlighted GitPublisher is Jira issue .

So it seems that the only option you have is to actually lay out tag / merge commands ... However, note that you can still take advantage of some of Jenkins built-in features, such as using credentials for your Git repo which makes it quite simple and then tag / merge according to your needs.

Statement example:

 git url: "ssh:// jenkins@your-git-repo :12345/your-git-project.git", credentialsId: 'jenkins_ssh_key', branch: develop 

Then tag / merge / push will be pretty simple:

 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.

+25
source share

If you use git credentials, you can use the SSH agent plugin, as 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" } 
+17
source share

In my case, I was forced to work with HTTPS. I solved this:

  • Create username / password credentials bitbucketUsernamePassword .
  • Use this identity for verification.
  • Install credential.helper before doing the validation.
  • Running a git checkout branch to get the remote tracker of the local branch.

Then I can promote things with git push .

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' 
+7
source share

Yes this!! After a few days of struggle, I got a simple code block for a script pipeline script that worked for me.

 withCredentials([sshUserPrivateKey(credentialsId: '<credential-id>', keyFileVariable: 'SSH_KEY')]) { sh("git push origin <local-branch>:<remote-branch>") } 

Enjoyy !!

+4
source share

This topic 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") } 

Username and password are hidden in the log:

 + git push http://****:****@git.corp.mycompany.com/repo 
+3
source share

I had to perform a similar task, and I managed to get it to work with one of the options: 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 identifier of the credentials you set in Jenkins. The path to the ssh key is made available as an SSH_KEY environment SSH_KEY .

+2
source share

In my case, I want to send to the CodeCommit repository via SSH. sshagent does not work because it does not install User . This ultimately 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}' } } 
+1
source share

Got it while working with sshagent on the blue ocean (which uses https authentication)

 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}") } 
+1
source share

Litti Philips's answer gave 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> """ } 
0
source share

All Articles