有条件检查字符串是否包含在列表中?

sor*_*rin 12 github-actions

我该如何if github.event.action in ['foo', 'bar']使用 GitHub Actions?我想限制https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#release中的一些操作,并且可能值的列表很长。

使用||会使整个构造变得庞大,特别是如果您已经需要将其与其他条件结合起来。

udo*_*udo 22

您可以使用 GitHub函数来完成此操作:

contains(fromJson('["bar", "foo"]'), github.event.action)
Run Code Online (Sandbox Code Playgroud)

来源:

例子:

...
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)
    ...
...
Run Code Online (Sandbox Code Playgroud)


Guy*_*Guy 3

您可以使用的最接近的一个是

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

文档

contains( search, item )

如果搜索包含项目,则返回 true。如果 search 是一个数组,并且该项是数组中的元素,则此函数返回 true。如果 search 是一个字符串,并且该项是 search 的子字符串,则该函数返回 true。该函数不区分大小写。将值转换为字符串。

使用数组的示例 contains(github.event.actions, 'bug')

使用字符串的示例 contains('Hello world', 'llo')返回 true

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