Don't run actions on draft pull requests

I am trying to set up my workflow so it doesn’t run when opening/synching/reopening pull requests that are marked as draft.

I have found that I can use

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

on my job, but it will still run for a few seconds, and “fail”, leaving a red X on every commit on the PR until it is marked ready for reveiw.

I could maybe add a dummy-job that has

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

on it, and just echo something, but it will still run for a few seconds though.

My test-workfow so far looks like this:

name: Deploy test

on:
  pull_request:
    branches: [master]
    types: [synchronize, opened, reopened, ready_for_review]

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

    steps:
    - uses: actions/checkout@v1
    - name: Run all the stuff to set up my site and run tests
      run: set_up_site

  draft-build:
    runs-on: ubuntu-latest
    if: github.event.pull_request.draft == true
    timeout-minutes: 1

    steps:
    - name: Run a one-line script
      run: echo Draft PR, you are good.

Will those seconds be added up and billed? Is there another or better way of getting around this?

2 Likes

jobs.<id>.if is broken currently, that’s why you get “fail” instead of “skip” status.

1 Like

Ah, okay. So if I just leave it as-is and they fix the issue it won’t be a problem?

Yeah, if you don’t mind extra notifications. I’m not touching mine waiting for a fix - beauty of betas :]

1 Like

I can confirm that this works for me now:

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

Thanks for pointing it out!

1 Like

Because of this, I have found a better way for myself.

on:
  pull_request:
    types: [review_requested, ready_for_review]

See
https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-event-pull_request
https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#onevent_nametypes

@viktorianer 

on:
  pull_request:
    types: [review_requested, ready_for_review]

This isn’t working for me. Github actions are run anyways.

@wipe2238 

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

Is working but it has 2 drawbacks:

 - The check is reported as successful even though it didn’t actually run

 - When the PR is switched from “draft” to “ready” the CI does not run again and the check is still marked as successful so  a user is able to merge the PR without having the check actually pass. 

@julioromano
Could you provide your configuration? Because, it should work. We use it in our configuration and it is working very well.

@viktorianer 

name: My CI

on:
  push:
    branches: [master]
    tags:
      - '*'
  pull_request:
    types: [review_requested, ready_for_review]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
        # omitted

@julioromano
Thank you.

But I do not see my suggested solution in your configuration :roll_eyes:.

Sorry brainfart :slight_smile: I edited it.

I pasted wrong code while trying other solutions.

@julioromano I think it is not working because you have these two lines here, which is not what I suggested. 

@julioromano wrote:

@viktorianer 

 

name: My CI

on:
push:
branches: [master]


 

You should remove them. 

It should look like this:

name: reviewdog
on:
  pull_request:
    types: [review_requested, ready_for_review]
jobs:
  ...:

I tested it, still not very practical : it would run action once after undrafting or review request, but not for further pushes on the PR (e.g. when the reviewer asked changes and the developer pushes them).

When your actions are running tests or lint… it’s not ideal.

Just invite the reviewer again (small refresh button next to the name). It is not really good, but works. Unlike other things.

It seems to me that you should just use both:

on:  
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]
jobs:
  JOB:
    if: github.event.pull_request.draft == false

So it won’t run while draft, but it does run when undrafting and for future updates.