[英]Jenkins Pipeline Git Push
有没有办法在 Jenkinsfile 中拥有这样的阶段:
stage('Create Branch & Push Branch') {
steps {
script {
sh "git checkout -b release/${NEW_TAG}"
sh "git push --set-upstream
}
}
}
目前这导致:
- git push --set-upstream origin release/v1.0.3 致命:无法读取“ https://github.com ”的用户名:没有这样的设备或地址脚本返回退出代码 128
该存储库最初是在管道中使用以下方法克隆的:
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']]]
这部分工作正常(克隆),大概是因为我可以为这一步提供 github 的 jenkins 凭证 ID。
有没有办法让我做同样的事情来推回在构建中较早克隆的存储库?
我使用以下方法完成了这项工作:
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')
}
在阅读https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/push-git-repo/pushGitRepo.groovy和https://issues.jenkins-ci.org/browse/JENKINS-之后28335 。
使用SSH密钥的另一种方法是:
sshagent(['credentiald-id-using-ssh-key'])
{
sh('git command or program calling git inside')
}
要使此功能适用于蓝色海洋(使用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 不再支持密码认证。 而是使用个人访问令牌。 请参考: 如何在Jenkins中使用Github Personal Access Token
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.