0

I need to have a workflow that only runs on pull request reviews to the production branch but currently they are running on all pull request reviews.

I tried all of these options:

on:
  pull_request_review:
    types: [submitted, edited]
    branches:
      - production
on:
  pull_request_review:
    types: [submitted, edited]
    branches: [production]
on:
  pull_request_review:
    types: [submitted, edited]
    branches: production

and the workflow gets executed when there's a pull request review from feature-branch to main (not touching production) at all.

What am I missing? How do you do this?


In GitHub Actions: run step only for certain pull request base branches, there's this workflow:

name: my-workflow
on:
  push:
    branches:
      - develop
  pull_request:
    branches:
      - develop
      - main
    types:
      - closed

and the answer assumes that branches: just works correctly. There's no mention about what else to do to make branches: work. I already added a branches: to my workflow and it's being ignored.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • Per [the docs](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onpull_requestpull_request_targetbranchesbranches-ignore) `branches` is for the _target_ branch. – jonrsharpe May 12 '23 at 10:33
  • @jonrsharpe is there something I'm missing when you emphasize "target"? My target branch is `production`, which is what I wrote in the workflow yaml. And yes, I tried `main` just in case. – Pablo Fernandez May 12 '23 at 10:37
  • @CodeCaster: unless I'm missing something, I don't see the answer there. There's no comment about `branches:` not working. They just use it to filter for `main` and `develop` apparently successfully, when my workflow is not filtering for `production` successfully. – Pablo Fernandez May 12 '23 at 10:40
  • Yes, in _"from `main` to `production`"_ `production` is your target branch. You can select PRs with that _base_ using `branches:`. You can't select the head/source branch with that feature. Look at https://stackoverflow.com/q/62331829/3001761 for how to get the head reference inside the workflow. – jonrsharpe May 12 '23 at 10:45
  • @jonrsharpe and that's what I've been trying to do and it's not working. I literally have `branches: [production]` in the snippets of code I tried. – Pablo Fernandez May 12 '23 at 10:46
  • @CodeCaster: have I successfully addressed your argument for closing (and downvoting?) this question? – Pablo Fernandez May 12 '23 at 10:46
  • Did you perhaps miss the `if: github.ref == 'refs/heads/develop' || github.event.pull_request.base.ref == 'develop'` in the YAML? – CodeCaster May 12 '23 at 10:49
  • @CodeCaster no, I didn't. That's there so that step 1 runs on develop, and step 2 runs on master, but the workflow as a whole runs on both develop and main (I imagine they meant master) and not any other branch. Otherwise, what's the point of even having a list of branches on the `on:` statement above? That question is about having some steps run in some branches, some steps run in some others. Where my question is that the whole branch filtering for a workflow is not working. And yes, I can do an if as a workaround, but then why does `branches:` exist if it does nothing? – Pablo Fernandez May 12 '23 at 10:52
  • `on: { "pull_request_review": { "branches": ["production"], "types": ["submitted", "edited"] } }` (your first and second snippets above, I think the third is also equivalent) should work for running for all matching reviews of PRs _to_ `production` (albeit not only PRs _from_ `main`). I'd expect at least _different_ behaviour for the fourth thing you've tried, so it's hard to tease out exactly what's happening. – jonrsharpe May 12 '23 at 10:53
  • @jonrsharpe: I see your point. I'll clarify the question. Yes, my problem is not that it's running for blah->production, but that it's running for blah->bleh, when I only want to run when the target is production. – Pablo Fernandez May 12 '23 at 10:54
  • You can only create `on` triggers for either a source or a target branch. You must then filter the inverse branch using `if`. – CodeCaster May 12 '23 at 10:55
  • I've used `branches:` filtering in my own workflows, and it works correctly (e.g. https://github.com/textbook/starter-kit/blob/01fe260d5bd15e0909901982d90e215d13de1e4b/.github/workflows/push.yml#L3-L9 -> https://github.com/textbook/starter-kit/pull/1686/checks). The behaviour you're describing would be a bug in GHA, which we can't fix - with those `on:` settings, it should only be triggered by the specified PR review events on the specified branch. – jonrsharpe May 12 '23 at 10:57
  • @CodeCaster: that's fine, I'm happy filtering only on the target branch. I altered the question accordingly. – Pablo Fernandez May 12 '23 at 10:58
  • @jonrsharpe: hence me asking the question, because I'm confused why Github Actions is not behaving as I expect. If Github Actions is broken here, then that should probably be the answer to this question. – Pablo Fernandez May 12 '23 at 10:59
  • I'm afraid I've missed the "review" part. That doesn't seem to have a branches filter. – CodeCaster May 12 '23 at 11:03
  • @CodeCaster: in the first snippet of code, lines 4 and 5, `branches: production`. – Pablo Fernandez May 12 '23 at 11:06
  • 1
    Yes, but it does not seem to be documented that that action trigger (pull_request_review) supports a branches filter. I thought you were using `pull_request`. – CodeCaster May 12 '23 at 11:09
  • @CodeCaster: ah, I see. Then that's the answer. `pull_request_review` doesn't support branch filtering, it's just silently ignored. – Pablo Fernandez May 12 '23 at 11:10
  • I'll add my answer addressing `pull_request_review` once I have the correct workaround running. – Pablo Fernandez May 12 '23 at 11:21

1 Answers1

2

The branches and branches-ignore filters work on the following events:

The pull_request_review is not among those. You can use the if to conditionally execute jobs. In this case, check if the "base" (merge target) is "production":

name: PR_review

on:
  pull_request_review:
    types: [submitted, edited]

jobs:
  test:
    if: ${{ github.event.pull_request.base.ref == 'production' }}
    runs-on: ubuntu-latest
    steps:
      - name: Debug
        run: |
          echo ${{ github.event_name }}
          echo ${{ github.ref }}
          echo ${{ github.event.pull_request.head.ref }}
          echo ${{ github.event.pull_request.base.ref }}

Now when a review is submitted to another branch, this workflow will run - but its job will be marked as "skipped".

CodeCaster
  • 147,647
  • 23
  • 218
  • 272