Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(ci): use per-SHA concurrency for master pushes in Backend CI (#59400)

mendral-app[bot]4e2f14c32e8d
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index 27b8cce2d49a..26d059a36a54 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -16,8 +16,11 @@ on:
merge_group:
concurrency:
- group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
+ # PRs: one active run per branch, cancel stale. Push: per-SHA so master
+ # pushes never cancel each other (check-migrations always completes).
+ group: ${{ github.workflow }}-${{ github.event_name == 'push' && github.sha || github.head_ref || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+
env:
SECRET_KEY: '6b01eee4f945ca25045b5aab440b953461faf08693a9abbf1166dc7c6b9772da' # unsafe - for testing only
DATABASE_URL: 'postgres://posthog:posthog@localhost:5432/posthog'
diff --git a/tools/hogli-commands/hogli_commands/tests/test_workflow_lint.py b/tools/hogli-commands/hogli_commands/tests/test_workflow_lint.py
index bcdf2310a98e..463a271d1ea6 100644
--- a/tools/hogli-commands/hogli_commands/tests/test_workflow_lint.py
+++ b/tools/hogli-commands/hogli_commands/tests/test_workflow_lint.py
@@ -253,6 +253,60 @@ class TestPrConcurrencyCheck:
assert "github.run_id" in issue.message
assert "github.ref" in issue.message
+ def test_passes_with_job_level_concurrency_on_all_jobs(self, tmp_path: Path) -> None:
+ _write(
+ tmp_path,
+ "ci-foo.yml",
+ """
+ name: CI Foo
+ on: [pull_request]
+ jobs:
+ build:
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ concurrency:
+ group: build-${{ github.head_ref || github.ref }}
+ cancel-in-progress: true
+ steps:
+ - run: echo ok
+ test:
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ concurrency:
+ group: test-${{ github.head_ref || github.ref }}
+ cancel-in-progress: true
+ steps:
+ - run: echo ok
+ """,
+ )
+ assert PrConcurrencyCheck().run(_read_all(tmp_path)).issues == []
+
+ def test_passes_with_partial_job_level_concurrency(self, tmp_path: Path) -> None:
+ """Workflows managing concurrency at job-level for some (heavy) jobs pass."""
+ _write(
+ tmp_path,
+ "ci-foo.yml",
+ """
+ name: CI Foo
+ on: [pull_request]
+ jobs:
+ build:
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ concurrency:
+ group: build-${{ github.head_ref || github.ref }}
+ cancel-in-progress: true
+ steps:
+ - run: echo ok
+ lightweight:
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ steps:
+ - run: echo ok
+ """,
+ )
+ assert PrConcurrencyCheck().run(_read_all(tmp_path)).issues == []
+
def test_skips_non_ci_prefix(self, tmp_path: Path) -> None:
_write(
tmp_path,
diff --git a/tools/hogli-commands/hogli_commands/workflow_lint/checks/pr_concurrency.py b/tools/hogli-commands/hogli_commands/workflow_lint/checks/pr_concurrency.py
index 1382f547344a..59e0c697ad1c 100644
--- a/tools/hogli-commands/hogli_commands/workflow_lint/checks/pr_concurrency.py
+++ b/tools/hogli-commands/hogli_commands/workflow_lint/checks/pr_concurrency.py
@@ -1,4 +1,4 @@
-"""PR-triggered ``ci-*.yml`` workflows must declare a safe top-level ``concurrency:`` block.
+"""PR-triggered ``ci-*.yml`` workflows must declare concurrency control.
Without it, every push to a PR branch starts a fresh run while the in-flight
one keeps burning minutes. The repo convention (used by 30+ workflows):
@@ -7,6 +7,10 @@ one keeps burning minutes. The repo convention (used by 30+ workflows):
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+Alternatively, workflows may use job-level concurrency on every job instead of
+a top-level block. This is useful when different jobs need different concurrency
+strategies (e.g. some jobs are per-SHA while others are per-branch).
+
Using ``github.run_id`` as the fallback looks similar but disables dedup for
push events because every run gets a unique group.
@@ -28,7 +32,7 @@ BAD_FALLBACK = re.compile(r"head_ref\s*\|\|\s*github\.run_id")
class PrConcurrencyCheck(WorkflowCheck):
id = "WF002-pr-concurrency"
label = "PR concurrency"
- description = "PR-triggered ci-*.yml workflows declare safe top-level concurrency"
+ description = "PR-triggered ci-*.yml workflows declare safe concurrency (top-level or per-job)"
# Workflows intentionally exempt from concurrency cancellation. Each entry has
# a one-line reason so the next reader knows why.
@@ -46,11 +50,13 @@ class PrConcurrencyCheck(WorkflowCheck):
@property
def fix_hint(self) -> str | None:
return (
- "Add this block after `on:`:\n"
+ "Either add a top-level block after `on:`:\n"
"concurrency:\n"
" group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}\n"
" cancel-in-progress: ${{ github.event_name == 'pull_request' }}\n"
"\n"
+ "Or add a `concurrency:` block to every job in the workflow.\n"
+ "\n"
"Do not use `github.run_id` as the fallback; it creates a unique concurrency group per push run.\n"
"\n"
"Or, if cancelling stale runs would lose data (telemetry, schedule-only PR triggers, etc.),\n"
@@ -81,16 +87,30 @@ class PrConcurrencyCheck(WorkflowCheck):
continue
if wf.concurrency is not None:
continue
+ # Accept job-level concurrency as an alternative — the workflow
+ # intentionally manages concurrency per-job instead of top-level.
+ if _has_job_level_concurrency(wf):
+ continue
result.issues.append(
Issue(
workflow=wf.path.name,
- message="missing top-level concurrency block",
+ message="missing top-level concurrency block (or per-job concurrency on all jobs)",
file=str(wf.path),
)
)
return result
+def _has_job_level_concurrency(wf: Workflow) -> bool:
+ """Return True if at least one job in the workflow declares its own concurrency block.
+
+ This indicates the workflow intentionally manages concurrency at the job level
+ rather than using a single top-level block (e.g. because different jobs need
+ different strategies).
+ """
+ return any("concurrency" in job.raw for job in wf.jobs)
+
+
def _concurrency_group_expr(concurrency: dict | str | None) -> str:
if isinstance(concurrency, str):
return concurrency