Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Environment varibles shared between bitbuck pipeline steps

Edward Wilson
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 16, 2018

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

#!/bin/bash
set -e

prefix="GitVersion_"
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}");
done

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

- step:
    name: Build version
    image: mono:5.12
    script:
        - ...
        - ./scripts/gitversion.sh
        - printenv
- step:
    name: Build
    image: microsoft/aspnetcore-build:2.0-stretch
    script:
        - 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
Atlassian Team members are employees working across the company in a wide variety of roles.
May 17, 2018

Hey @edwardgwilson, 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
    script:
- while IFS='' read -r line || [[ -n "$line" ]]; do export $line; done < environment.txt
      - printenv

Hope that helps.

-Seb

Deleted user May 13, 2020

Hello,

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.

Like David Bieber likes this
2 votes
oschoenborn
Contributor
July 9, 2020

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 > shared_vars.sh
- echo export IMAGE_NAME=$IMAGE_NAME >> shared_vars.sh

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

- source shared_vars.sh

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 
sharedVars:
- IMAGE_TAG
- IMAGE_NAME
  • 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. 

oschoenborn
Contributor
July 9, 2020
Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events