Answers: <p>According to the <a href="https://jenkins.io/doc/book/pipeline/syntax/#when" rel="nofollow noreferrer">documentation</a>, the <code>when</code> directive is only allowed inside a <code>stage</code>. You have to use the <code>script</code> directive if you want to use conditional statements:</p>
  
  <pre class="lang-java prettyprint-override"><code>post {
    always {
      script {
        if (DeleteStack == 'true') {
          // Destroy what you want !
        }
      }
    }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-08-23T05:27:41.777Z
Answers: 2019-08-23T05:27:41.777Z
Answers: Hedi Nasr
Answers: <p>I'm not aware of any way to do what you want with Declarative, but it certainly is possible with Scripted:</p>
  
  <pre><code>try {
    // your Pipeline code here
  } finally {
    if (!DeleteStack) { return }
  
    [
      CFTBatchStackName,
      CFTVPCStackName,
      CFTSelfDeleteName,
    ].each { stack-&gt;
      withAWS(region:'us-west-2', credentials:'jenkins-deploy') {
        def outputs = cfnDelete(
          stack: stack,
          pollInterval: 1000,
        )
      }
    }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2018-09-26T16:13:03.347Z
Answers: 2018-09-26T16:13:03.347Z
Answers: jayhendren
Body: <p>Although it seems counterintuitive, I would like to execute an always statement when a conditional is met. In my pipeline, my post statement should always destroy my AWS CloudFormation Stack if a variable is set to true. Below is the post statement which is not correct. </p>
  
  <pre><code>post{
      always {
          when {
              expression { DeleteStack == 'true'}
          }
          stage("Delete All Stacks") {               
              steps {
                  script {
                      withAWS(region:'us-west-2', credentials:'jenkins-deploy') {
                          def outputs = cfnDelete(
                              stack:"${CFTBatchStackName}", 
                              pollInterval:1000)
                      }
                      withAWS(region:'us-west-2', credentials:'jenkins-deploy') {
                          def outputs = cfnDelete(
                              stack:"${CFTVPCStackName}", 
                              pollInterval:1000)
                      }
                      withAWS(region:'us-west-2', credentials:'jenkins-deploy') {
                      def outputs = cfnDelete(
                          stack:"${CFTSelfDeleteName}", 
                          pollInterval:1000)
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Can I achieve this using a declarative pipeline? </p>
  
Tags: <jenkins><amazon-web-services><jenkins-pipeline><aws-cloudformation>
Title: Always execute post statement in a jenkins pipeline when a conditional statement is met
UserDisplayName: Preston Martin
ThreadText: # Always execute post statement in a jenkins pipeline when a conditional statement is met
  
  (Asked by: username_0 on 2018-09-25)
  
  Although it seems counterintuitive, I would like to execute an always statement when a conditional is met. In my pipeline, my post statement should always destroy my AWS CloudFormation Stack if a variable is set to true. Below is the post statement which is not correct.
  
  ```
  post{
      always {
          when {
              expression { DeleteStack == 'true'}
          }
          stage("Delete All Stacks") {               
              steps {
                  script {
                      withAWS(region:'us-west-2', credentials:'jenkins-deploy') {
                          def outputs = cfnDelete(
                              stack:"${CFTBatchStackName}", 
                              pollInterval:1000)
                      }
                      withAWS(region:'us-west-2', credentials:'jenkins-deploy') {
                          def outputs = cfnDelete(
                              stack:"${CFTVPCStackName}", 
                              pollInterval:1000)
                      }
                      withAWS(region:'us-west-2', credentials:'jenkins-deploy') {
                      def outputs = cfnDelete(
                          stack:"${CFTSelfDeleteName}", 
                          pollInterval:1000)
                      }
                  }
              }
          }
      }
  }
  
  ```
  
  Can I achieve this using a declarative pipeline?
  
  **Tags:** jenkins, amazon-web-services, jenkins-pipeline, aws-cloudformation
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-08-23. Score: 2
  
  According to the [documentation](https://jenkins.io/doc/book/pipeline/syntax/#when), the `when` directive is only allowed inside a `stage`. You have to use the `script` directive if you want to use conditional statements:
  
  ```java
  post {
    always {
      script {
        if (DeleteStack == 'true') {
          // Destroy what you want !
        }
      }
    }
  }
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2018-09-26. Score: 0
  
  I'm not aware of any way to do what you want with Declarative, but it certainly is possible with Scripted:
  
  ```
  try {
    // your Pipeline code here
  } finally {
    if (!DeleteStack) { return }
  
    [
      CFTBatchStackName,
      CFTVPCStackName,
      CFTSelfDeleteName,
    ].each { stack->
      withAWS(region:'us-west-2', credentials:'jenkins-deploy') {
        def outputs = cfnDelete(
          stack: stack,
          pollInterval: 1000,
        )
      }
    }
  }
  
  ```
  
  
  ---
  
  