How To: Always run clean-up commands after pipeline execution

This article describes how to insert a parallel step within a sequential pipeline to run specific commands at the end of pipeline execution. A common use case for this approach is automating resource cleanup tasks.

The parallel step always runs at the end of the pipeline, regardless of its build status, or if the pipeline was manually terminated.

How to

In a sequential pipeline, which is the default execution mode for pipelines, manually terminating the pipeline also terminates the execution of subsequent steps, including any pipeline hooks set to run on success or failure.

You can circumvent this behavior by inserting a parallel step within the pipeline.

  • Add the step below as a template to ensure code clean-up will always run:
  image: alpine
      - echo "This is a step that is always executed at the end of the build. No matter what"
          buildTerminated: workflow.result == 'terminated'
          buildFailed: workflow.result == 'failure'
          buildSuccess: workflow.result == 'success'

Hooks in pipelines
Parallel pipeline execution