How do I use SSH in a Jenkins pipeline?

后端 未结 4 1150
离开以前
离开以前 2020-12-09 18:09

I have some Jenkins jobs defined using a Jenkins Pipeline Model Definition, which builds NPM projects. I use Docker containers to build these projects (using a common image

4条回答
  •  独厮守ぢ
    2020-12-09 18:14

    I had a similar issue. I did not use label 'master', and I identified that the file transfer works across slaves, when I do it like this:

    Step 1 - create SSH keys in remote host server, include the key to authorized_keys

    Step 2 - Create credential using SSH keys in Jenkins, use the private key from the remote host

    The below step is added to the pipeline:

    stage ('Deploy') {
        steps{
            sshagent(credentials : ['use-the-id-from-credential-generated-by-jenkins']) {
                sh 'ssh -o StrictHostKeyChecking=no user@hostname.com uptime'
                sh 'ssh -v user@hostname.com'
                sh 'scp ./source/filename user@hostname.com:/remotehost/target'
            }
        }
    }
    

提交回复
热议问题