Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-71374

Pipeline post fixed block within matrix block runs even if it previously succeeded

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • pipeline
    • None
    • Jenkins 2.346.2
      Windows 10
      Java 8

      I've recently ported a whole bunch of independent Jenkinsfiles into one Jenkinsfile that uses matrix to go over a set of platforms. Below the stages block, I would use post to detect if a build was fixed or failed and notify Slack. Since porting to use a matrix pipeline, the post fixed block runs upon any success if any of the previous matrix entries failed in the previous build, leading to lots of noisy reporting. I've reproduced this with a minimal example Jenkinsfile:

      pipeline {
          agent none
          stages {
              stage('Build All Platforms') {
                  matrix {
                      axes {
                          axis {
                              name 'PLATFORM'
                              values 'A', 'B'
                          }
                      }
                      agent any
                      stages {
                          stage('Update') {
                              steps {
                                  echo 'Updating'
                              }
                          }
                          stage('Build') {
                              environment {
                                  failure = "${new Random().nextInt(2)}"
                              }
                              steps {
                                  echo "Building. Expect failure = ${failure}"
                                  bat "exit /b ${failure}"
                              }
                          }
                      }
                      post {
                          success {
                              echo 'Success'
                          }
                          fixed {
                              echo 'Fixed'
                          }
                          failure {
                              echo 'Failed'
                          }
                      }
                  }
              }
          }
      } 

      This just has 2 platforms and upon build each platform will have a 50% chance of succeeding.

      With the following build results:

      • Build #1: Platform A fails, platform B succeeds
      • Build #2: Platform A fails, platform B succeeds

      I see build #2 will claim platform B's build was "fixed" even though it succeeded in the previous build. This seems to be because platform A failed in the previous build. I would expect the post fixed block to not run at all for platform B in build #2.

      Is this a known issue? Am I just misunderstanding how to use the post block within the context of a matrix pipeline? Is there a workaround you can suggest?

      The full console output is here:

      [Pipeline] Start of Pipeline
      [Pipeline] stage
      [Pipeline] { (Build All Platforms)
      [Pipeline] parallel
      [Pipeline] { (Branch: Matrix - PLATFORM = 'A')
      [Pipeline] { (Branch: Matrix - PLATFORM = 'B')
      [Pipeline] stage
      [Pipeline] { (Matrix - PLATFORM = 'A')
      [Pipeline] stage
      [Pipeline] { (Matrix - PLATFORM = 'B')
      [Pipeline] withEnv
      [Pipeline] {
      [Pipeline] withEnv
      [Pipeline] {
      [Pipeline] node
      [Pipeline] node
      Running on Jenkins in C:\ProgramData\Jenkins\.jenkins\workspace\test-post-stages
      Running on Jenkins in C:\ProgramData\Jenkins\.jenkins\workspace\test-post-stages@2
      [Pipeline] {
      [Pipeline] {
      [Pipeline] stage
      [Pipeline] { (Update)
      [Pipeline] stage
      [Pipeline] { (Update)
      [Pipeline] echo
      Updating
      [Pipeline] }
      [Pipeline] echo
      Updating
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] // stage
      [Pipeline] stage
      [Pipeline] { (Build)
      [Pipeline] stage
      [Pipeline] { (Build)
      [Pipeline] withEnv
      [Pipeline] {
      [Pipeline] withEnv
      [Pipeline] {
      [Pipeline] echo
      Building. Expect failure = 1
      [Pipeline] bat
      [Pipeline] echo
      Building. Expect failure = 0
      [Pipeline] bat
      
      C:\ProgramData\Jenkins\.jenkins\workspace\test-post-stages>exit /b 1 
      [Pipeline] }
      
      C:\ProgramData\Jenkins\.jenkins\workspace\test-post-stages@2>exit /b 0 
      [Pipeline] // withEnv
      [Pipeline] }
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] // withEnv
      Post stage
      [Pipeline] }
      [Pipeline] // stage
      Post stage
      [Pipeline] echo
      Fixed
      [Pipeline] echo
      Failed
      [Pipeline] }
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] }
      [Pipeline] // withEnv
      [Pipeline] }
      [Pipeline] // withEnv
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] }
      Failed in branch Matrix - PLATFORM = 'A'
      [Pipeline] // stage
      [Pipeline] }
      [Pipeline] // parallel
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] End of Pipeline
      ERROR: script returned exit code 1
      Finished: FAILURE 

            Unassigned Unassigned
            nlange Nick Lange
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: