栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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_keys 或 cp id_rsa.pub authorized_keys,最终目录如下:

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

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/842303.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号