Expressions reference
CircleCI expressions are small boolean and value expressions that CircleCI evaluates against variables and values, such as pipeline values. Several CircleCI features accept expressions, and they all use the syntax described on this page.
For example:
pipeline.git.branch == "main" and not (pipeline.trigger.type starts-with "api")
CircleCI evaluates expressions with the expr library.
Where you can use expressions
Expressions can be used with various CircleCI features. The following table lists the features for which expressions are supported and the variables and values available.
| Feature | Variables and values available | More information |
|---|---|---|
Job filters and workflow |
Pipeline values and pipeline parameters. |
|
Other fields in config, wrapped in |
Pipeline values and pipeline parameters. |
|
Context expression restrictions |
Pipeline values (except the deprecated |
|
Release validation |
The fields that |
Literals
Expressions support number, string, boolean, and regular expression pattern literals.
Strings
Enclose string literals in double quotes (" "). Use \ to escape an embedded quote or an embedded \.
For example, "the quick brown fox" or "You can embed \" and \\ characters".
Regular expression patterns
Enclose regular expression patterns in forward slashes (/ /). Use \ to escape an embedded / or \. For example, /hello\s+world/.
-
The pattern syntax is re2, limited to ASCII and Latin-1 characters in the pattern. Special characters such as
.can still match any Unicode character. -
Capturing groups are allowed in a pattern, but you cannot use the captured values in an expression.
Variables and values
A variable is a name that CircleCI replaces with a value when it evaluates the expression. Expressions can use the following values and variables:
-
Pipeline values, such as
pipeline.git.branch. See Pipeline Values. -
Pipeline parameters, referenced as
pipeline.parameters.<name>. See Pipeline Parameters. -
Element parameters, referenced as
parameters.<name>. These are available in<< >>expressions inside the job that declares them. They are not available in jobfilters, which CircleCI evaluates at the workflow level. -
The job value
job.ssh.enabled, which istrueif SSH is enabled for the job andfalseotherwise. It is the only job value, and it is available only in context expression restrictions, not in configuration. -
Environment variables cannot be used in expressions and logic statements. You can only use pipeline values or pipeline parameters.
Expressions and logic statements are evaluated at compilation time whereas the actual values of environment variables only become available at run time.
See the "Can I use environment variables in conditional statements?" article for more information.
Which of these are available depends on the feature. See Where you can use expressions.
Names follow these rules:
-
A name starts with a letter. It can contain letters, digits, hyphens (
-), and underscores (_). -
Use periods (
.) to separate segments, as inpipeline.git.branch. A name cannot start with a period, and cannot contain consecutive periods.
This applies to pipeline parameters too. If a pipeline parameter has a name that does not follow these rules, CircleCI performs string replacement instead of evaluating an expression.
Valid names:
-
foo -
foo.bar -
fo19-bar.baz_12
Invalid names:
-
14bar: must start with a letter. -
_bar: must start with a letter. -
-bar: must start with a letter. -
.bar: cannot start with a period. -
foo..bar: cannot contain consecutive periods.
Referring to a variable or value that does not exist is an error. See Undefined values for variables and values that exist but have no value.
Operators
You can group sub-expressions with parentheses ( and ).
| Type | Operators | Description |
|---|---|---|
Logical |
|
Short-circuiting boolean operators. |
Equality |
|
String, numeric, and boolean equality. If the operands are of different types, |
Comparison |
|
Numeric comparison. It is an error to use a non-numeric operand. |
Negation |
|
Boolean negation. |
String |
|
String prefix equality. |
String |
|
Matches if the right operand is part of the left operand. |
String |
|
Regular expression match against the entire string. |
Precedence
The following table shows operator precedence from weakest to strongest binding. All operators are left associative.
| Operator | Associativity |
|---|---|
|
left |
|
left |
|
left |
|
left |
|
not has the highest precedence, so CircleCI applies it before any other operator. To negate a larger expression, put the expression in parentheses. For example, when foo is true and bar is false:
-
not foo and baris read as(not foo) and bar, so it evaluates tofalse. -
not (foo and bar)negates the whole parenthesized expression, so it evaluates totrue.
Avoid chaining operators other than and and or. CircleCI evaluates chained operators from left to right, and the result of the first operator becomes an operand of the next. For example, 1 < 2 < 3 is read as (1 < 2) < 3. This compares the boolean true with the number 3, which is an error.
Evaluation
For example, take this expression:
pipeline.git.branch == "main"
and not job.ssh.enabled
and not (pipeline.trigger.type starts-with "api")
With these values:
-
pipeline.git.branchis"main". -
job.ssh.enabledisfalse. -
pipeline.trigger.typeis"api".
CircleCI evaluates the expression from the inside out, as shown in the following diagram. Each box shows an operator and the value it produces.
true"] --> And1["and
true"] Ssh["not job.ssh.enabled
true"] --> And1 And1 --> And2["and
false"] Api["not (pipeline.trigger.type
starts-with #quot;api#quot;)
false"] --> And2
The evaluation steps are as follows:
-
Look up
pipeline.git.branch:"main" == "main" and not job.ssh.enabled and not (pipeline.trigger.type starts-with "api") -
Evaluate
"main" == "main":true and not job.ssh.enabled and not (pipeline.trigger.type starts-with "api") -
Look up
job.ssh.enabled:true and not false and not (pipeline.trigger.type starts-with "api") -
Evaluate
not false:true and true and not (pipeline.trigger.type starts-with "api") -
Evaluate
true and true:true and not (pipeline.trigger.type starts-with "api") -
Look up
pipeline.trigger.type:true and not ("api" starts-with "api") -
Evaluate
"api" starts-with "api":true and not true -
Evaluate
not true:true and false -
Evaluate
true and false:false
The expression evaluates to false.
Undefined values
A variable or value that exists but has no value is undefined. An undefined value is not equal to anything, including itself.
-
If an equality or comparison operator has an undefined operand, the result is undefined. For example, when
foois undefined, each of the following is undefined:-
foo == foo -
foo != 1 -
foo > 3 -
"hello" starts-with foo
-
-
In a logical operator, CircleCI treats an undefined operand as
falsebut preserves the undefined value. This lets you provide a fallback:-
foo or 12evaluates to12. -
12 or fooevaluates to12. -
foo and "hello"evaluates to undefined. -
not fooevaluates totrue.
-
-
If the final result of an expression is undefined, it is treated as
false.
In GitHub OAuth projects, a variable or value that has no value is an empty string, not undefined. This applies on CircleCI Cloud and CircleCI Server. An empty string counts as true, so the rules above do not apply. For example, on a tag build pipeline.git.branch or "none" evaluates to the empty string, not to "none".
Errors
Evaluating an expression fails with an error in the following cases:
-
A variable or value does not exist.
-
An operand of a numeric comparison operator is not a number.
-
An operand of
starts-withorcontainsis not a string. -
The left operand of
matchesis not a string, or the right operand is not a pattern.
What happens next depends on the feature. For example, a context expression restriction that fails to evaluate prevents the context from being used.