【问题标题】:Conditional to check if a string is included in a list?有条件地检查字符串是否包含在列表中?
【发布时间】:2021-11-28 18:55:15
【问题描述】:

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

使用|| 会使整个构造变得庞大,尤其是当您已经需要将它与其他条件结合起来时。

【问题讨论】:

标签: github-actions


【解决方案1】:

您可以通过 GitHub functions 来实现这一点:

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

来源:

示例:

...
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)
    ...
...

【讨论】:

    【解决方案2】:

    您可以使用的最接近的是

    ${{ 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') 返回真

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 2022-12-13
      • 2013-01-01
      • 2017-11-20
      相关资源
      最近更新 更多