You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/actions/writing-workflows/choosing-what-your-workflow-does/evaluate-expressions-in-workflows-and-actions.md
+37-7Lines changed: 37 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -109,21 +109,51 @@ env:
109
109
* {% data variables.product.prodname_dotcom %} ignores case when comparing strings.
110
110
* Objects and arrays are only considered equal when they are the same instance.
111
111
112
-
{% data variables.product.prodname_dotcom %} offers ternary operator like behaviour that you can use in expressions. By using a ternary operator in this way, you can dynamically set the value of an environment variable based on a condition, without having to write separate if-else blocks for each possible option.
113
-
114
-
### Example
112
+
% data variables.product.prodname_dotcom %} provides a way to create conditional logic in expressions using binary logical operators (`&&` and `||`). This pattern can be used to achieve similar functionality to the ternary operator (`?:`) found in many programming languages, while actually using only binary operators.
In this example, we're using a ternary operator to set the value of the `MY_ENV_VAR` environment variable based on whether the {% data variables.product.prodname_dotcom %} reference is set to `refs/heads/main` or not. If it is, the variable is set to `value_for_main_branch`. Otherwise, it is set to `value_for_other_branches`.
126
-
It is important to note that the first value after the `&&` must be truthy. Otherwise, the value after the `||` will always be returned.
122
+
In this example, we're using logical operators to set the value of the `MY_ENV_VAR` environment variable based on whether the {% data variables.product.prodname_dotcom %} reference is set to `refs/heads/main` or not.
123
+
124
+
### How It Works
125
+
126
+
This expression uses two binary operators:
127
+
- `&&` (logical AND): Returns the right operand if the left operand is truthy, otherwise returns the left operand
128
+
- `||` (logical OR): Returns the left operand if it's truthy, otherwise returns the right operand
129
+
130
+
### Order of Operations
131
+
132
+
The order of operations is important to understand:
133
+
1. The equality comparison (`==`) is evaluated first
134
+
2. The logical AND (`&&`) has higher precedence than logical OR (`||`)
135
+
3. The expression is evaluated as `(github.ref == 'refs/heads/main' && 'value_for_main_branch') || 'value_for_other_branches'`
136
+
137
+
### Step-by-Step Evaluation
138
+
139
+
When `github.ref == 'refs/heads/main'` is true:
140
+
1. `github.ref == 'refs/heads/main'` evaluates to `true`
141
+
2. `true && 'value_for_main_branch'` evaluates to `'value_for_main_branch'`
142
+
3. `'value_for_main_branch' || 'value_for_other_branches'` evaluates to `'value_for_main_branch'`
143
+
144
+
When `github.ref == 'refs/heads/main'` is false:
145
+
1. `github.ref == 'refs/heads/main'` evaluates to `false`
146
+
2. `false && 'value_for_main_branch'` evaluates to `false`
147
+
3. `false || 'value_for_other_branches'` evaluates to `'value_for_other_branches'`
148
+
149
+
### Important Considerations
150
+
151
+
- This is NOT a true ternary operator (which would take the form `condition ? true_value : false_value`), but rather two binary operators arranged to achieve similar functionality
152
+
- The expression after `&&` must evaluate to a truthy value to work correctly
153
+
- If the expression after `&&` is falsy (empty string, zero, false, null), the value after `||` will be returned even if the initial condition is true
154
+
- The evaluation follows standard JavaScript-like operator precedence rules
155
+
156
+
For complex conditional logic, you might prefer using job conditionals or separate if-steps.
0 commit comments