jenkins 远程 withCredentials.sshUserPrivateKey 方式、identity 文件怎么配置?

jenkins 远程 withCredentials.sshUserPrivateKey 方式、identity 文件怎么配置?

0.用户名/密码的方式[withCredentials.usernamePassword]
    只需要在全局凭据上,然后粘贴下面的stage到pipeline中测试

// 调用上面定义好的方法
stage('Hello12345') {
    steps {
        script{
            def remote = [:]
            remote.name = '49.7.206.55'
            remote.host = '49.7.206.55'
            remote.port = 22
            remote.allowAnyHosts = true
            //通过withCredentials调用Jenkins凭据中已保存的凭据,credentialsId需要填写,其他保持默认即可
            withCredentials([usernamePassword(credentialsId: '49.7.206.55-01', passwordVariable: 'password', usernameVariable: 'userName')]) {
                remote.user = "${userName}"
                remote.password = "${password}"
            }
            sshCommand remote: remote, command: "ifconfig"
        }
    }
}

1.ssh-key-identity (No such file or directory)

使用withCredentials.sshUserPrivateKey的方式远程控制,会报"java.io.FileNotFoundException: /var/jenkins_home/workspace/kernel@tmp/secretFiles/5fb95f6b-43d0-4c00-a700-2abe2ffb8449/ssh-key-identity (No such file or directory)"错误
解决:"sshCommand remote: remote, command:'pwd'"要紧接着写在remote.xxx配置下面

2.keyFileVariable: 'identity'是什么?
withCredentials([sshUserPrivateKey(credentialsId: '49.7.206.55-02', keyFileVariable: 'identity')]) {...}
identity 是在jenkins每次远程时会拷贝私钥认证文件 'ssh-key-'+identity 到相应的路径做鉴权

3.sshUserPrivateKey方式到底怎么配?
withCredentials([sshUserPrivateKey(credentialsId: '换成自己的全局凭据ID', keyFileVariable: 'identity')]) {...}
keyFileVariable: 'identity',这个不要改
3.1 jenkins中的配置:

// 调用上面定义好的方法
stage('Hello54321') {
    steps {
        script{
            def remote = [:]
            remote.name = '49.7.206.55'
            remote.host = '49.7.206.55'
            remote.port = 22
            remote.allowAnyHosts = true
            withCredentials([sshUserPrivateKey(credentialsId: '49.7.206.55-02', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
                remote.user = userName
                remote.identityFile = identity
                sshCommand remote: remote, command:'pwd'
            }
        }
    }
}

仅此还会报"com.jcraft.jsch.JSchException: Auth fail"
3.2 jenkins远程的target机器中配置:
    目标机器公钥得单独维护一份到authorized_keys文件中,可以执行 cat id_rsa.pub >> authorized_keyscp id_rsa.pub authorized_keys,最终目录如下:

再尝试用 sshUserPrivateKey 的方式远程就没问题了

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值