Export environment variable in bash for use in python

TylerAvocat February 18, 2019

Hi everyone,

In Pipeline, I have a step which runs integration tests. The config use to be in a file but I am migrating to environment variable, here is my step:

- step:
name: Integration Tests on Dev
caches:
- pip
script:
- bash Scripts/set_env_vars_locally.sh -e dev && cd IntegrationTests && python -m unittest discover

 The set_env_vars_locally.sh script ends by a printenv and I can see the right values. However, when the python command runs, it sees the default value, not the one it has been updated with.

I tried doing that locally on ubuntu running a command like "export MY_VAR=NEW_VALUE && python3 show_vars.py" and it properly outputs NEW_VALUE.

I would be glad to have any help on understanding why the env var update does not seem to work.


1 answer

0 votes
Tom Bradshaw
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 20, 2019

Hi @TylerAvocat,

Without seeing your `Scrips/set_env_vars_locally.sh` I can't be sure but I suspect the problem is related to the way shell handles environments.

A new shell script is executed in a new sub-shell to avoid contaminating the environment of the calling shell. When you run `printenv` at the end of your shell script it prints the environment in the CURRENT environment context. i.e. the context within the shell script.

The reason your manual export and python run on a local ubuntu machine works is because the environment variable export is being run from the global context so the environment variable persists into the python program being run.

To execute this script in the context of the current shell and have the environment modification persist after the script finishes run the script with this command

. ./Scripts/set_env_vars_locally.sh

The first '.' indicates that the script should be run in the current environment context and will thus persist the environment variable.

For this to work you may have to modify your script to have the first line

#!/bin/bash

To indicate what interpreter you want to use to run the script.

Cheers,

Tom

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events