Jenkins管道Git推送

9

是否有一种方法可以在Jenkinsfile中像这样使用stage:

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 fatal: could not read Username for 'https://github.com': No such device or address script returned exit code 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。

那么,有没有办法让我向在构建过程中先克隆的存储库推送相同的内容?

3个回答

30

我使用了以下技术来完成这项工作:

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.groovyhttps://issues.jenkins-ci.org/browse/JENKINS-28335后,另一种可选方法是使用SSH密钥:

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

2
致命错误:您当前不在任何分支上。 要推送导致当前(分离的 HEAD)状态的历史记录,请使用 git push https://****:****@repository.git HEAD:<name-of-remote-branch> - Rob Juurlink

1

1
要使此功能适用于使用https连接的Blue Ocean,请使用以下内容:
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}")
                }

不确定对你来说有什么不同,但是我上面的原始示例在Blue Ocean上使用正常(至少对我来说是这样)。 - David

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接