Running a Github action for every PR comment only when the target is a specific branch

  Kiến thức lập trình

I have an action I’d like to run every time someone comments on a pull request, but only if that pull request is targeting the production branch.

I’ve been unable to figure out a way to do this that doesn’t have me incur Action minutes for every comment made on any pull request in my repository.

I have the following for my workflow definition:

on:
  pull_request:
    types:
      - "opened"
      - "synchronize"
    branches:
      - "production"
  issue_comment:

jobs:
  check-comment:
    uses: my-org/my-shared-actions-repo/.github/workflows/check-comment-action.yml@main
    with:
      repo_name: ${{ github.repository }}
      pr_number:
        ${{ github.event_name == 'pull_request' && github.event.pull_request.number ||
        github.event.issue.number }}
    permissions: read-all

That, unfortunately, triggers for any comment made on any PR in the repository.

I’ve had to add a step to the triggered workflow that does:

jobs:
  check-for-comment:
    runs-on: ubuntu-20.04
    steps:
      - name: Check if the base ref is production
        run: |
          if [ "${BASE_BRANCH}" != "production" ]; then
            exit 1
          fi

To keep the workflow to only PRs targeting production.

This incurs GHA minutes though. And it can add up when it’s firing for comments on PRs that it doesn’t need to be firing for.

I’ve been unable to craft a way, between the on and the jobs.<>.if statements to have GHA do the “should I perform this?” logic.

if: github.base_ref == 'production'

Seemed like it might work, but github.base_ref isn’t available for triggers on comments. Only for PR triggers like opening a PR.

Is it even possible to achieve this kind of filtering without having to use GHA minutes?

LEAVE A COMMENT