Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I assure that a Jenkins pipeline stage is always executed, even if a previous one failed?

I am looking for a Jenkinsfile example of having a step that is always executed, even if a previous step failed.

I want to assure that I archive some builds results in case of failure and I need to be able to have an always-running step at the end.

How can I achieve this?

like image 972
sorin Avatar asked May 26 '16 14:05

sorin


People also ask

How do you skip failed stage in Jenkins pipeline?

To ignore a failed step in declarative pipeline you basically have two options: Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)

What defines when and how a pipeline should be executed in Jenkins?

Jenkins Pipeline can be defined by a text file called JenkinsFile. You can implement pipeline as code using JenkinsFile, and this can be defined by using a DSL (Domain Specific Language). With the help of JenkinsFile, you can write the steps required for running a Jenkins Pipeline.

How does Jenkins handle error in pipeline?

If you want to abort your program on exception, you can use pipeline step error to stop the pipeline execution with an error. Example : try { // Some pipeline code } catch(Exception e) { // Do something with the exception error "Program failed, please read logs..." }

How do you make Jenkins pipeline fail?

You can use the error step from the pipeline DSL to fail the current build. error("Build failed because of this and that..")


1 Answers

We switched to using Jenkinsfile Declarative Pipelines, which lets us do things like this:

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh './gradlew check'
            }
        }
    }
    post {
        always {
            junit 'build/reports/**/*.xml'
        }
    }
}

References:

Tests and Artifacts

Jenkins Pipeline Syntax

like image 160
m0j0hn Avatar answered Sep 21 '22 02:09

m0j0hn