Jenkinsfile 下一阶段失败

Jenksinfile fail next stage

如果前一个失败,我希望下一阶段能够失败,但后一个应该是 运行。

真的不能给出任何代码,所以我希望我能从你们那里得到一些指导,我应该如何实现这个目标。

但例如

Stages{ 
   Stage{
      Stage that will fail
   }
   Stage{
      Stage that should fail if previous fail
   }
   Stage{
      Stage that should fail if previous fail
   }
   Stage{
      Stage that should run eitherway
   }
}

这可能不是唯一的方法,但它是一种可行的方法。通过设置一些环境变量开关,然后在后续条件阶段开始时在 when 块中对其进行评估。这里的后续阶段不会“失败”,它们只会因条件而被跳过。最后一个阶段没有 when 块,因此无论如何都会执行。

// declarative

environment {
  FAIL = false
}

Stages{ 
  Stage('Stage that might fail') {
    steps {
      script {
        try {
          sh 'whatever happens that may cause this stage to fail'
        } catch (err) {
          echo err.getMessage()
          env.FAIL = true // Sets the variable to true which will be evaluated in when block on subsequent stages 
        }
      }
    }
  }
  
  Stage('Stage that should fail if previous fail') {
    when {
      expression {
        return env.FAIL != "true" // note that even though FAIL was set as a boolean value the var is stored as a "string"
      }
    }
    steps {
      // do something
    }
  }
  
  Stage('Stage that should fail if previous fail') {
    when {
      expression {
        return env.FAIL != "true"
      }
    }
    steps {
      // do something
    }
  }
  
  Stage('Stage that should run either way') {
    // no when block = execute either way
    // do stuff
  }
}