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

Pipeline stage is started even when currentResult is FAILURE

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • None
    • Jenkins 2.222.1, warnings-ng: 8.1, pipeline: 2.6, pipeline-api: 2.40, pipeline-declarative: 1.6.0, workflow-job: 2.38, pipeline-stage-step: 2.38, workflow-step-api: 2.22

      There is, from my point of view, unexpected behavior: when some plugin, or some post action, sets the build to the FAILURE, the stage and also build is correctly set to FAILED with the correct color BUT the following stages are executed despite the fact that currentBuild.currentResult is set to FAILURE. I think that the next stages should not be executed when the previous has failed.

      There is existing workaround such as using when expresssion

      when { expression { return currentBuild.result == "SUCCESS" } }
      

      in stage, but I think, that it should be the default behavior.

      Example minimum Jenkinsfile:

      pipeline {
          agent { label 'executor' }
          stages {
              stage("Pylint") {
                  steps {
                      echo "During Build currentResult: ${currentBuild.currentResult}"
                      sh "pylint . > pylint_report.txt"
                  }
                  post {
                      always {
                          //   warnings-ng plugin
                          recordIssues(
                              // Fails the build if errors have been reported during the execution of this step.
                              failOnError: true,
                              // If true, then only successful or unstable builds will be considered as baseline.
                              ignoreFailedBuilds: false,
                              // If the actual number of issues is greater or equal than this threshold then a build is considered as unstable or failed, respectively.
                              qualityGates: [[threshold: 1, type: 'TOTAL', unstable: false]],
                              // Enable recording for failed builds.
                              enabledForFailure: true,
                              tools: [
                                  pyLint(
                                      id: 'pylint-linux',
                                      name: 'Pylint Linux',
                                      pattern: 'pylint_report.txt'
                                  )
                          ])
                      }
                  }
              }
              stage("Push") {
                  // when { expression { return currentBuild.result == "SUCCESS" } }
                  steps {
                      echo "During Build currentResult: ${currentBuild.currentResult}"
                      echo "Why am I executed?"
                  }
              }
          }
      }
      

      Blue Ocean View:

      When there is

        when { expression { return currentBuild.result == "SUCCESS" } }
      

      in the stage, behavior is as expected:

      The log from the job ends with:

      [Pylint Linux] All reported issues will be considered outstanding
      [Pylint Linux] Evaluating quality gates
      [Pylint Linux] -> FAILED - Total (any severity): 1 - Quality QualityGate: 1
      [Pylint Linux] -> Some quality gates have been missed: overall result is FAILED
      [Pylint Linux] Health report is disabled - skipping
      [Pylint Linux] Created analysis result for 1 issues (found 0 new issues, fixed 0 issues)
      [Pylint Linux] Attaching ResultAction with ID 'pylint-linux' to run 'DoSample #2'.
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] stage
      [Pipeline] { (Deploy)
      [Pipeline] echo
      During Build result: FAILURE
      [Pipeline] echo
      During Build currentResult: FAILURE
      [Pipeline] echo
      Why am I executed?
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline
      Finished: FAILURE
      

      Is this expected behavior?

      When I've played a little more with finding a minimal example, I was able to fail the build only when I've called error().

      pipeline {
          agent { label 'executor' }
          stages {
              stage("pre-build") {
                  steps {
                      echo "currentResult: ${currentBuild.currentResult}"
                  }
                  post {
                      always {
                          script {
                              echo "setting build to failed"
                              currentBuild.result = 'FAILURE'
                              // error('Build has failed')
                          }
                      }
                  }
              }
              stage("deploy") {
                  // when { expression { return currentBuild.currentResult == "SUCCESS" } }
                  steps {
                      echo "currentResult: ${currentBuild.currentResult}"
                      echo "Why am I executed?"
                  }
              }
          }
      }
      

      Maybe it's just warnings-ng should call error() when build should fail?

          [JENKINS-62269] Pipeline stage is started even when currentResult is FAILURE

          Ulli Hafner added a comment -

          Removing me as assignee (and warnings-ng from components) since the problem seems to be related to pipeline code (and the warnings plugin is just the trigger for the problem).

          Ulli Hafner added a comment - Removing me as assignee (and warnings-ng from components) since the problem seems to be related to pipeline code (and the warnings plugin is just the trigger for the problem).

          Similar issues: JENKINS-56536 WEBSITE-364

          Andrej Staruch added a comment - Similar issues: JENKINS-56536 WEBSITE-364

          Bogdan Katishev added a comment - - edited

          Hello, we also have this problem, our linting stage fails (which is good) but it will still proceed to the next pipeline stage(s), which we don't want. We want it to exit the pipeline when the linting stage fails.

           

          Any new updates?

          Bogdan Katishev added a comment - - edited Hello, we also have this problem, our linting stage fails (which is good) but it will still proceed to the next pipeline stage(s), which we don't want. We want it to exit the pipeline when the linting stage fails.   Any new updates?

          Your problem is out of a scope of this ticket I think.

          But what I can suggest to you is to set your linting tool to exit always with zero exit code, and then process results afterwards in post stage and do your aftermath there (e.g fail if lint had failed).

          Andrej Staruch added a comment - Your problem is out of a scope of this ticket I think. But what I can suggest to you is to set your linting tool to exit always with zero exit code, and then process results afterwards in post stage and do your aftermath there (e.g fail if lint had failed).

            Unassigned Unassigned
            astaruch Andrej Staruch
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: