Avoid re-running wokflow for the same commit

With on: push a workflow sometimes runs multiple times for the same commit (pushed as a branch and then a tag, or fast-forward merge). Is there a way to avoid this (for one specific workflow)? I. e. don’t run the workflow if it already ran for the same commit id.

Yes, you can use the Check Runs API to identify Workflow runs for a ref. As luck would have it, someone has already built a comprehensive Action for this use-case: fkirc/skip-duplicate-actions.

  1. Add a new “pre” job to your Workflow, this job uses fkirc/skip-duplicate-actions to determine if your main job should be skipped
  2. Add a condition to your main job using the should_skip output of the “pre” job.

For example, adapted from the fkirc/skip-duplicate-actions readme:

jobs:
  pre_job:
    runs-on: ubuntu-latest
    outputs:
      should_skip: ${{ steps.skip_check.outputs.should_skip }}
    steps:
      - id: skip_check
        uses: fkirc/skip-duplicate-actions@v3.4.0
        with:
          skip_after_successful_duplicate: 'true'
  main_job:
    needs: pre_job
    if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
    runs-on: ubuntu-latest
    steps:
      - run: echo "Running slow tests..." && sleep 30

I use:

on:
  push:
    branches:
      - "**"
    tags-ignore:
      - "**"

Technically if someone pushed a second branch for the same commit, it’d run, but mostly it guards against tags triggering a thing again.