Search code examples
githubgithub-actions

Re-run status checks on a PR via GitHub Actions


I'm trying to setup a workflow using GitHub Actions in which a PR is created to merge a feature branch into a long running branch, and the PR is automatically merged after the required status checks (tests) have passed.

The only aspect of this that is not working is that the checks don't get triggered at the creation of the PR as they normally do.

A workaround I found is to push an empty commit git -c "user.name=github-actions" -c "[email protected]" commit -m "Force run of status checks" --allow-empty. This works (it triggers the status checks) when run manually after the action has finished, but not when it's run as the last step inside the action.

I tried adding a wait/sleep action, with up to 60 seconds of sleep, before the commit, but that didn't help.

Here's the full workflow file:

name: Process Approved Feature Branch
on:
  workflow_dispatch:
    inputs:
      branch_name:
        required: true
        type: string
jobs:
  merge_to_main:
    name: Merge Feature to Main
    runs-on: ubuntu-latest
    steps:
      - name: Git Checkout
        uses: actions/checkout@v3
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          ref: ${{ inputs.branch_name }}
      - name: PR to Main
        run: gh pr create --base my-test-branch --title "Automated PR" --body "Created by Process Approved Feature Branch action"
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Merge to my test branch
        run: gh pr merge --auto --squash
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Trigger checks
        run: git -c "user.name=github-actions" -c "[email protected]" commit -m "Force run of status checks" --allow-empty && git push --no-verify

And here's the relevant part of the workflow that is the required check:

name: Run Tests
on:
  pull_request:
    branches:
      - my-test-branch
jobs:
  ... all the test specific stuff

Does anyone have any idea what might be going on here?

Thanks.


Solution

  • See Triggering a workflow from a workflow.

    You're making the PR with the GITHUB_TOKEN, so it won't fire the event for the workflows that have that trigger (actions taken with GITHUB_TOKEN wont trigger downstream events).

    Even if you swapped to a PAT, there's the issue that occasionally workflows triggered by a PR can take a half a minute or so to kick off, and if you've already run your merge command without setting up those workflows as required, the merge might go through before the workflows have a chance to register.

    The best way to kick them off and guarantee they run before the auto merge goes through is to convert the required checking workflows into workflow_call triggers, that can be directly invoked. That'd mean though that you'll need to add a job that can be run before the merge_to_main job. YOu'll need to need the test calling job in the merge_to_main job.