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?
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
sharedVars:
- IMAGE_TAG
- IMAGE_NAME
I'll see if I can submit a feature request for this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.