仅对非草稿拉取请求运行操作

问题描述 投票:0回答:2

我已将 Github 操作设置为在创建草稿拉取请求时跳过,但当拉取请求准备好供审核时不会触发它。当我起草 PR 从草稿更改为准备审核时,有什么方法可以运行该操作吗?

pull_request:
  types: ['opened', 'edited', 'reopened', 'synchronize', 'ready_for_review']

jobs:
 build:
    if: github.event.pull_request.draft == 'false'
    runs-on: ubuntu-latest
github-actions pull-request
2个回答
35
投票

pull_request.draft
是一个布尔值,但您将其视为字符串,因此比较中的类型不匹配。

根据 docs,在这种情况下操作数被强制转换为数字:如果 true 则左侧(布尔值)变为

1
,如果 false 则变为
0
;右侧(字符串)变为
NaN
,因此您的
if
语句永远不会计算为
true

要修复,请删除引号:

    if: github.event.pull_request.draft == false

这可以使用否定运算符

!
来缩短,但是因为
!
对于 YAML 来说是特殊的,所以必须用引号将该值括起来:

    if: '! github.event.pull_request.draft'

0
投票
if: github.event.pull_request.draft == false

本杰明的这个回答是100%正确的。然而,https://github.com/orgs/community/discussions/25722中解释了一些缺点,所以我看到了一位开发人员

types: [opened, synchronize, reopened, ready_for_review]

jobs:
  fail_if_pull_request_is_draft:
    if: github.event.pull_request.draft == true
    runs-on: ...
    steps:
        run: exit 1

  get_changes:
    runs-on: ...
    if: github.event.pull_request.draft == false
© www.soinside.com 2019 - 2024. All rights reserved.