我对 Jenkins 管道中的 git 命令有疑问。有时,用户使用 git fetch 或 git push 时不带 withCredentials。 由于历史原因,在 Jenkins 节点上设置了一个选项:
git config --global credential.helper cache
此选项可能会破坏 Github Enterprise 的主要身份验证。有时会出现错误:
stderr: fatal: Authentication failed for 'https://github.com/org/project'
或
Failed to connect to repository : Command "git ls-remote -h https://github.com/org/project.git HEAD" returned status code 128:
stdout
stderr: fatal: Authentication failed for 'https://github.com/org/project.git/'
有 GitHub App 作为凭据。
禁用选项 credential.helper=cache 有助于解决此问题,但会破坏其他管道。
我制作了示例管道并确认了此错误。不使用管道定义中的凭据。除非在第一阶段我使用:
git config credential.helper "cache --timeout=3600"
这是一种解决方法,除非必须,否则我不想使用它,因为有很多管道,大多是多分支类型。
有什么方法可以从管道本身使用的管道定义中获得相同的凭据,还是我必须添加这些解决方法或使用 withCredentials 步骤?
如果我要使用 withCredentials,那么无论凭证类型如何,我该如何使用它:ssh 私钥、github 应用程序或简单的用户名/密码?
还是从一开始就普遍做错了?管道等中缺少 withCredentials
withCredentials([usernamePassword(credentialsId: 'jenkins-readonly', usernameVariable: 'GITHUB_APP', passwordVariable: 'GITHUB_ACCESS_TOKEN')]) {
sh "echo https://x-access-token:${GITHUB_ACCESS_TOKEN}@github.com > ~/.git-credentials"
sh "git config --global credential.helper 'store --file ~/.git-credentials'"
## do your stuff
sh 'git config --global --remove-section credential'
sh 'rm ~/.git-credential
}
像这样尝试一下