Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional to check if a string is included in a list?

How can I do if github.event.action in ['foo', 'bar'] using GitHub Actions? I want to limit some actions from https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#release and the list of possible values is quite long.

Using || makes the entire construct huge, especially if you already need to combine it with other conditions.

like image 462
sorin Avatar asked Dec 30 '25 07:12

sorin


2 Answers

you can accomplish that with GitHub functions:

contains(fromJson('["bar", "foo"]'), github.event.action)

sources:

  • https://docs.github.com/en/actions/learn-github-actions/expressions#contains
  • https://docs.github.com/en/actions/learn-github-actions/expressions#fromjson

example:

...
jobs:
  some-job:
    # runs on pull_request if action is "opened" or "synchronize"
    if: github.event_name == 'pull_request' && contains(fromJson('["opened", "synchronize"]'), github.event.action)
    ...
...
like image 86
udo Avatar answered Jan 02 '26 00:01

udo


The closest one you can use is

${{ contains(github.event.action, 'foo') || contains(github.event.action, 'bar') }}

Documentation

contains( search, item )

Returns true if search contains item. If search is an array, this function returns true if the item is an element in the array. If search is a string, this function returns true if the item is a substring of search. This function is not case sensitive. Casts values to a string.

Example using an array contains(github.event.actions, 'bug')

Example using a string contains('Hello world', 'llo') returns true

Reference: https://docs.github.com/en/free-pro-team@latest/actions/reference/context-and-expression-syntax-for-github-actions#contains

like image 30
Guy Avatar answered Jan 02 '26 01:01

Guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!