Ruben Pretorius
Ruben Pretorius

Reputation: 353

Only run actions on non draft pull request

I have set Github actions to be skipped when a draft pull request has been created however it is not trigger when the pull request is ready for review. Is there any way to run the action when I draft PR is changed from draft to ready for review?

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

jobs:
 build:
    if: github.event.pull_request.draft == 'false'
    runs-on: ubuntu-latest

Upvotes: 32

Views: 18959

Answers (2)

Andrea Paolo Ferraresi
Andrea Paolo Ferraresi

Reputation: 107

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

This answer by Benjamin is 100% correct. However, there are drawbacks explained in https://github.com/orgs/community/discussions/25722 , so I see a fellow developer

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

Upvotes: 5

Benjamin W.
Benjamin W.

Reputation: 52132

pull_request.draft is a boolean, but you're treating it as a string, so the types in your comparison don't match.

According to the docs, the operands are coerced to numbers in this case: the left-hand side (boolean) becomes 1 if true and 0 if false; the right-hand side (string) becomes NaN, so your if statement will never evaluate to true.

To fix, drop the quotes:

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

This can be shortened using the negation operator !, but because ! is special for YAML, the value has to be quoted:

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

Upvotes: 46

Related Questions