强制Jenkins每次都在标签的不同节点上运行构建?

时间:2017-04-21 18:44:04

标签: jenkins jenkins-plugins

我有一个项目设置,每次构建时都会在MISC标签上运行,并且它一直运行良好。

但是,我遇到了一个问题,如果一台机器上的上一次构建失败,它也会导致该机器上的进一步构建失败。对另一个奴隶来说没问题。

如果可能的话,我们希望作业在标签中的不同节点上运行,以防将来再次发生。

谢谢,

1 个答案:

答案 0 :(得分:0)

我遇到过类似的问题。我的解决方案是在发生某些类型的错误时使节点脱机。

我使用此插件在每次构建后运行一个groovy脚本ttps://wiki.jenkins-ci.org/display/JENKINS/Global+Post+Script+Plugin

我的脚本看起来像这样

import jenkins.model.Jenkins
import hudson.model.*
import hudson.slaves.OfflineCause

// this script is designed to be called by https://wiki.jenkins-ci.org/display/JENKINS/Global+Post+Script+Plugin

if (BUILD_RESULT == "FAILURE") {
  println("The job failed. The build failure cause will be checked.")
  job = Jenkins.instance.getItemByFullName(JOB_NAME)
  build = job.getBuildByNumber(BUILD_NUMBER.toInteger())
  def buildLog = build.log

  if (buildLog.contains("something indicating an unrecoverable error")) {
      Node buildNode = build.getBuiltOn();

      // Never set master offline
      if (Hudson.getInstance() != buildNode) {
        println("This is fatal. The node ${NODE_NAME} is being taken offline.")
        buildNode.toComputer().setTemporarilyOffline(true, OfflineCause.create(new OfflineMessage()));
      } else {
        println("The error is marked to take the node offline, but the node is not being taken offline because it is the master")
      }
  }
}

class OfflineMessage extends org.jvnet.localizer.Localizable {
  def message
  OfflineMessage() {
    super(null, null, [])
    def timestr = new Date().format("HH:mm dd/MM/yy z", TimeZone.getDefault())
    this.message = "This node was taken offline because of a failed job at " + timestr
  }
  String toString() {
    this.message
  }
  String toString(java.util.Locale l) {
    toString()
  }
}