Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
Level
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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 Edited

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

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

Hope that helps.

-Seb

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.

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. 

Like # people like this

Suggest an answer

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

Calling any interview participants for Bitbucket Data Center

Hi everyone,  We are looking to learn more about development teams’ workflows and pain points, especially around DevOps, integrations, administration, scale, security, and the related challeng...

505 views 6 4
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