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 的方式远程就没问题了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)