Skip to content

Jenkins SSH Connection Pipeline

To use SSH in a Jenkins pipeline:

  1. Go to Manage Jenkins > Credentials > (Global) > Add Credentials.

  2. Choose Kind: SSH Username with private key.

  3. Enter:

    • Username: Remote SSH user (e.g., root).
    • Private Key: Enter or upload the private key.
    • ID: ssh-server-key (used in your pipeline).
  4. Click OK.

Your pipeline can now reference the SSH key with sshagent(['ssh-server-key']).

groovy
pipeline {
    agent any

    stages {
        stage('SSH LS') {
            steps {
                sshagent(['ssh-server-key']) {
                    sh '''
                        ssh -o StrictHostKeyChecking=no root@147.93.62.212 "ls -la"
                    '''
                }
            }
        }
    }
}