gpt4 book ai didi

Jenkins ssh-agent 启动然后在管道构建中立即停止

转载 作者:行者123 更新时间:2023-12-02 10:44:10 28 4
gpt4 key购买 nike

我有一个简单的 Jenkins 管道构建,这是我的 Jenkins 文件:

pipeline {
agent any
stages {
stage('deploy-staging') {
when {
branch 'staging'
}
steps {
sshagent(['my-credentials-id']) {
sh('git push joe@repo:project')
}
}
}
}
}

我正在使用 sshagent 推送到远程服务器上的 git 存储库。我已经创建了指向 Jenkins master ~/.ssh 中的私钥文件的凭据。

当我运行构建时,我得到以下输出(我用 * 替换了一些敏感信息):

[ssh-agent] Using credentials *** (***@*** ssh key)
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent] Exec ssh-agent (binary ssh-agent on a remote machine)
$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-cjbm7oVQaJYk/agent.11558
SSH_AGENT_PID=11560
$ ssh-add ***
Identity added: ***
[ssh-agent] Started.
[Pipeline] {
[Pipeline] sh
$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 11560 killed;
[ssh-agent] Stopped.
[TDBNSSBFW6JYM3BW6AAVMUV4GVSRLNALY7TWHH6LCUAVI7J3NHJQ] Running shell script
+ git push joe@repo:project
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

如您所见,ssh-agent 启动,然后立即停止,然后运行 ​​git push 命令。奇怪的是它曾经正确工作过一次,但这看起来完全是随机的。

我对 Jenkins 还很陌生 - 我是否遗漏了一些明显的东西?感谢任何帮助,谢谢。

编辑:我正在运行一个多分支管道,以防有帮助。

最佳答案

我最近遇到了类似的问题,尽管它是在 docker 容器内。日志给人的印象是 ssh-agent 退出得太早,但实际上问题是我忘记将 git 服务器添加到已知主机。

我建议 ssh-ing 到你的 jenkins master 并尝试执行与管道使用 ssh-agent (cli)相同的步骤。然后你就会看到问题出在哪里。

例如:

eval $(ssh-agent -s)
ssh-add ~/yourKey
git clone

如上所述on help.github.com

更新:如果尚未添加,这里有一个用于添加knownHosts的实用程序:

/**
* Add hostUrl to knownhosts on the system (or container) if necessary so that ssh commands will go through even if the certificate was not previously seen.
* @param hostUrl
*/
void tryAddKnownHost(String hostUrl){
// ssh-keygen -F ${hostUrl} will fail (in bash that means status code != 0) if ${hostUrl} is not yet a known host
def statusCode = sh script:"ssh-keygen -F ${hostUrl}", returnStatus:true
if(statusCode != 0){
sh "mkdir -p ~/.ssh"
sh "ssh-keyscan ${hostUrl} >> ~/.ssh/known_hosts"
}
}

关于Jenkins ssh-agent 启动然后在管道构建中立即停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49275255/

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