失败后在另一个从属服务器上重新运行Jenkins作业



我有一个与2个从机兼容的作业(配置在不同的位置(。由于VPN会话超时,我经常遇到连接问题,所以我正试图找到一种方法,如果从设备1上的作业失败,可以在从设备2上自动运行该作业。请让我知道是否有任何插件或任何方式来完成它。

我认为对于一个自由风格的项目,很难实现您的需求。

管道脚本

如果你不知道这个插件,请检查这个:如何创建管道脚本

根据这个答案,管道插件允许您使用标签:编写在多个从属节点上运行的作业

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
}

从设备1 中模拟错误的日志

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

最新更新