Search code examples
jenkinsssh-agent

jenkins how to use ssh-agent in docker


My jenkins is run in docker, I write a demo to remote my server with ssh-agent.

Here is my pipeline

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                sshagent (credentials: ['hehu']) {
                    sh 'ssh -o StrictHostKeyChecking=no -l yunwei xxx.xxx.xx.25 -a'
                    sh 'pwd'
                    sh 'whoami'
                }
            }
        }
    }
}

Output enter image description here

It looks like pwd and whoami command still run in jenkins docker not my server. I have no idea how to use this plugin, I can't find any usage from ssh-agent document.


Solution

  • You should use:

    sh 'ssh -o StrictHostKeyChecking=no -l yunwei x.x.x.x pwd && whoami && cmd...'