继续 Jenkins 输油管道的失败阶段

我有一系列的阶段,执行快速检查。即使有失败,我也要把它们全部执行。例如:

stage('one') {
node {
sh 'exit 0'
}
}
stage('two') {
node {
sh 'exit 1'   // failure
}
}
stage('three') {
node {
sh 'exit 0'
}
}

阶段 two失败,因此默认情况下不执行阶段 three

通常这是 parallel的工作,但是我想在舞台视图中显示它们。在下面的模拟中:

  • 构建 # 4显示了通常发生的情况。作业 two失败,所以 three不能运行。
  • 我 PS 了 Build # 6来展示我想看到的。作业 two失败并以这种方式显示,但是 three仍然运行。真正的詹金斯可能会显示整个建造 # 6略带红色,这当然是罚款。

Mock up of desired Stage View result

120288 次浏览

This should work. However all boxes are red if even only one fails, but you can see boxes with error marked, so you will easily distinguish failed jobs.

def indexes = ['one', 'two', 'three']


node() {
for (index in indexes) {
catchError {
stage(index) {
println index
sh '''echo "123"'''
}
}
}
}

I had the same concern. I was able to resolve it doing this.

Second stage will show in red and be marked as failed while the rest of the stages will keep running. You can set a flag and at the end of the stages check the flag and inform the status of the whole build.

node {


def build_ok = true


stage('one') {
sh 'exit 0'
}


try{
stage('two') {
sh 'exit 1'   // failure
}
} catch(e) {
build_ok = false
echo e.toString()
}


stage('three') {
sh 'exit 0'
}


....


if(build_ok) {
currentBuild.result = "SUCCESS"
} else {
currentBuild.result = "FAILURE"
}
}

I resolved that, using post actions: https://jenkins.io/doc/pipeline/tour/post/

           post {
always {
...
}
}

It depends whether you are using declarative pipeline syntax or scripted pipeline syntax.

declarative pipeline syntax:

pipeline {
agent any
stages {
stage('one') {
steps {
sh 'exit 0'
}
}
stage('two') {
steps {
sh 'exit 1'   // failure
}
}
}
post {
always {
sh 'exit 0'
}
}
}

Post-condition blocks contain steps the same as the steps section.

scripted pipeline syntax:

node {


def build_ok = true


stage('one') {
sh 'exit 0'
}


try{
stage('two') {
sh 'exit 1'   // failure
}
} catch(e) {
build_ok = false
echo e.toString()
}


stage('three') {
sh 'exit 0'
}


if(build_ok) {
currentBuild.result = "SUCCESS"
} else {
currentBuild.result = "FAILURE"
}
}

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'
}
}
}
}

In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed:

Pipeline Example

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.

Just make sure your Jenkins is up to date, since this is a fairly new feature.

EDIT: You need "Pipeline: Basic Steps" 2.16 (May 14, 2019)

Solution: In order to always continue failed steps in your jenkins pipeline:

Option 1. wrap you function in try/catch or in bash script <someOpertation> || true

try/catch:

script {
try {
sh 'do your stuff'
} catch (Exception e) {
sh 'Handle the exception!'
}
}

bash always true:

script {
sh 'cp ~/someFile.txt ~/dev || true'
}

Option 2. run your jenkins pipeline in parallel and set failFast false configuration property in your step.

    pipeline {
agent any
stages {
stage('Parallel Stage') {
when {
branch 'master'
}
failFast false
parallel {
stage('Branch A') {
agent {
label "for-branch-a"
}
steps {
echo "On Branch A"
}
}
stage('Branch B') {
agent {
label "for-branch-b"
}
steps {
echo "On Branch B"
}
}
}
}
}
}

Try this example:

stage('StageName1')
{
steps
{
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE')
{
SomeCodeThatCanBeErrored
}
}
}
stage('StageName2')
{
steps
{
ContinueOtherCode
}
}

My simple suggestion, make use of shell scripts beauty like, $ cmd1 || cmd2: This will run cmd1, and in case of failure it will run cmd2. In your case cmd2 is simple "echo "cmd1 failed, proceeding next step" Thats all. It works like a charm :-)