[英]Jenkinsfile with docker agent not able to load ssh key
我有一個簡單的管道:
pipeline {
agent {
docker {
image 'python:3.8-alpine3.15'
}
}
...
steps {
withCredentials([sshUserPrivateKey(credentialsId: "repo", keyFileVariable: 'keyfile')]){
sh '''
set +x
eval `ssh-agent -s`
ssh-add ${keyfile}
git clone git@gitlab.com/blabla
'''
}
}
}
錯誤的output為:
Masking supported pattern matches of $keyfile
Agent pid 53
+ ssh-add **** (blabla@blabla.com)
...
Host key verification failed.
fatal: Could not read from remote repository.
我在同一台機器上用相同的密鑰逐步嘗試了相同的步驟並且它有效,問題在於withCredentials
綁定。 改成ssh-agent
插件是不可行的。
有誰知道出了什么問題以及為什么我無法成功加載憑據?
經過大量調試,對我有用的是:
pipeline {
agent {
docker {
image 'python:3.8-alpine3.15'
}
}
...
steps {
sshagent(credentials: ['repo']) {
sh '''
set +x
mkdir ~/.ssh
ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
git clone git@gitlab.com/blabla
pip install -r requirements.txt
'''
}
}
}
最后我最終使用了sshagent
插件,否則如果你需要使用withCredentials
插件,你應該考慮:
pipeline {
agent {
docker {
image 'python:3.8-alpine3.15'
}
}
...
steps {
withCredentials([sshUserPrivateKey(credentialsId: "repo", keyFileVariable: 'keyfile')]) {
sh '''
set +x
mkdir ~/.ssh
ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
eval `ssh-agent -s`
ssh-add ${keyfile}
git clone git@gitlab.com/blabla
pip install -r requirements.txt
'''
}
}
}
我個人認為withCredentials
的實現更容易實現,因為你不依賴於外部插件。
外部參考:
SSH Agent插件官網: https://plugins.jenkins.io/ssh-agent/
Jenkins錯誤論壇: https://issues.jenkins.io/browse/JENKINS-36997
Jenkins錯誤論壇: https://issues.jenkins.io/browse/JENKINS-43050
通過這兩個實現,將 ssh 密鑰傳遞到在 docker 容器內運行的管道時應該沒有任何問題。
免費軟件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.