Trigger job on push on commit which is in a PR via if condition

I seem to be unable to locate in the documentation where I can trigger a job with the following conditions only:

  1. on push
  2. branch happens to be in a PR

Which means that pushes on branches that are not currently in a PR would not trigger the job.

How would one write such a workflow yaml file?

I would like this to happen via the if: condition.

Hi @basickarl, welcome to the GitHub Support Community! I believe you should be able to do this without needing to use the if conditional, by listening to the synchronize type of the pull_request event:

on:
  pull_request:
    types: [synchronize]

Or is there a specific reason you need to use the if conditional? If so, it’d be helpful to hear why you’re listening for that!

Hi thanks for the welcoming :slight_smile:.

I do wish to specifically do this via an if condition. I wish to do the following:

  1. build app
  2. run tests
  3. deploy if the commit push is in a PR.

Basically I do not wish to deploy the application if it’s a normal push.

Something like this:

jobs:
    build:
        runs-on: ubuntu-latest
        strategy:
            matrix:
                node-version: [12.x]
        steps:
            - run: npm run build # <--- runs on every push
            - run: npm run test # <--- runs on every push
            - name: deploy # <--- only runs if it is a PR
              if: pull_request === true (or something)
              run: ...my deploy code
1 Like

Awesome, thanks for the context! So in this case you should be able to look at the event_name item inside the github context object.

        steps:
            - name: deploy
              if: github.event_name == 'pull_request'
              run: ...

You could also specifically only listen for pushes to the pull request too:

              if: github.event_name == 'pull_request' && github.event.action == 'synchronize'

You can see the syntax for constructing these statements here:

2 Likes

Ah I see!

I did try the following:

name: Test

on:
    pull_request:
        types: [synchronize]

jobs:
    build:
        runs-on: ubuntu-latest
        strategy:
            matrix:
                node-version: [12.x]
        steps:
            - name: test
              if: github.event_name == 'pull_request' && github.event.action == 'synchronize'
              run: echo hello

But it doesn’t seem to want to trigger a workflow now :frowning_face::

I am force pushing from branch km/auth0 (the workflow file is in that branch).

I solved the issue. It was that the PR had a merge conflict with the master branch. I resolved it and now the pull_request event is triggering correctly. Thanks @thomasshaped for your code, it also helped!