1

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?

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,
      )
    }
  }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.