GitHub Actions:如何定位除 master 之外的所有分支?

fwe*_*n14 37 git github github-actions

我希望能够让操作在除 master 之外的任何给定分支上运行。我知道有一个预先构建的filter动作,但我想要的是完全相反的。

更像是 GitLab 的except关键字。由于这不在官方文档中,是否有人准备了一个像样的解决方法?

非常感谢。

Oll*_*e P 68

更新:在Samy 的回答中描述了一个更新的过滤器,它提供了一种更简洁的方法来实现这一点。


文档现已更新,提供更多信息:

当您指定branchestags过滤器时,工作流仅在至少一个模式匹配 时运行。对与定义模式不匹配的分支或标签的任何更改都不会触发工作流。您定义模式的顺序很重要:

  • 正匹配后匹配的负模式将再次排除引用。
  • 否定匹配后匹配的肯定模式将再次包含 ref。

因此,为了 exclude master,您需要确保首先包含匹配所有内容的模式:

on:
  push:
    branches:    
      - '*'         # matches every branch that doesn't contain a '/'
      - '*/*'       # matches every branch containing a single '/'
      - '**'        # matches every branch
      - '!master'   # excludes master
Run Code Online (Sandbox Code Playgroud)

  • @wrymug 和其他人不确定这是否是一个新功能,但“**”将匹配每个分支。有关更多信息,请参阅[文档](https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#filter-pattern-cheat-sheet)。 (4认同)

Sam*_*amy 56

现在有一个branches-ignore选项:

on:
  push:
    branches-ignore:
      - master
Run Code Online (Sandbox Code Playgroud)

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#onpushpull_requestbranchestags

  • @Vaulstein `branches-ignore` 和 `branches` 不能在一个事件上同时使用。 (5认同)