Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline Git Push

Is there as way to have a stage like so in a Jenkinsfile:

stage('Create Branch & Push Branch') {
            steps {
                script {
                    sh "git checkout -b release/${NEW_TAG}"
                    sh "git push --set-upstream
                }
            }
    }

Currently this leads to:

  • git push --set-upstream origin release/v1.0.3 fatal: could not read Username for 'https://github.com': No such device or address script returned exit code 128

The repository was originally cloned earlier in the pipeline using:

checkout poll: false, scm: [$class: 'GitSCM', branches: [[name: 'develop']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout'], [$class: 'CleanCheckout'], [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'ci-github', url: 'https://github.com/my-org/my-repo.git']]]

That part works ok (the clone), presumably because I can supply this step with the jenkins credential id for github.

Is there a way for me to do the same to push back to a repo that was cloned earlier in the build?

like image 716
David Avatar asked Nov 15 '18 18:11

David


People also ask

Is it possible to Git merge push using Jenkins pipeline?

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.

How do I push Jenkins code to GitHub?

Step 1: go to your GitHub repository and click on 'Settings'. Step 2: Click on Webhooks and then click on 'Add webhook'. Step 3: In the 'Payload URL' field, paste your Jenkins environment URL. At the end of this URL add /github-webhook/.


2 Answers

I've made this work using:

withCredentials([usernamePassword(credentialsId: 'ci-github', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
                        sh('git push https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/my-org/my-repo.git')
                    }

After reading https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/push-git-repo/pushGitRepo.groovy and https://issues.jenkins-ci.org/browse/JENKINS-28335.

An alternative approach using SSH keys appears to be :

sshagent(['credentiald-id-using-ssh-key']) 
 {
    sh('git command or program calling git inside') 
 }
like image 53
David Avatar answered Sep 18 '22 00:09

David


To get this working for blue ocean (which uses https connection) use the following:

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}")
                }
like image 30
Martynas Petuška Avatar answered Sep 18 '22 00:09

Martynas Petuška