skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    There's indeed no variable at github.event.pull_request.reopened, so that was probably being evaluated as null and the step skipped.

    @azeem's suggestion worked like a charm instead:

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

    I had to move the step into the main job because I needed the condition to run before the main installation step, not in parallel

    on:
      pull_request:
        types: [opened, reopened]
    
    jobs:
      install-app:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - name: Wait for deletion to complete before proceeding with reinstallation
            if: ${{ github.event_name == 'pull_request' && github.event.action == 'reopened' }}
            run: |
              # here there's some logic to check that the GHA that runs when PR is closed
              # is done, with a timeout and a `exit 1` after some attempts.
    
          - run: |
            # here goes the normal installation GHA that should be executed at PR reopening
            # too, but only after the deletion is completed
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search