Jenkins SSH Connection Pipeline
To use SSH in a Jenkins pipeline:
Go to Manage Jenkins > Credentials > (Global) > Add Credentials.
Choose Kind: SSH Username with private key.
Enter:
- Username: Remote SSH user (e.g.,
root). - Private Key: Enter or upload the private key.
- ID:
ssh-server-key(used in your pipeline).
- Username: Remote SSH user (e.g.,
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"
'''
}
}
}
}
}