我在 jenkins 中有一个 Github Organisation
项目。在我存储库的根目录下,我的 Jenkinsfile
看起来像这样:
node {
def jenkinsCredsId = 'xxxxxxxx-yyyy-zzzz-aaaa-bbbbbbbbbbbb'
stage 'Checkout'
checkout scm
// I also tried the following:
// checkout scm: [$class: 'GitSCM', source: 'ssh://git@github.com:MY_ORGANISATION/jenkins-testing-temp.git', clean: true, credentialsId: jenkinsCredsId]
stage 'Build'
// generate some artefact (dist.zip)
stage 'Release'
sshagent([jenkinsCredsId]) {
sh '''
git remote -v // show remotes
ssh-add -l // show currently loaded ssh keys fingerprints
git fetch --all --tags // IT FAILS HERE
CURRENT_BUILD_TAG="some_build/${BUILD_NUMBER}"
git tag ${CURRENT_BUILD_TAG}
git push --tags
github-release release \
--security-token ${GITHUB_RELEASE_TOKEN} \
--user MY_ORGANIZATION \
--repo MY_REPO \
--tag ${CURRENT_BUILD_TAG} \
--name ${CURRENT_BUILD_TAG}
github-release upload \
--security-token ${GITHUB_RELEASE_TOKEN} \
--user MY_ORGANIZATION \
--repo MY_REPO \
--tag ${CURRENT_BUILD_TAG} \
--name ${CURRENT_BUILD_TAG} \
--file dist.zip
'''
}
这里有几行用于测试存储库访问,目前在 git fetch
部分失败,出现以下错误:
fatal: could not read Username for 'https://github.com': No such device or address
来自上述 Jenkinsfile
的 git remote -v
命令输出类似于 origin https://github.com/MY_ORGANIZATION/MY_REPO
的内容。
我发现了几个相关的问题:
最佳答案
事实证明,Github Organization
项目仅使用 https 凭据作为 Scan credentials
(如上图)。
解决方案是点击 Advanced
按钮,并在 Checkout credentials
下 pull 列表中实际选择一个 ssh
凭证,而不是默认的 - 与扫描凭据相同 -
。
请注意,带星号的凭据是用户/密码条目,这就是我发现问题的方式。
这样,checkout scm
将使用 ssh 而不是 sshagent([jenkinsCredsId]) {
block 将按预期工作,让您创建标签、获取和推送根据您的权利。 :)
关于git - 如何在 github 组织 Jenkins 工作流中 check out ssh 远程并在 Jenkinsfile 中使用 ssh 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40618449/