Can I use environment variables in conditional statements?

Overview:

While configuring a conditional workflow or a when step, you might want to use an environment variable in the logic statement. However you'll find that this doesn't work. This article explains why, and discusses alternatives.

 

Why can't environment variables be used in conditional statements?

In CircleCI, the configuration file is processed in two main stages: compilation time and run time.

  1. Compilation Time:

    • During compilation time, CircleCI processes and parses your configuration file to understand the structure and define the workflow. This is when it evaluates conditions specified in "when" logic statements, which determine when to run specific jobs or steps.
    • At this stage, CircleCI doesn't have access to the actual values of environment variables. Instead, it sees placeholders or references to those variables.

  2. Run Time:

    • When your pipeline is actually executed, during run time, the defined jobs and steps are processed. Now, CircleCI can access and use the real values of environment variables.

The issue arises because logic statements, such as those used in "when" conditions, are evaluated at compilation time when the actual values of environment variables are not available. Since the values are determined at run time, they can't be used directly in logic statements during the compilation phase.

 

Alternatives:

 

Additional Resources:

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.