Skip to content

Commit

Permalink
ci: prevent running jobs twice for push and pull_request events
Browse files Browse the repository at this point in the history
  • Loading branch information
dafyddj committed Apr 26, 2024
1 parent 7a90039 commit 356e485
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,43 @@ concurrency:
cancel-in-progress: true

jobs:
should_run:
name: Should run
runs-on: ubuntu-latest
outputs:
should_run: ${{ steps.check.outputs.should_run }}
steps:
- name: Check whether workflow should run
env:
GH_TOKEN: ${{ github.token }}
id: check
run: |
if [ "${GITHUB_EVENT_NAME}" = "pull_request" ]; then
echo "This run was triggered by a pull request event"
OUTPUT="should_run=true"
echo "Setting '$OUTPUT'"
echo "$OUTPUT" >> $GITHUB_OUTPUT
exit
fi
BRANCH_NAME="${GITHUB_REF#refs/heads/}"
PR_COUNT=$(gh api "repos/${GITHUB_REPOSITORY}/pulls?state=open" \
| jq --arg branch "$BRANCH_NAME" '.[] | select(.head.ref == $branch) | .number' \
| wc -l)
if [ $PR_COUNT -gt 0 ]; then
echo "There is an associated pull request for branch $BRANCH_NAME"
OUTPUT="should_run=false"
echo "Setting '$OUTPUT'"
echo "$OUTPUT" >> $GITHUB_OUTPUT
else
echo "There is no associated pull request for branch $BRANCH_NAME"
OUTPUT="should_run=true"
echo "Setting '$OUTPUT'"
echo "$OUTPUT" >> $GITHUB_OUTPUT
fi
pre-commit:
name: Run `pre-commit`
needs: should_run
if: fromJSON(needs.should_run.outputs.should_run)
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
Expand Down

0 comments on commit 356e485

Please sign in to comment.