https://codeleading.com/article/11015806881/
Remeber sshCommand remote, remote infos have identityFile
identityFile need be at insdie identity
Don't understand identity be keep. Only can be used inside identity
def getRemoteHost(ip, user) {
def remote = [:]
remote.name = ip
remote.host = ip
remote.user = user
remote.identityFile = identity
remote.port = 22
remote.allowAnyHosts = true
return remote
}
pipeline {
agent any
environment {
ssh_ip = 'ooo.xxx.ooo.xxx'
ssh_user = 'ubuntu'
ssh_jenkins_key_uid = 'oooooooo-xxxx-xxxx-xxxx-oooooooooooo'
// SSH_CREDS = credentials('oooooooo-xxxx-xxxx-xxxx-oooooooooooo')
}
stages {
stage('ssh Command'){
steps {
withCredentials([sshUserPrivateKey(credentialsId: "${ssh_jenkins_key_uid}", keyFileVariable: 'identity')]) {
sshCommand remote: getRemoteHost(ssh_ip, ssh_user), command: "whoami" //在远程服务器执行echo命令
}
}
}
}
}
def runCommand(cmd) {
def remote = [:]
remote.name = "${ssh_ip}"
remote.host = "${ssh_ip}"
remote.user = "${ssh_user}"
// remote.identityFile = identity
remote.port = 22
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(
credentialsId: "${ssh_jenkins_key_uid}",
keyFileVariable: 'identity')])
{
remote.identityFile = identity
sshCommand remote: remote, command: cmd
}
}
pipeline {
agent any
environment {
ssh_ip = 'ooo.xxx.ooo.xxx'
ssh_user = 'ubuntu'
ssh_jenkins_key_uid = 'oooooooo-xxxx-xxxx-xxxx-oooooooooooo'
// SSH_CREDS = credentials('oooooooo-xxxx-xxxx-xxxx-oooooooooooo')
}
stages {
stage('ssh Command') {
steps {
echo 'whoami start...'
runCommand('whoami')
echo 'whoami success'
}
}
}
}