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.
-
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.
-
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:
- An obvious alternative is to modify your configuration so conditional steps or workflows only rely on pipeline parameters/values. However, depending on your case, this might not be possible.
- An other alternative is to leverage Dynamic Configuration and to set parameters using the environment variable(s) in question.
Additional Resources:
- Logic statements [Documentation]
- Defining conditional steps [Documentation]
- Conditional steps in jobs and conditional workflows [Support article]
- Using dynamic configuration [Documentation]
- Building CI/CD pipelines using dynamic config [Blog post / Tutorial]
- How to pass a parameter when using the continuation orb [Support article]
Comments
Article is closed for comments.