Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Environment varibles shared between bitbuck pipeline steps


I have a number of steps in my pipeline, the first step creates many environment variables via a script which 'exports' them.

set -e

for row in $(mono ../tools/GitVersion/GitVersion.exe | jq -c -r 'to_entries[]'); do
    key=$(echo "${row}" | jq -c -r '(.key)')
    value=$(echo "${row}" | jq -c -r '(.value)')
    export $(echo "${prefix}${key}=${value}");

Unfortunately it seems they are then not avaliable within the following steps.

- step:
    name: Build version
    image: mono:5.12
        - ...
        - ./scripts/
        - printenv
- step:
    name: Build
    image: microsoft/aspnetcore-build:2.0-stretch
        - printenv
        - ...

In 'build version' step my printenv shows all my newly created envionment variables. However the first line of my build step does not have them. I understand these are created locally to the container of the previous step and therefore they would not be carried over however I need some way to do this.

Since they are created dynamicly I cannot add them to the project environment settings which get injected into each step. I need the to be created dynamicly at build, but global to the whole pipeline.

Is it possible to create global pipeline varables?

2 answers

4 votes
SebC Atlassian Team May 17, 2018

Hey @Edward Wilson, when we execute steps, each step is its own docker container so that one image/environment does not step on another.


If you want to pass state between steps I would recommend persisting your variables to disk using something like

printenv | grep KEYS_I_CARE_ABOUT > environment.txt

and use bitbucket pipelines artifacts to share the environment between steps.


- step:
name: Build version
image: mono:5.12 script: - printenv | grep KEYS_I_CARE_ABOUT > environment.txt artifacts: - environment.txt
- step:
    name: Build
    image: microsoft/aspnetcore-build:2.0-stretch
- while IFS='' read -r line || [[ -n "$line" ]]; do export $line; done < environment.txt
      - printenv

Hope that helps.



This looks like a quick workaround, but not a serious way to set shared variables between steps.

By common variables I mean context for the step (when step's script is coming from a YAML anchor to be shared), like iMAGE_NAME, IMAGE_TAG for example ?

This is less than optimal to repeat 4, or 8 times the same definitions or to add lines to tinker with printenv to accomplish that.

Can we expect a common key for variables that could be initialised and set inside the container running each step, like we do with GitlabCI ?

Thank you.

It bugs me too that there isn't an easy way of sharing vars across steps. An approach that is a bit easier than the one involving generating a text file and parsing it in the next step is to directly save to a format that can be sourced by shell in the next step. So at the end of each step, I have some lines like this: 

# share variables with other steps:
- echo export IMAGE_TAG=$IMAGE_TAG >
- echo export IMAGE_NAME=$IMAGE_NAME >>

Then in subsequent steps I just need one line at the top of the script element, like this:

- source

This isn't too bad. Actually you would think Atlassian could automate this: 

  • have a section above pipelines where you can define variable names, eg 
  • The bitbucket pipelines, when it creates the script from the list of lines under `script`, and copies it into the container (I'm guessing that's how it does it), it appends code to export the value of each var appearing in sharedVars, just like I did; the file would a temporary "hidden" artifact.
  • Before each step, Pipelines inserts a line before the first line under script, to source that env-var-sharing artifact, and voila!

I'll see if I can submit a feature request for this. 

Like # people like this

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket

⭐ Calling all Bitbucket and DevOps experts: Special showcase opportunity ⭐

Hi, Bitbucket community! Are you a DevOps practitioner (or know one in your network)? Do you have DevOps tips, tricks, or learnings you'd like to share with the community? If so, we'd love to hea...

1,441 views 4 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you