继续詹金斯管道过去失败的阶段
我有一系列执行快速检查的阶段.即使有失败,我也想全部执行.例如:
I have a series of stages that perform quick checks. I want to perform them all, even if there are failures. For example:
stage('one') {
node {
sh 'exit 0'
}
}
stage('two') {
node {
sh 'exit 1' // failure
}
}
stage('three') {
node {
sh 'exit 0'
}
}
阶段 two
失败,因此默认情况下不执行阶段 three
.
Stage two
fails, so by default stage three
is not executed.
通常这将是 parallel
的工作,但我想在舞台视图中显示它们.在下面的模型中:
Ordinarily this would be a job for parallel
, but I want to display them in the stage view. In the mock up below:
- Build #4 显示了通常发生的情况.作业
two
失败,所以three
不会运行. - 我对 Build #6 进行了 Photoshop 处理,以展示我想看到的内容.作业
two
失败并显示为这样,但three
仍在运行.真正的 Jenkins 可能会显示整个 Build #6 略带红色,这当然没问题.
- Build #4 shows what normally happens. Job
two
fails sothree
does not run. - I Photoshopped Build #6 to show what I would like to see. Job
two
fails and is displayed as such, butthree
still runs. The real Jenkins would probably display the entire Build #6 tinged slightly red, which is of course fine.
现在可以了.下面是声明式管道的示例,但 catchError
也适用于脚本式管道.
This is now possible. Below is an example of a declarative pipeline, but catchError
works for scripted pipelines as well.
pipeline {
agent any
stages {
stage('1') {
steps {
sh 'exit 0'
}
}
stage('2') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh "exit 1"
}
}
}
stage('3') {
steps {
sh 'exit 0'
}
}
}
}
在上面的例子中,所有阶段都将执行,管道将成功,但阶段 2 将显示为失败:
In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed:
正如您可能已经猜到的,您可以自由选择 buildResult
和 stageResult
,以防您希望它不稳定或其他任何东西.您甚至可以使构建失败并继续执行管道.
As you might have guessed, you can freely choose the buildResult
and stageResult
, in case you want it to be unstable or anything else. You can even fail the build and continue the execution of the pipeline.
只需确保您的 Jenkins 是最新的,因为这是一个相当新的功能.
Just make sure your Jenkins is up to date, since this is a fairly new feature.