Share environment variables between steps in Bitbucket Cloud Pipelines

Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.

Summary

  • The purpose of this how-to article is to explain why environment variables are not available between build steps by default and to provide a solution to share environment variables between your build steps by making use of artifacts in your YML configuration.

  • An example use case for this would be that an environment variable has its value generated dynamically in StepA, then StepB performs some actions as a result of the environment variable generated in StepA.

Environment

  • Bitbucket Cloud Pipelines

Solution

Each build step that is executed spins up its own individual and isolated build container, for this reason - environment variables generated within a build step are not visible to other build steps.

To share your environment variables between build steps, you can reference the worked example below:

  • Create a file to store the variable (for example: a text document), then echo the value of the variable into that file and declare it as an artifact:

    1 2 3 4 5 6 7 8 9 pipelines: default: - step: script: - my_variable=123 - touch bitbucket_env.txt - echo $my_variable >> ./bitbucket_env.txt artifacts: - bitbucket_env.txt
  • Reference the artifact file in a subsequent build step so that its value can be extracted and assigned to an environment variable for usage by that subsequent step:

    1 2 3 4 5 6 7 pipelines: default: // previous steps - step: script: - my_variable=$(cat ./bitbucket_env.txt) - echo $my_variable

If the above solution is appropriate for your use case, we have an existing feature request ticket logged with our developers to include the ability for environment variables to be natively accessible by your other build steps. You can Watch this for future updates and Vote for it to improve its visibility with regard to customer demand - there is another workaround example included on this ticket as well:

BCLOUD-20294

If you are encountering issues when following the steps contained within this troubleshooting article, please feel free to raise a support ticket or raise a community support ticket for further assistance.

Updated on February 28, 2025

Still need help?

The Atlassian Community is here for you.