Re run Jenkins job on a different slave upon failure

时间:2018-12-03 12:53:26

标签: jenkins jenkins-plugins jenkins-pipeline

I have a job which is compatible with 2 slaves(configured on the different locations). I often experience connectivity issues due to VPN session timeout so I am trying to figure out a way to automatically run a job on the slave 2 if the job gets fail on slave 1. Please let me know if there is any plugin or any way to accomplish it.

1 个答案:

答案 0 :(得分:0)

我认为使用自由风格的项目很难实现您的要求。

管道脚本

如果您不知道此插件,请检查此内容:How create a pipeline script

根据此answer,使用管道插件,您可以使用标签来写在多个从属节点上运行的作业:

node('linux') {
  git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'
  sh "make"
  step([$class: 'ArtifactArchiver', artifacts: 'build/program', fingerprint: true])
}
node('windows && amd64') {
  git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'
  sh "mytest.exe"
}

我创建了这个简单的管道脚本并工作(此示例没有标签,但是您可以使用它):

def exitStatusInMasterNode = 'success';

node {
   echo 'Hello World in node master'
   echo 'status:'+exitStatusInMasterNode
   exitStatusInMasterNode = 'failure'
}

node {
   echo 'Hello World in node slave'
   echo 'master status:'+exitStatusInMasterNode
}

exitStatusInMasterNode 变量可以在节点之间共享。

因此,如果您的slave1发生故障,则可以将 exitStatusInMasterNode 设置为失败。在slave2的开始处,您可以验证 exitStatusInMasterNode 是否失败,以便在该Slave中执行相同的构建。

示例:

def exitStatusInMasterNode = 'none';

node {
  try{
    echo 'Hello World in Slave-1'
    throw new Exception('Simulating an error')
    exitStatusInMasterNode = 'success'    
  } catch (err) {
    echo err.message
    exitStatusInMasterNode = 'failure'
  }
}

node {
   if(exitStatusInMasterNode == 'success'){
     echo 'Job in slave 1 was success. Slave-2 will not be executed'
     currentBuild.result = 'SUCCESS'
     return;

   }

   echo 'Re launch the build in Slave-2 due to failure on Slave-1'
   // exec simple tasks or stages
}

slave1中模拟错误的日志

Running on Jenkins in .../multiple_nodes
Hello World in Slave-1
Simulating an error

Running on Jenkins in .../multiple_nodes
Re launch the build in Slave-2 due to failure on Slave-1

Finished: SUCCESS

在slave1中没有错误时记录日志(注释此行:抛出新异常)

Running on Jenkins in .../multiple_nodes
Hello World in Slave-1

Running on Jenkins in .../multiple_nodes
Job in slave 1 was success. Slave-2 will not be executed

Finished: SUCCESS