Github Action for checking if the current commit belongs to an open pull request.
Inside a running GitHub Action, you can't always know if you are currently in a PR. When the trigger event is not pull_request
(e.g push
), you don't have that information.
If for example you need to configure a workflow to run on both push
and pull_request
events, there is currently no way to prevent it from running twice when commits are pushed to a PR branch. See the "Prevent duplicate checks on PRs" example.
This action enables you to know if the commit is part of an open PR no matter which event type triggered the workflow.
steps:
- uses: insurgent-lab/is-in-pr-action@v0.2.0
id: isInPR
- run: echo "This commit is part of a PR!"
if: ${{ steps.isInPR.outputs.result == 'true' }}
- run: echo "This commit is not part of a PR!"
if: ${{ steps.isInPR.outputs.result == 'false' }}
jobs:
is-in-pr:
runs-on: ubuntu-latest
steps:
- uses: insurgent-lab/is-in-pr-action@v0.2.0
id: isInPR
outputs:
result: ${{ steps.isInPR.outputs.result }}
log-result:
runs-on: ubuntu-latest
needs: is-in-pr
steps:
- run: echo "This commit is part of a PR!"
if: ${{ needs.is-in-pr.outputs.result == 'true' }}
- run: echo "This commit is not part of a PR!"
if: ${{ needs.is-in-pr.outputs.result == 'false' }}
steps:
- uses: insurgent-lab/is-in-pr-action@v0.2.0
id: isPR
with:
# Include draft PRs. (default: true)
includeDraft: false
See action.yml for more details.
In this example, we run the action in a separate job and use its output to avoid running our "Test" workflow twice on PRs (because both the push
& pull_request
triggers). Note: this is a good solution to deduplicate PRs checks on Renovate Bot failed "branch automerge".
name: Test
on:
push:
pull_request:
jobs:
prevent-duplicate-checks:
runs-on: ubuntu-latest
steps:
- uses: insurgent-lab/is-in-pr-action@v0.2.0
id: isInPR
outputs:
should-run: ${{ !(steps.isInPR.outputs.result == 'true' && github.event_name == 'push') }}
test:
runs-on: ubuntu-latest
needs: prevent-duplicate-checks
if: ${{ needs.prevent-duplicate-checks.outputs.should-run == 'true' }}
# ...
Check out 8BitJonny/gh-get-current-pr
, from which this action was forked.
Contributions are always welcome!