Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit GitHub action workflow concurrency on push and pull_request?

I would like to limit concurrency to one run for my workflow:

on:
  pull_request:
    paths:
      - 'foo/**'
  push:
    paths:
      - 'foo/**' 

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

However, I found out that for push head_ref is empty and run_id is always unique (as described here: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-using-a-fallback-value)

How I can create a concurrency key that will be constant across pull_request and push events?

like image 262
pixel Avatar asked Jan 26 '26 15:01

pixel


1 Answers

Try this configuration:

concurrency:
  group: ${{ github.head_ref || github.ref_name }} 
  cancel-in-progress: true

This will set the group always to the <branch-name>. The trick is that github.head_ref is only set when the workflow was triggered by a pull_request and it contains the value of the source branch of the PR.

GitHub Repo (especially look at Actions tab to see an example of cancelled workflow)

like image 57
ysfaran Avatar answered Jan 28 '26 16:01

ysfaran