Jenkins ssh uses private-key to log in

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
insert image description here

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'

            
        }


    
}


Guess you like

Origin blog.csdn.net/helenyqa/article/details/129836850