gpt4 book ai didi

git - Jenkins 管道无法将代码检入 git

转载 作者:太空狗 更新时间:2023-10-29 13:00:59 26 4
gpt4 key购买 nike

在我的 jenkins 管道项目中,我可以很好地从 git check out 代码...但是我们需要做一些 git 检入,并且凭证显然没有被缓存。

    stage 'Checkout'
git url: 'git@bitbucket.org:myproj.git', branch: 'master', credentialsId: '012ce21d-e920-44ee-b6f7-08df8ab41de0', variable: 'CREDENTIALS'
sh('git push') <---- fails with Permission denied (public key).

这里是示例输出:

Entering stage Checkout
Proceeding
[Pipeline] git
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url git@bitbucket.org:myproj # timeout=10
Fetching upstream changes from git@bitbucket.org:myproj.git
> git --version # timeout=10
using GIT_SSH to set credentials
> git -c core.askpass=true fetch --tags --progress git@bitbucket.org:myproj.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision cc35402c6b39e8a1f8d55a831d2d10215d47ccd0 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f cc35402c6b39e8a1f8d55a831d2d10215d47ccd0 # timeout=10
> git branch -a -v --no-abbrev # timeout=10
> git branch -D master # timeout=10
> git checkout -b master cc35402c6b39e8a1f8d55a831d2d10215d47ccd0
> git rev-list cc35402c6b39e8a1f8d55a831d2d10215d47ccd0 # timeout=10
[Pipeline] sh
[myproj] Running shell script
+ git push --set-upstream origin master
Warning: Permanently added the RSA host key for IP address '192.192.143.2' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

有人对此有好的解决方案吗?

谢谢

最佳答案

从 Jenkins 管道示例存储库中提取,我们可以避免使用 sshagent:https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/push-git-repo/pushGitRepo.Groovy

那么对于您的示例,解决方案应该是使用凭据绑定(bind)插件 ( https://wiki.jenkins-ci.org/display/JENKINS/Credentials+Binding+Plugin ) 并使用此代码段:

stage ('Checkout') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '012ce21d-e920-44ee-b6f7-08df8ab41de0', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
sh("git tag -a some_tag -m 'Jenkins'")
sh('git push git://${GIT_USERNAME}:${GIT_PASSWORD}@bitbucket.org:myproj.git')
}
}

关于git - Jenkins 管道无法将代码检入 git,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39237910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com