在jenkins文件中执行远程主机上的shell命令



为什么连接到远程主机后,其他命令如docker rm和docker run在本地执行,而不是在远程主机上执行。如何通过远程主机使用ssh连接执行第一个命令后编写的命令?

if (params.Env == 'test') {
sshagent (credentials: ['*****-****-****-****-********']) {

sh 'ssh -o StrictHostKeyChecking=no -l root *.*.*.* uname -a'
sh "docker rm mycontainer-'${params.Env}' -f"
sh "docker run -d -p 1234:8090 --name mycontainer-'${params.Env}' 10.0.0.1:5050/myproj:'${params.Env}'-'${currentBuild.number}'"

}
}

在这种情况下应该使用凭据绑定插件为Jenkins - https://www.jenkins.io/doc/pipeline/steps/credentials-binding/

例如:

withCredentials([sshUserPrivateKey(credentialsId: 'ssh_pkey', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'jenkins')]) {

def remote=[:]
remote.name = '$params.Env'
remote.host = 'your_ip_address'
remote.user = 'root'
remote.identityFile=identity
remote.allowAnyHosts = true
sshCommand remote: remote, command: "docker rm container_name -f"
sshCommand remote: remote, command: "docker run -d -p 443:443 --restart always --name 
container_name

最新更新