13
votes

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?

2
I hope this question would be helpful, stackoverflow.com/q/36651432/6128602. - luka5z

2 Answers

15
votes

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

8
votes
   try {
         sh "false"
    } finally {

        stage 'finalize'
        echo "I will always run!"
    }