Search code examples
github-actionscicd

How to trigger GHA when PR is reopened


I'm trying to run a GitHub Action every time a PR is opened or reopened. The GHA triggered on opening works fine. The GHA triggered by the reopening should run some checks and wait for some minutes before letting the "normal" installation steps reinstall the software, otherwise there's a race condition: the deletion step takes a while, meanwhile the reinstallation step sees that everything is still installed, and happily exits; after a while the deletion is finished, and the state of the installation is broken because no software is installed once the PR is reopened.

on:
  pull_request:
    types: [opened, reopened]

jobs:
  wait-on-pr-reopening:
    runs-on: ubuntu-latest
    if: github.event.pull_request.reopened == true
    steps:
      - name: Wait for deletion to complete before proceeding with reinstallation
        run: |
          # here there's some logic to check that the GHA that runs when PR is closed is done

  install-app:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: |
        # here goes the normal installation GHA that should be executed at PR reopening too,
        # but only after the deletion is completed

The problem is that the if condition if: github.event.pull_request.reopened (I tried with both == true and without) is always skipped.

Is that the correct value to filter on?

Is there another way to synchronize the state when reopening a PR?


Solution

  • According to pull_request event payload, there's this action field that you can use to achieve that. Also, if you want to filter on the specific event, you can use github.event_name to do that.

    Combining above will give you something like this to enable/disable a job/step based on pull_request event and its action reopened:

    if: ${{ github.event_name == 'pull_request' && github.event.action == 'reopened' }}
    

    Currently, the workflow in your question only has pull_request trigger so you can skip github.event_name altogether and that should work too:

    if: ${{ github.event.action == 'reopened' }}
    

    But, do this if and only if your workflow won't have any other event.