【问题标题】:How to cancel existing runs when a new push happens on GitHub Actions, but only for PRs?当 GitHub Actions 上发生新推送时如何取消现有运行,但仅适用于 PR?
【发布时间】:2021-07-17 08:40:01
【问题描述】:

基于documentation(还有一个SO answer),我写了这个:

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

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

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

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true
  if: github.ref != 'refs/heads/master'

【问题讨论】:

  • "A concurrency group can be any string or expression" - 所以也许在 master 上,该组是提交哈希,即没有任何东西被分组,而在 master 上它是分支?
  • ????‍♂️,是的!谢谢!可能是早上太早了。
  • 这很有趣,@jonrsharpe,这里没有三元...无论如何我找到了解决方法来获得“如果”,让我试试。

标签: github-actions


【解决方案1】:

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

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}

【讨论】:

  • 天啊,这是一个合适的麦克风放在那里@thisismydesign。使用表达式,而不是硬编码的布尔值 ?‍♂️。
  • 我认为你甚至不需要三元 hack,因为 ==!= 是布尔值。
  • @TWiStErRob 好点!
【解决方案2】:

GitHub Actions expression syntax 中没有三元运算符,但我发现了一些 workaroundshttp://rickluna.com/wp/tag/fake-ternary/ 是一篇很棒的帖子。

head 分支的提交哈希在github context object 中作为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) }}

【讨论】:

  • 在这里获得真正的三元投票:github.com/actions/runner/issues/409
  • 关于${{ x && <yes> || <false> }} 构造的注释:仅当<yes> 不是空字符串时才有效。如果<yes> == '''' 被视为false,然后计算|| 的右侧。刚刚为此浪费了十分钟,希望对您有所帮助!
猜你喜欢
  • 2021-05-25
  • 2020-06-27
  • 2019-09-05
  • 2022-01-05
  • 2021-02-14
  • 1970-01-01
  • 2023-01-05
  • 2019-10-19
  • 2019-09-17
相关资源
最近更新 更多