11

I'm trying a post failure action with a parallel step but it never works.

This is my Jenkinsfile:

pipeline {
  agent any
  stages {
    stage("test") {
      steps {
        withMaven(
          maven: 'maven3', // Maven installation declared in the Jenkins "Global Tool Configuration"
          mavenSettingsConfig: 'maven_id', // Maven settings.xml file defined with the Jenkins Config File Provider Plugin
          mavenLocalRepo: '.repository')
        {
          // Run the maven build
          sh "mvn --batch-mode release:prepare -Dmaven.deploy.skip=true" --> it will always fail
        }
      }
    }
    stage("testing") {
      steps {
        parallel (
          phase1: { sh 'echo phase1' },
          phase2: { sh "echo phase2" }
        )
      }
    }
  }
  post {
    failure {
      echo "FAIL"
    }
  }
}

But the post failure action here is a bit useles... I don´t see it any place.

Thanks to all! Regards

cdrini
  • 989
  • 10
  • 17
Daniel Majano
  • 976
  • 2
  • 10
  • 18

2 Answers2

22

I've found the issue, after several hours of searching. What you are missing (and I was missing too) is the catchError section.

pipeline {
    agent any
    stages {
        stage('Compile') {
           steps {
                catchError {
                    sh './gradlew compileJava --stacktrace'
                }
            }
            post {
                success {
                    echo 'Compile stage successful'
                }
                failure {
                    echo 'Compile stage failed'
                }
            }
        }
        /* ... other stages ... */
    }
    post {
        success {
            echo 'whole pipeline successful'
        }
        failure {
            echo 'pipeline failed, at least one step failed'
        }
    }

You should wrap every step that can potentially fail into a catchError function. What this does is:

  • If an error occurs...
  • ... set build.result to FAILURE...
  • ... and continue the build

The last point is important: your post{ } blocks did not get called because your entire pipeline was aborted before they even had a chance to execute.

Martin Häusler
  • 6,544
  • 8
  • 39
  • 66
  • The problem in this case is the parallel step. If you use a normal stage without the parallel. All the post actions works good. – Daniel Majano May 24 '17 at 15:07
  • 2
    Not for me. I have no parallelism in my build pipeline and post actions refused to fire if a shell script in one of the steps was not executed successfully. I actually had to use `catchError` to see the results of the `post{}` actions. – Martin Häusler May 24 '17 at 18:22
2

Just in case someone else also made the same stupid mistake I did, don't forget the post block needs to be inside the pipeline block.

i.e. This is apparently valid, but (obviously) won't work:

pipeline {
  agent { ... }
  stages { ... }
}
// WRONG!
post {
  always { ... }
}

This is what's correct:

pipeline {
  agent { ... }
  stages { ... }
  post {
    always { ... }
  }
}
cdrini
  • 989
  • 10
  • 17