Cannot use environment variables in workflow condition

Environment variables are expanded at runtime as opposed to compile-time. Because of that, the config.yml would actually evaluate the exact string $STAGING_BRANCH as opposed to the value it contains.

With that said, have you looked into the newly released feature Dynamic Config via Setup Workflows? With this feature, you could create a preliminary workflow that would apply the environment variable and then call your main config.

setup: true
version: 2.1
orbs:
  continuation: circleci/continuation@0.1.3
workflows:
  setup:
    jobs:
      - continuation/continue:
          configuration_path: ".circleci/main.yml"
          parameters: /home/circleci/test.json
          pre-steps:
            - run:
                command: |
                  export TEST_ENV="hello world"
                  echo '{ "staging-branch": "'$STAGING_BRANCH'" }' >> /home/circleci/test.json

In this example, we move the original .circleci/config.yml to .circleci/main.yml and then create a new .circleci/config.yml with the above. In the main.yml, we can then use the following conditional:

equal: [<< pipeline.parameters.staging-branch >>, << pipeline.git.branch >>]

Remember, you’ll also need to declare the parameter in the new main.yml.

parameters:
  staging-branch:
    type: string
1 Like