简体   繁体   English

当节点离线时让 Jenkins 快速失败

[英]Have Jenkins Fail Fast When Node Is Offline

I have a MultiJob Project (made with the Jenkins Multijob plugin), with a series of MultiJob Phases.我有一个 MultiJob 项目(使用 Jenkins Multijob 插件制作),有一系列 MultiJob 阶段。 Let's say one of these jobs is called SubJob01.假设这些作业之一称为 SubJob01。 The jobs that are built are each configured with the "Restrict where this project can be run" option to be tied to one node.构建的每个作业都配置了“限制此项目可以运行的位置”选项以绑定到一个节点。 SubJob01 is tied to Slave01. SubJob01 绑定到 Slave01。

I would like it if these jobs would fail fast when the node is offline, instead of saying "(pending—slave01 is offline)".我希望这些作业在节点离线时快速失败,而不是说“(待定 - slave01 离线)”。 Specifically, I want there to be a record of the build attempt in SubJob01, with the build being marked as failed.具体来说,我希望在 SubJob01 中有构建尝试的记录,构建被标记为失败。 This way, I can configure my MultiJob project to handle the situation as I'd like, instead of using the Jenkins build timeout plugin to abort the whole thing.这样,我可以配置我的 MultiJob 项目来处理我想要的情况,而不是使用 Jenkins 构建超时插件来中止整个过程。

Does anyone know of a way to fail-fast a build if all nodes are offline?如果所有节点都脱机,有没有人知道一种快速失败的构建方法? I could intersperse the MultiJob project with system Groovy scripts to check whether the desired nodes are offline, but that seems like it'd be reinventing, in the wrong place, what should already be a feature.我可以将 MultiJob 项目与系统 Groovy 脚本穿插在一起,以检查所需的节点是否处于脱机状态,但这似乎是在错误的地方重新发明应该已经是一个功能的功能。

I ended up creating this solution which has worked well.我最终创建了这个运行良好的解决方案。 The first build step of SubJob01 is an Execute system Groovy script, and this is the script: SubJob01 的第一个构建步骤是执行系统 ​​Groovy 脚本,这是脚本:

import java.util.regex.Matcher
import java.util.regex.Pattern

int exitcode = 0
println("Looking for Offline Slaves:");
for (slave in hudson.model.Hudson.instance.slaves) {
 if (slave.getComputer().isOffline().toString() == "true"){
 println('  * Slave ' + slave.name + " is offline!");
   if (slave.name == "Slave01") {
     println('    !!!! This is Slave01 !!!!');
     exitcode++;
   } // if slave.name
  } // if slave offline
} // for slave in slaves

println("\n\n");
println "Slave01 is offline: " + hudson.model.Hudson.instance.getNode("Slave01").getComputer().isOffline().toString();
println("\n\n");

if (exitcode > 0){
 println("The Slave01 slave is offline - we can not possibly continue....");
 println("Please contact IT to resolve the slave down issue before retrying the build.");
 return 1;
} // if

println("\n\n");

The jenkins pipeline statement 'beforeAgent true' can be used in evaluating the when condition previous to entering the agent. jenkins 管道语句“beforeAgent true”可用于评估进入代理之前的 when 条件。

    stage('Windows') {
      when {
        beforeAgent true
        expression { return ("${TARGET_NODES}".contains("windows")) }
      }
      agent { label 'win10' }
      steps {
        cleanWs()
        ...
      }

Ref:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM