Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use "propagate=false" in a Jenkinsfile with declarative syntax directly for stage/step?

You can use propagate on a build job as described here:

https://jenkins.io/doc/pipeline/steps/pipeline-build-step/

So you can use something like this to prevent a failing step from failing the complete build:

build(job: 'example-job', propagate: false)

Is there a way to use this for a stage or a step? I know i can surround it with a try/catch and that does works almost as i want. It does ignore the failing the stage and resumes the rest of the build, but it does not display the stage as failed. For now i write all failing stages to a variable and output that on a later stage, but this is not ideal.

If i cant suppress propagation in a stage/step, is there maybe a way to use the build() call to do the same? Maybe if i move it to another pipeline and call that via build()?

Any help appreciated.

like image 282
MassiveHiggsField Avatar asked Apr 17 '19 08:04

MassiveHiggsField


People also ask

Is JenkinsFile declarative?

A Jenkinsfile can be written using two types of syntax - Declarative and Scripted.

Can I mix declarative and scripted pipeline?

Yes you can only if you want to have external function inside step block.

What is Jenkins pipeline declarative versus scripted pipeline syntax?

Scripted vs declarative pipelines in Jenkins – differences One uses a declarative programming model and the second uses an imperative programming mode. Declarative pipelines break down stages into multiple steps, while in scripted pipelines there is no need for this.

What is an agent specified while writing the JenkinsFile declarative pipeline?

agent. The agent directive specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent directive is placed. The directive must be defined at the top-level inside the pipeline block, but stage-level usage is optional.


Video Answer


1 Answers

With catchError you can prevent a failing step from failing the complete build:

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.

like image 183
Erik B Avatar answered Oct 03 '22 17:10

Erik B