Bitbucket Pipelines now supports the ability to run scripts at the end of a step execution, regardless of whether the step has failed or succeeded, using the after-script keyword. A BITBUCKET_EXIT_CODE environment variable is also exported, which contains the status code returned from the last command run in script.

An example of how you could use after scripts is if you need to spin up test environments and clean up these resources after you have finished testing. You can also integrate with other tools that you want to trigger, for example sending a notification based on the result of the step so that your developers get instant updates. This allows you to have greater control over your configuration and enables easier automation.

Here is a bitbucket-pipelines.yml that demonstrates this:

pipelines:
  branches:
    master:
      - step:
          name: Test
          script:
            - ./spin_up_test_environment.sh
          after-script:
            - ./cleanup.sh
      - step:
          name: Release
          deployment: production
          script:
            - ./release.sh
          after-script:
            - ./send_notifcation.sh $BITBUCKET_EXIT_CODE

The commands in the after-script section run until one fails, and the result of this section does not affect the overall build status. For information, check out our documentation.

We hope you enjoy this addition to Pipelines!

After scripts now available for Bitbucket Pipelines