简体   繁体   English

Jenkins 流水线 Git 推送

[英]Jenkins Pipeline Git Push

Is there as way to have a stage like so in a Jenkinsfile:有没有办法在 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 git push --set-upstream origin release/v1.0.3 致命:无法读取“ https://github.com ”的用户名:没有这样的设备或地址脚本返回退出代码 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.这部分工作正常(克隆),大概是因为我可以为这一步提供 github 的 jenkins 凭证 ID。

Is there a way for me to do the same to push back to a repo that was cloned earlier in the build?有没有办法让我做同样的事情来推回在构建中较早克隆的存储库?

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 . 在阅读https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/push-git-repo/pushGitRepo.groovyhttps://issues.jenkins-ci.org/browse/JENKINS-之后28335

An alternative approach using SSH keys appears to be : 使用SSH密钥的另一种方法是:

sshagent(['credentiald-id-using-ssh-key']) 
 {
    sh('git command or program calling git inside') 
 }

To get this working for blue ocean (which uses https connection) use the following: 要使此功能适用于蓝色海洋(使用https连接),请使用以下命令:

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

Github no longer supports password authentication. Github 不再支持密码认证。 Instead use a personal access token.而是使用个人访问令牌。 Please refer: How to use Github Personal Access Token in Jenkins请参考: 如何在Jenkins中使用Github Personal Access Token

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM