首页 > 解决方案 > ssh-add 使 Jenkins 管道作业失败

问题描述

Jenkins (2.162),更新了模块。我需要为 cargo build 添加私有 github 依赖项。所以,我需要先将 SSH 密钥存储到 Jenkins 容器中cargo build

我做了:

stage('Build') {
    steps{
        script {
            dir('api'){
                withCredentials([string(credentialsId: 'GitKeyText', variable: 'ID_RSA')]) {
                    sh '''
                        set +x
                        eval `ssh-agent -s`
                        mkdir ~/.ssh
                        echo ${ID_RSA} >~/.ssh/id_rsa
                        chmod go-r ~/.ssh/id_rsa
                        ssh-add
                        cargo build
                    '''
                }
            }
            input message: "wait"
        }
    }
}

一切看起来都很好,并且这个命令序列在 docker 容器内手动运行良好。但是,Jenkins 的工作一直失败,ssh-add没有任何错误消息。就ERROR: script returned exit code 1在 Jenkins 控制台日志的末尾。

add01:我在代码中添加了回显注释,并更改set +xset -x

ssh-add(控制台输出)没有输出

.....
+ echo before ssh-add
before ssh-add
+ ssh-add
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // script
Post stage
.....

标签: dockersshjenkins-pipelinejenkins-pluginsssh-keys

解决方案


我使用了 Jenkins SSH 代理插件。所有工作都按预期工作。

script {
    dir('contract_api'){
        sshagent(['GitSSHcred']) {
            sh 'cargo build'
        }
    }
}

推荐阅读