From 3003f9deebf91fd86bdbc1203d5fa1897aaf2278 Mon Sep 17 00:00:00 2001 From: Megagyger Date: Fri, 9 Jun 2023 18:57:33 +0200 Subject: [PATCH] Update actions expression documentation for ternary like behaviour (#24357) Co-authored-by: Siara <108543037+SiaraMist@users.noreply.github.com> --- .../actions/learn-github-actions/expressions.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/content/actions/learn-github-actions/expressions.md b/content/actions/learn-github-actions/expressions.md index 736a8d6bbf..29271980c6 100644 --- a/content/actions/learn-github-actions/expressions.md +++ b/content/actions/learn-github-actions/expressions.md @@ -105,6 +105,22 @@ 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 + +{% 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 `&&` condition must be `truthy` otherwise the value after the `||` will always be returned. + ## Functions {% data variables.product.prodname_dotcom %} offers a set of built-in functions that you can use in expressions. Some functions cast values to a string to perform comparisons. {% data variables.product.prodname_dotcom %} casts data types to a string using these conversions: