Skip to content

Update evaluate-expressions-in-workflows-and-actions.md #38230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
May 30, 2025
Merged
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0fe93b4
Update evaluate-expressions-in-workflows-and-actions.md
silky-x0 May 12, 2025
afa2aa2
Merge branch 'main' into patch-1
silky-x0 May 12, 2025
6450dc4
Merge branch 'main' into patch-1
silky-x0 May 13, 2025
c16a4af
Merge branch 'main' into patch-1
silky-x0 May 13, 2025
4bd7a02
Merge branch 'main' into patch-1
silky-x0 May 14, 2025
ebc39a7
Merge branch 'main' into patch-1
silky-x0 May 14, 2025
0585a40
Merge branch 'main' into patch-1
silky-x0 May 14, 2025
142a9e7
Merge branch 'main' into patch-1
silky-x0 May 14, 2025
c6122da
Merge branch 'main' into patch-1
silky-x0 May 15, 2025
ff9960c
Merge branch 'main' into patch-1
silky-x0 May 15, 2025
2be87bd
Merge branch 'main' into patch-1
silky-x0 May 15, 2025
5238b91
Merge branch 'main' into patch-1
silky-x0 May 16, 2025
97483ad
Merge branch 'main' into patch-1
silky-x0 May 17, 2025
8d15e90
Merge branch 'main' into patch-1
silky-x0 May 19, 2025
27a538b
Merge branch 'main' into patch-1
silky-x0 May 20, 2025
803982f
Merge branch 'main' into patch-1
silky-x0 May 21, 2025
c138654
Merge branch 'main' into patch-1
silky-x0 May 22, 2025
a66583c
Merge branch 'main' into patch-1
silky-x0 May 23, 2025
282e1a0
Merge branch 'main' into patch-1
silky-x0 May 28, 2025
6525e2c
Merge branch 'main' into patch-1
silky-x0 May 29, 2025
2bdfaeb
Merge branch 'main' into patch-1
silky-x0 May 30, 2025
c817803
Update evaluate-expressions-in-workflows-and-actions.md
silky-x0 May 30, 2025
da97b46
Merge branch 'main' into patch-1
silky-x0 May 30, 2025
9642468
Merge branch 'main' into patch-1
Sharra-writes May 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,51 @@ env:
* {% data variables.product.prodname_dotcom %} ignores case when comparing strings.
* Objects and arrays are only considered equal when they are the same instance.

{% 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.

### Example
% 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.

## Example:
{% raw %}

```yaml
env:
MY_ENV_VAR: ${{ github.ref == 'refs/heads/main' && 'value_for_main_branch' || 'value_for_other_branches' }}
```

{% endraw %}

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`.
It is important to note that the first value after the `&&` must be truthy. Otherwise, the value after the `||` will always be returned.
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.

### How It Works

This expression uses two binary operators:
- `&&` (logical AND): Returns the right operand if the left operand is truthy, otherwise returns the left operand
- `||` (logical OR): Returns the left operand if it's truthy, otherwise returns the right operand

### Order of Operations

The order of operations is important to understand:
1. The equality comparison (`==`) is evaluated first
2. The logical AND (`&&`) has higher precedence than logical OR (`||`)
3. The expression is evaluated as `(github.ref == 'refs/heads/main' && 'value_for_main_branch') || 'value_for_other_branches'`

### Step-by-Step Evaluation

When `github.ref == 'refs/heads/main'` is true:
1. `github.ref == 'refs/heads/main'` evaluates to `true`
2. `true && 'value_for_main_branch'` evaluates to `'value_for_main_branch'`
3. `'value_for_main_branch' || 'value_for_other_branches'` evaluates to `'value_for_main_branch'`

When `github.ref == 'refs/heads/main'` is false:
1. `github.ref == 'refs/heads/main'` evaluates to `false`
2. `false && 'value_for_main_branch'` evaluates to `false`
3. `false || 'value_for_other_branches'` evaluates to `'value_for_other_branches'`

### Important Considerations

- 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
- The expression after `&&` must evaluate to a truthy value to work correctly
- If the expression after `&&` is falsy (empty string, zero, false, null), the value after `||` will be returned even if the initial condition is true
- The evaluation follows standard JavaScript-like operator precedence rules

For complex conditional logic, you might prefer using job conditionals or separate if-steps.

## Functions

Expand Down
Loading