How to have a defaulted environment variable in config.yml?

Here are two config.yml snippets:

      - run:
          command: echo $DEFAULTED_ENV_VAR
          environment:
            DEFAULTED_ENV_VAR: ${OVERRIDE_ENV_VAR:-default}

$DEFAULTED_ENV_VAR seems to be rendered as a string, the env var is not expanded.

      - run:
          command: echo $DEFAULTED_ENV_VAR
          environment:
            DEFAULTED_ENV_VAR: "${OVERRIDE_ENV_VAR:-default}"

$DEFAULTED_ENV_VAR seems to be rendered as a string, the env var is not expanded.

How does one have a defaulted environment: variable in config.yml?

Turns out this is not possible (as of November 2022), as there’s an outstanding feature request for this: Support variable expansion in config.yml | Cloud Feature Requests | CircleCI Ideas

There is also another solution/workaround.

The value of such an environment variable must come from somewhere, but there is limited value in it coming from the shell that the current job is running in as the shell/environment was created as a clean instance to run the job.

You can pass parameters to the config.yml as detailed here

https://circleci.com/docs/pipeline-variables/

Sadly, currently, this can only be done via API execution of the project pipeline. What is missing is the ability to map a project’s Environment Variables across to a project’s pipeline variable. The end result is that you have to create a config.yml that has logic included that executes itself via an API call while passing any defined environment variables as project pipeline variables.

1 Like

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