Jenkins Pipeline Fails if Step is Unstable
Asked Answered
L

2

23

Currently my pipeline fails (red), when a maven-job is unstable (yellow).

node {
    stage 'Unit/SQL-Tests'
    parallel (
       phase1: { build 'Unit-Tests' }, // maven
       phase2: { build 'SQL-Tests' } // shell
    )
    stage 'Integration-Tests'
    build 'Integration-Tests' // maven
}

In this example the job Unit-Test's result is unstable, but is shown as failed in the pipeline.

How can I change the jobs/pipeline/jenkins to have the (1) the pipeline step unstable instead of failed and (2) the pipeline's status unstable instead of failed.

I tried adding the MAVEN_OPTS parameter -Dmaven.test.failure.ignore=true, but that did not solve the issue. I am unsure how to wrap the build 'Unit-Test' into some logic that can catch and process the result.

Adding a sub-pipeline with this logic doesn't do the trick, as there is no option to checkout from subversion (that option is available in a regular maven job). I would not like to use commandline checkout if possible.

Leatrice answered 2/8, 2016 at 7:8 Comment(0)
S
24

Whatever the step is UNSTABLE or FAILED, the final build result in your script will be FAILED.

You can add propagate to false by default to avoid fail the flow.

def result = build job: 'test', propagate: false

In the end of the flow, you can verdict the final result based on what you got from the "result" variable.

For example

currentBuild.result='UNSTABLE'

Here is a detail example How to set current build result in Pipeline

Sneer answered 2/8, 2016 at 11:30 Comment(1)
Link at end of answer is dead - "404 Not Found".Extravert
L
35

Lessons learned:

  • Jenkins will continuously update the pipeline according to the currentBuild.result value which can be either SUCCESS, UNSTABLE or FAILURE (source).
  • The result of build job: <JOBNAME> can be stored in a variable. The build status is in variable.result.
  • build job: <JOBNAME>, propagate: false will prevent the whole build from failing right away.
  • currentBuild.result can only get worse. If that value was previously FAILED and receives a new status SUCCESS through currentBuild.result = 'SUCCESS' it will stay FAILED

This is what I finally used:

    node {
        def result  // define the variable once in the beginning
        stage 'Unit/SQL-Tests'
        parallel (
           phase1: { result = build job: 'Unit', propagate: false }, // might be UNSTABLE
           phase2: { build 'SQL-Tests' }
        )
        currentBuild.result = result.result  // update the build status. jenkins will update the pipeline's current status accordingly
        stage 'Install SQL'
        build 'InstallSQL'
        stage 'Deploy/Integration-Tests'
        parallel (
           phase1: { build 'Deploy' },
           phase2: { result = build job: 'Integration-Tests', propagate: false }
        )
        currentBuild.result = result.result // should the Unit-Test be FAILED and Integration-Test SUCCESS, then the currentBuild.result will stay FAILED (it can only get worse)
        stage 'Code Analysis'
        build 'Analysis'
    }
Leatrice answered 3/8, 2016 at 6:10 Comment(4)
And you can't set a SUCCESS result, if it was set to FAILED already (as discussed here.Diandre
Thanks for the info. Added that to the answer.Leatrice
I am having the same problem. Still I do not understand the reason. Why Unstable is considered as FAILED, if the Stage View plugin supports unstable (yellow)? why do you need to set currentBuild.result all the time and not just take the worst of all?Barbican
Note that the result value is FAILURE rather than FAILED github.com/jenkinsci/jenkins/blob/…Continual
S
24

Whatever the step is UNSTABLE or FAILED, the final build result in your script will be FAILED.

You can add propagate to false by default to avoid fail the flow.

def result = build job: 'test', propagate: false

In the end of the flow, you can verdict the final result based on what you got from the "result" variable.

For example

currentBuild.result='UNSTABLE'

Here is a detail example How to set current build result in Pipeline

Sneer answered 2/8, 2016 at 11:30 Comment(1)
Link at end of answer is dead - "404 Not Found".Extravert

© 2022 - 2024 — McMap. All rights reserved.