使用ssh,scp或sftp在jenkins管道中使用ssh凭据

joh*_*nfo 13 ssh jenkins jenkins-pipeline

我想使用scp/ssh将一些文件上传到服务器.我发现我需要使用基于证书的身份验证,但问题是如何?我真正想做的是使用与Jenkins中存储的git - passworded ssh cert相同的凭证.但是,我无法弄清楚如何 - 片段生成器没有明显的选择.

其他人做什么?是否有未记录的功能可以做到这一点?

tia*_*eng 28

    withCredentials([sshUserPrivateKey(credentialsId: "yourkeyid", keyFileVariable: 'keyfile')]) {
       stage('scp-f/b') {
        sh "scp -i ${keyfile} do sth here"
       }
   }
Run Code Online (Sandbox Code Playgroud)

也许这就是你想要的.安装Credentials Plugin
Credentials Binding Plugin.添加一些凭据,然后您将获得"yourkeyid",将此凭据绑定到keyFileVariable,passwordVariable等.

更多细节和文档可以在Jenns Credentials绑定插件,Credentials插件,SSH管道步骤插件的Github站点上找到

  • 我想补充一点,`sh` 中使用的凭据不应放在双引号之间,而应放在单引号之间,即 `sh 'scp -i ${keyfile} do sth here'`。https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#interpolation-of-sensitive-environment-variables (2认同)

Joe*_*ers 11

如果安装SSH代理插件,则可以使用ssh-agent管道步骤运行带有ssh-agent活动的shell脚本.在ssh-agent需要詹金斯凭证ID(一个需要密码的ssh证书,就像你有GIT中的一个).