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

Error during always block skips execution of remaining steps

    Details

    • Epic Link:
    • Similar Issues:

      Description

      Here's a trivial example of a general problem:

      pipeline {
          agent any
          stages {
              stage ("Example") {
                  steps {
                      // Install dependencies
                      sh 'npm install'
                      echo 'Execute build that creates artifacts'
                      echo 'Execute step that fails to create the expected junit test file'
                  }
              }
          }
          post {
              always {
                  junit 'reports/**'
                  echo 'This step and everything after it won't run.'
                  archive '**'
                  echo "send email to people with final build status"
              }
          }
      }
      

      I have an always block, but I can't actually guarantee that all steps in that always block will all always attempt to run.

      In Scripted Pipeline I could wrap each of these in a try-catch, but I can't do that in Declarative.

      I can reorder these steps to put the ones that I know won't fail earlier, but what about the email step? I want that to be last, so it gets the proper build status in the email.

      Andrew Bayer suggested a catchError block. But depending on which step fails, that doesn't get the right behavior either. I want each step in the always to run exactly once no matter the build status. If the second step fails the catchError can't tell which one failed and not to run the first step again.

      Logically what I would expect to work:

      pipeline {
          agent any
          stages {
              stage ("Example") {
                  steps {
                      // Install dependencies
                      sh 'npm install'
                      echo 'Execute build that creates artifacts'
                      echo 'Execute step that fails to create the expected junit test file'
                  }
              }
          }
          post {
              always {
                  junit 'reports/**'
              }
              always {
                  echo 'This step and everything after it won't run.'
              }
              always {
                  archive '**
              }
              always {
                  echo "send email to people with final build status"
              }
          }
      }
      

      However, I'm not allowed to have more than one always block.

        Attachments

          Issue Links

            Activity

            Hide
            mb_o Moritz Baumann added a comment -

            Andrew Bayer thank you very much! While it doesn't fix the original issue, it very much fixes my use case. Especially the fact that `always` ran before `success` was giving me headaches.

            Show
            mb_o Moritz Baumann added a comment - Andrew Bayer thank you very much! While it doesn't fix the original issue, it very much fixes my use case. Especially the fact that `always` ran before `success` was giving me headaches.
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: Andrew Bayer
            Path:
            pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Cleanup.groovy
            pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Messages.properties
            pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BuildConditionResponderTest.java
            pipeline-model-definition/src/test/resources/postChecksAllConditions.groovy
            http://jenkins-ci.org/commit/pipeline-model-definition-plugin/83abd0ec35960c1f2a37b6a66b2d26385b2962e2
            Log:
            [FIXED JENKINS-41239] Add new cleanup post condition

            The new `cleanup` condition will always run regardless of build
            status, like `always`, but runs after all other `post` conditions
            have been evaluated, rather than before.

            I wanted to call this `finally` but that actually breaks Groovy
            parsing because `finally` is a reserved word. Dang. So for now, I'm
            calling it `cleanup`, but am open to suggestions.

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Cleanup.groovy pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Messages.properties pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BuildConditionResponderTest.java pipeline-model-definition/src/test/resources/postChecksAllConditions.groovy http://jenkins-ci.org/commit/pipeline-model-definition-plugin/83abd0ec35960c1f2a37b6a66b2d26385b2962e2 Log: [FIXED JENKINS-41239] Add new cleanup post condition The new `cleanup` condition will always run regardless of build status, like `always`, but runs after all other `post` conditions have been evaluated, rather than before. I wanted to call this `finally` but that actually breaks Groovy parsing because `finally` is a reserved word. Dang. So for now, I'm calling it `cleanup`, but am open to suggestions.
            Hide
            abayer Andrew Bayer added a comment -

            Merged, releasing later today in Declarative 1.2.9

            Show
            abayer Andrew Bayer added a comment - Merged, releasing later today in Declarative 1.2.9
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: Andrew Bayer
            Path:
            content/doc/book/pipeline/syntax.adoc
            http://jenkins-ci.org/commit/jenkins.io/c8c7bce2e7c5955d5176f0791800156cf0c087a2
            Log:
            JENKINS-41239 Add doc for new cleanup post condition.

            This was just released in Declarative 1.2.9 so this should be ready to
            go whenever works for you.

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: content/doc/book/pipeline/syntax.adoc http://jenkins-ci.org/commit/jenkins.io/c8c7bce2e7c5955d5176f0791800156cf0c087a2 Log: JENKINS-41239 Add doc for new cleanup post condition. This was just released in Declarative 1.2.9 so this should be ready to go whenever works for you.
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: R. Tyler Croy
            Path:
            content/doc/book/pipeline/syntax.adoc
            http://jenkins-ci.org/commit/jenkins.io/5c434e826c79dc06f837a6d244c9ddf5df1fa382
            Log:
            Merge pull request #1504 from abayer/jenkins-41239

            JENKINS-41239 Add doc for new cleanup post condition.

            Compare: https://github.com/jenkins-infra/jenkins.io/compare/92e468e7f66b...5c434e826c79

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: R. Tyler Croy Path: content/doc/book/pipeline/syntax.adoc http://jenkins-ci.org/commit/jenkins.io/5c434e826c79dc06f837a6d244c9ddf5df1fa382 Log: Merge pull request #1504 from abayer/jenkins-41239 JENKINS-41239 Add doc for new cleanup post condition. Compare: https://github.com/jenkins-infra/jenkins.io/compare/92e468e7f66b...5c434e826c79

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                bitwiseman Liam Newman
              • Votes:
                1 Vote for this issue
                Watchers:
                8 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: