1. Generate the secret key (it must be generated in this way, otherwise the login will fail)
ssh-keygen -m PEM -t rsa -b 4096
2. Execute the command after the key is generated
cat id_rsa.pub >> authorized_keys
Configure Credentials
cat id_rsa copied to the private key
jenkins test configuration credentials
pipeline {
agent any
stages {
stage('Build') {
steps {
test()
}
}
}
}
def test(){
// private-key
def remote = [:]
remote.name = "node-1"
remote.host = "192.168.1.200"
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId: 'private-key', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName
remote.identityFile = identity
# sshCommand 必须写在withCredentials中
sshCommand remote: remote, command:'ls -all'
}
}