|
81 | 81 | * {% data variables.product.prodname_dotcom %} ignores case when comparing strings. |
82 | 82 | * Objects and arrays are only considered equal when they are the same instance. |
83 | 83 |
|
84 | | -{% 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. |
85 | | - |
86 | | -### Example |
87 | | - |
88 | | -{% raw %} |
89 | | - |
90 | | -```yaml |
91 | | -env: |
92 | | - MY_ENV_VAR: ${{ github.ref == 'refs/heads/main' && 'value_for_main_branch' || 'value_for_other_branches' }} |
93 | | -``` |
94 | | - |
95 | | -{% endraw %} |
96 | | - |
97 | | -In this example, we're using a combination of `&&` and `||` 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. 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. |
98 | | - |
99 | 84 | ## Functions |
100 | 85 |
|
101 | 86 | {% 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: |
@@ -287,6 +272,43 @@ Creates a hash for all `.rb` files in the `lib` directory at root level, includi |
287 | 272 |
|
288 | 273 | `hashFiles('/lib/**/*.rb', '!/lib/foo/*.rb')` |
289 | 274 |
|
| 275 | +### case |
| 276 | + |
| 277 | +`case( pred1, val1, pred2, val2, ..., default )` |
| 278 | + |
| 279 | +Evaluates predicates in order and returns the value corresponding to the first predicate that evaluates to `true`. If no predicate matches, it returns the last argument as the default value. |
| 280 | + |
| 281 | +#### Example with a single predicate |
| 282 | + |
| 283 | +{% raw %} |
| 284 | + |
| 285 | +```yaml |
| 286 | +env: |
| 287 | + MY_ENV_VAR: ${{ case(github.ref == 'refs/heads/main', 'production', 'development') }} |
| 288 | +``` |
| 289 | + |
| 290 | +{% endraw %} |
| 291 | + |
| 292 | +Sets `MY_ENV_VAR` to `production` when the ref is `refs/heads/main`, otherwise sets it to `development`. |
| 293 | + |
| 294 | +#### Example with multiple predicates |
| 295 | + |
| 296 | +{% raw %} |
| 297 | + |
| 298 | +```yaml |
| 299 | +env: |
| 300 | + MY_ENV_VAR: ${{ case( |
| 301 | + github.ref == 'refs/heads/main', 'production', |
| 302 | + github.ref == 'refs/heads/staging', 'staging', |
| 303 | + startsWith(github.ref, 'refs/heads/feature/'), 'development', |
| 304 | + 'unknown' |
| 305 | + ) }} |
| 306 | +``` |
| 307 | + |
| 308 | +{% endraw %} |
| 309 | + |
| 310 | +Sets `MY_ENV_VAR` based on the branch: `production` for `main`, `staging` for `staging`, `development` for branches starting with `feature/`, or `unknown` for all other branches. |
| 311 | + |
290 | 312 | ## Status check functions |
291 | 313 |
|
292 | 314 | You can use the following status check functions as expressions in `if` conditionals. A default status check of `success()` is applied unless you include one of these functions. For more information about `if` conditionals, see [AUTOTITLE](/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idif) and [AUTOTITLE](/actions/creating-actions/metadata-syntax-for-github-actions#runsstepsif). |
|
0 commit comments