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

Jenkins ssh-agent starts and then stops immediately in pipeline build

我有一个简单的 jenkins 管道构建,这是我的 jenkinsfile:

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 启动后立即停止,然后 运行s git 推送命令。奇怪的是它曾经正确工作过一次,但那似乎完全是随机的。

我对 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"
    }
}

我是在docker里面用的,加到我Jenkins master的known_hosts感觉有点乱,所以我选择了这样的:

  1. 在 Jenkins 中,创建一个“Secret text”类型的新凭证(我们称之为 GITHUB_HOST_KEY),并将其值设置为主机密钥,例如:
# gets the host for github and copies it. You can run this from
# any computer that has access to github.com (or whatever your
# git server is)
ssh-keyscan github.com | clip
  1. 在您的 Jenkinsfile 中,将字符串保存到 known_hosts
pipeline {
    agent { docker { image 'node:12' } }
    stages {
        stage('deploy-staging') {
            when { branch 'staging' }
            steps {
                withCredentials([string(credentialsId: 'GITHUB_HOST_KEY', variable: 'GITHUB_HOST_KEY')]) {
                    sh 'mkdir ~/.ssh && echo "$GITHUB_HOST_KEY" >> ~/.ssh/known_hosts'
                }
                sshagent(['my-credentials-id']) {
                    sh 'git push joe@repo:project'
                }
            }
        }
    }
}

这确保您使用的是“受信任的”主机密钥。