Jenkins 流水线 Git 推送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53325544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jenkins Pipeline Git Push
提问by David
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?
有没有办法让我做同样的事情来推回在构建中较早克隆的存储库?
回答by David
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.groovyand https://issues.jenkins-ci.org/browse/JENKINS-28335.
阅读https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/push-git-repo/pushGitRepo.groovy和https://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')
}
回答by Martynas Petu?ka
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}")
}