groovy 一个Jenkins管道中的多个SSH远程

jobtbby3  于 6个月前  发布在  Jenkins
关注(0)|答案(1)|浏览(86)

我可以使用SSH管道步骤插件在一个jenkins管道中使用多个遥控器吗?
现在我的管道看起来像这样:

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"
        }
    }
}

字符串
现在我需要额外的stage('RUN ON DEV'),在这里我可以在其他VM上运行我的工件。我如何在同一个管道中完成它?

jjhzyzn0

jjhzyzn01#

解决方案一:

你可以定义另一个像blow这样的dict:

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

字符串
然后通过sshCommand remote: secondRemote, command: "your new command"使用它

解决方案二:

将私钥存储在jenkins凭据中,然后使用ssh-agent插件。
https://www.jenkins.io/doc/pipeline/steps/ssh-agent/

相关问题