Injecting custom environment variables when running locally via CircleCI CLI



The CircleCI CLI is awesome, and definitely makes it much easier to tinker with 2.0 builds in a tight feedback loop.

Some of my build steps rely on environment variables provided by CircleCI (for example, any environment variables specified in the project settings UI in CircleCI, or AWS credentials).

It would be super useful when running circleci build locally, to be able to pass in my own custom environment variables, so that I can mimic the way the UI lets you set variables.

Specifically, I’m trying to test out a set of AWS credentials and their permissions for the deploy step, and I’d rather not write these credentials to disk (I use aws-vault to obtain short-lived secrets, which get passed to the child command as environment variables).

How to set ENV VARS for local builds using circleci 2.0

This feature would be nice. Without, I can’t test most ot the builds locally.
For example, we collect build scripts in another repository and “import” them with a git checkout. Therefore we use credentials stored in environment variables.

I propose the argument --env-file known from docker run. The file format is known from the .env file project, used also e.g. with circle-env sync.

Same request from @DavidAntaramian (cc’ing @Eric).


Sorry - I never commented here but I did open an internal feature request a month ago.

In the meantime, you can probably work around this with putting an env file in your repo and setting BASH_ENV to it to automatically source.


This workaround is an exercise in missing the point, which is that you need to be able to use secure environment variables locally. An environment file outside the repo solves for this (I know the values I put into CircleCI’s secure environment variable settings), but a file inside the repo is a flat non-starter.


Sorry for being unclear- add it to .gitignore. It’s simple to access it from within the repo directory because it’s already being mounted to the container.


I tried it in the way you mentioned it in another post:

    working_directory: /root/circleci-test
      BASH_ENV: /root/circleci-test/bash.env
      - image: alpine
      - run:
          name: Print test environment variable
          command: |
            cat ${BASH_ENV}
            echo ${TEST_ENV}

But the $BASH_ENV file is not automatically sourced:

====>> Spin up Environment
Build-agent version 0.0.3142-e6f3cba (2017-04-23T16:25:32+0000)
Starting container alpine
using image alpine@sha256:58e1a1bb75db1b5a24a462dd5e2915277ea06438c3f105138f97eb53149673c4

Using build environment variables:
====>> Print test environment variable
#!/bin/sh -eo pipefail
cat {BASH_ENV} echo {TEST_ENV}

TEST_ENV=“Hello world”


i had this same issue and it was because my bash_env file wasnt EXPORTing the env vars


It actually works with an image with Bash installed by default. So, it’s just not working with the alpine image (or other images without Bash).

/etc/profile is not automatically sourced in the alpine image. Maybe it’s intended or a bug.

Sourcing $BASH_ENV in an Alpine docker image
Sourcing $BASH_ENV in an Alpine docker image

Another duplicate: How to set ENV VARS for local builds using circleci 2.0 (for backlinking from there)


We released a new version of the CLI so that now you can use circleci build --env VAR=VAL to set additional environment variables. Run circleci update, and then circleci build --help to verify that it’s available (I needed to run circleci update twice, please share if you needed to do the same).


It has output that says Using build environment variables: with all the CIRCLCI=true, etc. but doesn’t show my variables, even though they are there.

Maybe it needs fixing.


Thanks for reporting this. I just ran a build with circleci build --env VAR=VAL. It looks like VAR isn’t showing up in Using build environment variables:, but it’s available in the container. I made an internal ticket to capture this issue.