• Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • pipeline
    • None
    • Jenkins ver. 2.70

      I'm having a lot of trouble properly reporting build status at the end of my declarative pipeline. I've witnessed failed builds report success in Slack and GitLab, both separately and together. For example, here's a failed build, yet it shows success in Slack for the same build.

      And another example where the build failed and, yet, it shows success in GitLab.

      I'm using a simple map of Jenkins status to Slack color / GitLab status. This helps keep my post block small & clean, since the only thing that varies is this result.

      Jenkinsfile (partial)
      def COLOR_MAP = ['SUCCESS': 'good', 'FAILURE': 'danger', 'UNSTABLE': 'danger', 'ABORTED', 'danger']
      def STATUS_MAP = ['SUCCESS': 'success', 'FAILURE': 'failed', 'UNSTABLE': 'failed', 'ABORTED', 'failed']
      
      ...
      
      post { 
        always { 
          slackSend channel: '#build', color: COLOR_MAP[currentBuild.currentResult], message: 'Whatever.'
          updateGitlabCommitStatus name: 'build', state: STATUS_MAP[currentBuild.currentResult]
        }
      }
      

      It's not obvious which should be used and there's a lack of documentation (short of reading the source code).

      Am I using the wrong model property or am I using it incorrectly? Clearly, Jenkins has one result and, yet, it's sending a different result to the plugins. I need a reliable result. I thought I could depend on the result in the post block, is that not true?

          [JENKINS-46325] Should we use result or currentResult?

          Liam Newman added a comment - - edited

          To answer the question, you should use currentBuild.currentResult for this case.
          There are also currentbuild.resultIsBettterOrEqualTo/currentbuild.resultIsWorseOrEqualTo methods.

          As to the error you've reported, I'm not able to reproduce it locally. When I run the following Pipeline:

          pipeline {
              agent any
              stages {
                  stage ('Init') {
                      steps {
                          echo "Init result: ${currentBuild.result}"
                          echo "Init currentResult: ${currentBuild.currentResult}"
                      }
                      post {
                          always {
                              echo "Post-Init result: ${currentBuild.result}"
                              echo "Post-Init currentResult: ${currentBuild.currentResult}"
                          }
                      }
                  }
                  stage ('Build') {
                      steps {
                          echo "During Build result: ${currentBuild.result}"
                          echo "During Build currentResult: ${currentBuild.currentResult}"
                          sh 'exit 1'
                      }
                      post {
                          always {
                              echo "Post-Build result: ${currentBuild.result}"
                              echo "Post-Build currentResult: ${currentBuild.currentResult}"
                          }
                      }
                  }
              }
              post {
                  always {
                      echo "Pipeline result: ${currentBuild.result}"
                      echo "Pipeline currentResult: ${currentBuild.currentResult}"
                  }
              }
          }
          

          I get this output (trimmed for clarity):

          Init result: null
          Init currentResult: SUCCESS
          Post-Init result: null
          Post-Init currentResult: SUCCESS
          During Build result: null
          During Build currentResult: SUCCESS
          [test-pipeline] Running shell script
          + exit 1
          Post-Build result: FAILURE
          Post-Build currentResult: FAILURE
          Pipeline result: FAILURE
          Pipeline currentResult: FAILURE
          ERROR: script returned exit code 1
          Finished: FAILURE
          

          Please upgrade to Pipeline: Model Definition 1.1.9 and if that fixes for you.

          Liam Newman added a comment - - edited To answer the question, you should use currentBuild.currentResult for this case. There are also currentbuild.resultIsBettterOrEqualTo / currentbuild.resultIsWorseOrEqualTo methods. As to the error you've reported, I'm not able to reproduce it locally. When I run the following Pipeline: pipeline { agent any stages { stage ( 'Init' ) { steps { echo "Init result: ${currentBuild.result}" echo "Init currentResult: ${currentBuild.currentResult}" } post { always { echo "Post-Init result: ${currentBuild.result}" echo "Post-Init currentResult: ${currentBuild.currentResult}" } } } stage ( 'Build' ) { steps { echo "During Build result: ${currentBuild.result}" echo "During Build currentResult: ${currentBuild.currentResult}" sh 'exit 1' } post { always { echo "Post-Build result: ${currentBuild.result}" echo "Post-Build currentResult: ${currentBuild.currentResult}" } } } } post { always { echo "Pipeline result: ${currentBuild.result}" echo "Pipeline currentResult: ${currentBuild.currentResult}" } } } I get this output (trimmed for clarity): Init result: null Init currentResult: SUCCESS Post-Init result: null Post-Init currentResult: SUCCESS During Build result: null During Build currentResult: SUCCESS [test-pipeline] Running shell script + exit 1 Post-Build result: FAILURE Post-Build currentResult: FAILURE Pipeline result: FAILURE Pipeline currentResult: FAILURE ERROR: script returned exit code 1 Finished: FAILURE Please upgrade to Pipeline: Model Definition 1.1.9 and if that fixes for you.

          Andrew Bayer added a comment -

          So what could be happening here is that the result isn't actually set yet at the time that you're sending the message. I'd have to see the full Jenkinsfile and console log to be sure.

          Andrew Bayer added a comment - So what could be happening here is that the result isn't actually set yet at the time that you're sending the message. I'd have to see the full Jenkinsfile and console log to be sure.

          I do not get the same result. currentBuild.result is not set in the final post always section

           

          pipeline {
              agent { node { label 'kubernetes-tools-forseti-big' } }
              parameters {
                  string(name: 'QUICK_DEPLOYMENT_VERSION', defaultValue: '', description: '''
          Existing version to deploy. Using this will skip build and test stages. Please make sure this version was already built 
          and passed test previously. It will not skip build and test if you leave the QUICK_DEPLOYMENT_VERSION empty.'''
                  )
              }
          
              options {
                  buildDiscarder(logRotator(numToKeepStr: '10', artifactNumToKeepStr: '10'))
                  timeout(time: 2, unit: 'HOURS')
                  skipStagesAfterUnstable()
                  timestamps()
              }
              stages {
                  stage('Setup') {
                      steps {
                          step([$class: 'StashNotifier'])
                          script { version = setupVersion() }
                      }
                  }
                  stage('Build & test') {
                      when { environment name: 'QUICK_DEPLOYMENT_VERSION', value: '' }
                      steps { buildAndTest() }
                  }
                  stage('Integration test') {
                      when { environment name: 'QUICK_DEPLOYMENT_VERSION', value: '' }
                      failFast true
                      parallel {
                          stage('Test MD') {
                              agent { label 'kubernetes-tools-forseti-big' }
                              steps { integrationTest('Md') }
                          }
                          stage('Test EU') {
                              agent { label 'kubernetes-tools-forseti-big' }
                              steps { integrationTest('Eu') }
                          }
                          stage('Test US') {
                              agent { label 'kubernetes-tools-forseti-big' }
                              steps { integrationTest('Us') }
                          }
                      }
                  }
                  stage('Deploy') {
                      when { branch 'master' }
                      steps {
                          script {
                              printDisclaimerIfNeeded()
                              deployAll()
                          }
                      }
                  }
              }
              post {
                  always {
                      script {
                          // workaround. Seems to be a new bug
                          currentBuild.result = currentBuild.currentResult
                      }
                      unstash 'eu_test_result'
                      unstash 'us_test_result'
                      unstash 'md_test_result'
                      step([$class: 'CucumberReportPublisher', jsonReportDirectory: './', fileIncludePattern: '**/system_test_result_*.json'])
                      step([$class: 'StashNotifier'])
                  }
              }
          

          Unless I explicitly set currentBuild.result from currentBuild.currentResult it is null.
          (I have another problem too - if I place the cucumber report publisher after the parallel steps where I want it won't run, but that's another ticket I guess)

          Markus Torstensson added a comment - I do not get the same result. currentBuild.result is not set in the final post always section   pipeline { agent { node { label 'kubernetes-tools-forseti-big' } } parameters { string(name: 'QUICK_DEPLOYMENT_VERSION' , defaultValue: '', description: ' '' Existing version to deploy. Using this will skip build and test stages. Please make sure this version was already built and passed test previously. It will not skip build and test if you leave the QUICK_DEPLOYMENT_VERSION empty.''' ) } options { buildDiscarder(logRotator(numToKeepStr: '10' , artifactNumToKeepStr: '10' )) timeout(time: 2, unit: 'HOURS' ) skipStagesAfterUnstable() timestamps() } stages { stage( 'Setup' ) { steps { step([$class: 'StashNotifier' ]) script { version = setupVersion() } } } stage( 'Build & test' ) { when { environment name: 'QUICK_DEPLOYMENT_VERSION' , value: '' } steps { buildAndTest() } } stage( 'Integration test' ) { when { environment name: 'QUICK_DEPLOYMENT_VERSION' , value: '' } failFast true parallel { stage( 'Test MD' ) { agent { label 'kubernetes-tools-forseti-big' } steps { integrationTest( 'Md' ) } } stage( 'Test EU' ) { agent { label 'kubernetes-tools-forseti-big' } steps { integrationTest( 'Eu' ) } } stage( 'Test US' ) { agent { label 'kubernetes-tools-forseti-big' } steps { integrationTest( 'Us' ) } } } } stage( 'Deploy' ) { when { branch 'master' } steps { script { printDisclaimerIfNeeded() deployAll() } } } } post { always { script { // workaround. Seems to be a new bug currentBuild.result = currentBuild.currentResult } unstash 'eu_test_result' unstash 'us_test_result' unstash 'md_test_result' step([$class: 'CucumberReportPublisher' , jsonReportDirectory: './' , fileIncludePattern: '**/system_test_result_*.json' ]) step([$class: 'StashNotifier' ]) } } Unless I explicitly set currentBuild.result from currentBuild.currentResult it is null. (I have another problem too - if I place the cucumber report publisher after the parallel steps where I want it won't run, but that's another ticket I guess)

          According to the documentation if you are using Scripted Pipeline syntax you probably need to explicitly set the currentBuild.result so that you and Jenkins can track it between stages. https://support.cloudbees.com/hc/en-us/articles/218554077-How-to-set-current-build-result-in-Pipeline-

          Ethan Spoelstra added a comment - According to the documentation if you are using Scripted Pipeline syntax you probably need to explicitly set the currentBuild.result so that you and Jenkins can track it between stages.  https://support.cloudbees.com/hc/en-us/articles/218554077-How-to-set-current-build-result-in-Pipeline-

            Unassigned Unassigned
            anthonymastrean Anthony Mastrean
            Votes:
            10 Vote for this issue
            Watchers:
            16 Start watching this issue

              Created:
              Updated: