Search code examples
jenkinsgroovycontinuous-integrationjenkins-pipelinejenkins-plugins

Multiple ssh remotes in one jenkins pipeline


Could I use many remotes in one jenkins pipeline using SSH Pipeline Steps plugin?

Now my pipeline looks like this:

def remote = [:]
remote.name = 'PRE-DEV'
remote.host = 'x.x.x.x'
remote.user = 'jenkins'
remote.identityFile = '/var/lib/jenkins/.ssh/id_rsa'
remote.allowAnyHosts = true
remote.agentForwarding = true


pipeline {
agent any

stages{
   stage('BUILD'){
        steps{
            sshCommand remote: remote, command: "build commands"
        }
    }

    stage('UNIT TESTS'){
        steps{
           sshCommand remote: remote, command: "tests commands"
        }
    }

    stage('DEPLOY TO DEV'){
        steps{
            sshCommand remote: remote, command: "scp artifacts push to other vm"
        }
    }
}

Now i need additional stage ('RUN ON DEV'), where i can run my artifacts on other VM. How can i do it in the same pipeline?


Solution

  • solution one:

    you can just define another dict like blow:

    def secondRemote = [:]
    secondRemote.name = 'PRE-DEV'
    secondRemote.host = 'your new host'
    secondRemote.user = 'jenkins'
    secondRemote.identityFile = '/var/lib/jenkins/.ssh/id_rsa'
    secondRemote.allowAnyHosts = true
    secondRemote.agentForwarding = true
    

    then use it by sshCommand remote: secondRemote, command: "your new command"

    solution two:

    store your private key in jenkins credentials, then using ssh-agent plugin.

    https://www.jenkins.io/doc/pipeline/steps/ssh-agent/