How do I change the environment of subsequent steps at build time?



We have a job that creates a python virtualenv, then installs our dependencies into it. How do we ensure that each subsequent step in that job uses the same virtualenv? I seem to remember circleci 1.0 had a special file you could add extra env things to at build time, but I can’t see any reference to this in the 2.0 docs.

At the moment it seems the only alternative is to source the virtualenv’s activation script in every step, but that’s a bit frustrating. Are there any suggested workarounds?


Hi @BRMatt,
found your question looking for a similar use case. Persisting ENVs between steps can be done adding an export instruction to ${BASH_ENV}.

On Alpine based images, you need to add source ${BASH_ENV} to each step (where you want to use environment variables from it).

Here’s a MWE:

version: 2
      - image: debian
      - run:
          name: Export TEST_ENV
          command: echo 'export TEST_ENV="Hello"' >> $BASH_ENV
      - run:
          name: Display TEST_ENV
          command: echo ${TEST_ENV}

Result (in local CircleCI):

====>> Export TEST_ENV
  #!/bin/bash -eo pipefail
echo 'export TEST_ENV="Hello"' >> $BASH_ENV
====>> Display TEST_ENV
  #!/bin/bash -eo pipefail
echo ${TEST_ENV}


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.