当 GitHub Actions 上发生新推送时,如何取消现有运行,但仅限于 PR?

TWi*_*Rob 9 github-actions

根据文档(还有一个SO 答案),我写了这个:

on:
  push
concurrency:
  # Documentation suggests ${{ github.head_ref }}, but that's only available on pull_request/pull_request_target triggers.
  group: ci-${{ github.ref }}
  cancel-in-progress: true
jobs:
  ...
Run Code Online (Sandbox Code Playgroud)

这会为每个分支创建一个单独的组,并在稍后推送/强制推送到分支时取消构建。可悲的是,这也取消了master构建,这是我不想要的。所有提交都master应该完成运行,因此很容易看到何时出现问题。

我本质上是在寻找一种方式来表达:

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true
  if: github.ref != 'refs/heads/master'
Run Code Online (Sandbox Code Playgroud)

thi*_*ign 19

您可以cancel-in-progress根据默认分支指定:

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}
Run Code Online (Sandbox Code Playgroud)


TWi*_*Rob 11

GitHub Actions 表达式语法中没有三元运算符,但我找到了一些解决方法http://rickluna.com/wp/tag/fake-ternary/是一篇关于此的很棒的文章。

github头分支的提交哈希在上下文对象中可用,形式为github.sha

就我而言,假三元的真实性不是问题,它实际上总是必须有一个值,所以我可以这样做:

concurrency:
  # Documentation suggests ${{ github.head_ref }}, but that's only available on pull_request/pull_request_target triggers, so using ${{ github.ref }}.
  # On master, we want all builds to complete even if merging happens faster to make it easier to discover at which point something broke.
  group: ${{ github.ref == 'refs/heads/master' && format('ci-master-{0}', github.sha) || format('ci-{0}', github.ref) }}
Run Code Online (Sandbox Code Playgroud)