Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 100 additions & 5 deletions .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,114 @@ on:
fires after CI has already passed. Works on the Free plan.
type: boolean
default: true
required-workflows:
description: |
workflow_run path only. Workflow FILENAMES (space- or newline-
separated, e.g. "ci.yml secret-scan.yml") that must all have a
successful pull_request run for the PR's head commit before it is
merged. A workflow_run trigger fires per workflow, so without this
the first one to go green merges while the others are still
running, and on the Free plan no branch protection catches it.
List every one of them in the caller's `on.workflow_run.workflows`
too, by display name: the gate re-runs on each completion and the
last one merges. The caller's job must grant `actions: read`.
Empty (default): no gate, as before.
type: string
default: ""
optional-workflows:
description: |
Like required-workflows, for path-filtered workflows: no run, or a
run whose jobs were all skipped, is fine, but a run that exists
must have succeeded.
type: string
default: ""

jobs:
# Runs only when the caller names required workflows. It declares no
# permissions, so it gets the caller's job grant: `actions: read` is needed
# to read sibling runs, and a caller that doesn't use the gate is not asked
# for it (a permission declared here would be checked for every caller).
gate:
if: |
inputs.required-workflows != '' &&
github.event_name == 'workflow_run' &&
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.actor.login == 'dependabot[bot]'
runs-on: ${{ fromJSON(github.event.repository.visibility == 'public' && '["ubuntu-latest"]' || inputs.runs-on) }}
timeout-minutes: 5
outputs:
proceed: ${{ steps.check.outputs.proceed }}
steps:
- name: Require every listed workflow to have passed for this commit
id: check
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
SHA: ${{ github.event.workflow_run.head_sha }}
REQUIRED: ${{ inputs.required-workflows }}
OPTIONAL: ${{ inputs.optional-workflows }}
# tests/dependabot-gate.test.sh runs this block verbatim; keep it the
# only `run:` in this job.
run: |
set -euo pipefail
proceed=true
# The inputs are lists of filenames; fold newlines and runs of blanks
# into single spaces, so the " $list " matches below work.
required=$(printf '%s' "$REQUIRED" | tr -s '[:space:]' ' ')
optional=$(printf '%s' "$OPTIONAL" | tr -s '[:space:]' ' ')
for wf in $required $optional; do
# A failed query must not read as "no run": fail closed.
if ! run=$(gh api \
"repos/$REPO/actions/workflows/$wf/runs?head_sha=$SHA&event=pull_request&per_page=1" \
--jq '.workflow_runs[0] | select(.) | "\(.status) \(.conclusion // "-")"'); then
echo "::error::could not query $wf (does the caller's job grant actions: read?); blocking auto-merge"
proceed=false
continue
fi
if [ -z "$run" ]; then
case " $required " in
# Usually it just hasn't started yet; its own completion
# re-evaluates. A notice, not an error: this blocks either way.
*" $wf "*) echo "::notice::$wf has no run for this commit yet; blocking auto-merge"; proceed=false ;;
*) echo "$wf: no run for this commit; not required" ;;
esac
continue
fi
echo "$wf: $run"
case "$run" in
"completed success") ;;
# A path-filtered workflow can run with its jobs skipped for
# Dependabot (a job-level if:), which concludes "skipped". For an
# optional workflow that is the same as no run.
"completed skipped")
case " $optional " in
*" $wf "*) echo "$wf: skipped for this commit; not required" ;;
*) echo "::notice::$wf was skipped; blocking auto-merge"; proceed=false ;;
esac ;;
completed*) echo "::notice::$wf did not succeed; blocking auto-merge"; proceed=false ;;
*) echo "::notice::$wf still running; its completion will re-evaluate"; proceed=false ;;
esac
done
echo "proceed=$proceed" >> "$GITHUB_OUTPUT"

auto-merge:
needs: gate
# Accept a direct pull_request trigger, or a workflow_run trigger that
# fired for a successful Dependabot pull_request CI run (the Free-plan
# path — workflow_run runs in the base-repo context with a full token,
# unlike the restricted token on the dependabot PR run itself).
# always() so a skipped gate (no required-workflows, or the pull_request
# path) still lets this run; a failed or blocking gate stops it.
if: |
(github.event_name == 'pull_request' && github.actor == 'dependabot[bot]') ||
(github.event_name == 'workflow_run' &&
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.actor.login == 'dependabot[bot]')
always() &&
(needs.gate.result == 'skipped' ||
(needs.gate.result == 'success' && needs.gate.outputs.proceed == 'true')) &&
((github.event_name == 'pull_request' && github.actor == 'dependabot[bot]') ||
(github.event_name == 'workflow_run' &&
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.actor.login == 'dependabot[bot]'))
runs-on: ${{ fromJSON(github.event.repository.visibility == 'public' && '["ubuntu-latest"]' || inputs.runs-on) }}
timeout-minutes: 10
permissions:
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/self-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ jobs:
- name: Plaintext-secret scan fixtures
run: ./tests/plaintext-scan.test.sh

dependabot-gate:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Dependabot merge gate decisions
run: ./tests/dependabot-gate.test.sh

composite-actions-syntax:
runs-on: ubuntu-latest
timeout-minutes: 10
Expand Down
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ project uses [SemVer](https://semver.org/) for the `vMAJOR.MINOR.PATCH` tags.

## [Unreleased]

### Added

- **`dependabot-auto-merge.yml` can gate the `workflow_run` path on every
workflow that must pass** (`required-workflows`, plus `optional-workflows`
for path-filtered ones). A `workflow_run` trigger fires once per workflow,
so the first one to go green merged the PR while the rest were still
running; and callers still on the default `pull_request` + `--auto` path
merged at once on private Free-plan repos, which have no required checks
for `--auto` to wait for (sproncy-website-scrapers #646 merged 11 s after
its CI started, with tests failing). The gate checks each listed workflow's
run for the PR's head commit and fails closed: a missing required run, a
running, failed, cancelled or skipped one, or an API error blocks; an
optional one may be absent or skipped. It is a separate job that declares
no permissions, so only callers that use it grant `actions: read`; with
the inputs empty nothing changes. Replaces the inline gate job eleven
callers copied. Tested by `tests/dependabot-gate.test.sh`, which runs the
gate script verbatim against a fake `gh`.

## [3.7.0] - 2026-10-19

### Changed
Expand Down
20 changes: 17 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,20 +669,34 @@ jobs:
name: Dependabot auto-merge
on:
workflow_run:
workflows: [CI]
# Every workflow in required-/optional-workflows, by display name.
workflows: [CI, Secret scan]
types: [completed]
branches: ["dependabot/**"]
permissions: {}
# Serialise per commit, so two workflows finishing together don't race.
concurrency:
group: dependabot-auto-merge-${{ github.event.workflow_run.head_sha }}
cancel-in-progress: false
jobs:
merge:
# workflow_run runs with the repo default token; if that's read-only
# (the safe org default), grant write here so the reusable can merge.
# workflow_run runs with the repo default token; grant write so the
# reusable can merge, and actions: read so its gate can read the runs.
permissions:
contents: write
pull-requests: write
actions: read
uses: nkg/github-actions/.github/workflows/dependabot-auto-merge.yml@v3
with:
use-auto-merge: false
# Filenames. All must have passed for the PR's commit before it merges.
required-workflows: ci.yml secret-scan.yml
# Path-filtered ones: no run, or skipped, is fine.
# optional-workflows: docs.yml
```

**Name every workflow that must pass in `required-workflows`.** A `workflow_run` trigger fires once per workflow, so without the gate the first workflow to go green merges the PR while the others are still running, and on the Free plan no branch protection stops it. The gate re-runs on each completion and the last one to finish merges. It fails closed: a missing required run, a still-running or failed one, or an API error blocks. A path-filtered workflow belongs in `optional-workflows`. It needs `actions: read` on the calling job; callers that don't pass the inputs don't need it, and nothing changes for them.

The `if:` guard (Dependabot actor + successful run) lives inside the reusable, so the stub stays this small. Major-bump gating on this path is best-effort from the PR title: **grouped** updates can't be classified from the title and are treated as non-major. Keep majors manual where that matters, or set `auto-merge-major: true` to merge them too.

### Dependabot uv lockfile
Expand Down
88 changes: 88 additions & 0 deletions tests/dependabot-gate.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env bash
#
# Test the merge gate in .github/workflows/dependabot-auto-merge.yml.
#
# The gate decides whether a Dependabot PR may merge, for every caller that
# passes `required-workflows`. A gate that wrongly says "proceed" merges a PR
# whose CI failed or hasn't finished, which is exactly what this input exists
# to prevent, and nothing else would catch it.
#
# The gate's `run:` block is extracted FROM the workflow and run verbatim
# against a fake `gh`, so this test cannot drift from what runs.
#
# Run from the repo root: ./tests/dependabot-gate.test.sh

set -uo pipefail

WF=".github/workflows/dependabot-auto-merge.yml"
[ -f "$WF" ] || { echo "run from the repo root" >&2; exit 1; }

work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT

# The block under `id: check`'s `run: |`, up to and including its last line,
# with the step's indentation removed.
awk '
/id: check/ { in_step = 1 }
in_step && /^ *run: \|/ { in_run = 1; next }
in_run {
print
if ($0 ~ /proceed=\$proceed/) exit
}
' "$WF" | sed 's/^ //' > "$work/gate.sh"
# shellcheck disable=SC2016 # a literal $proceed, as written in the workflow
grep -q 'proceed=\$proceed' "$work/gate.sh" || { echo "could not extract the gate script" >&2; exit 1; }

# Fake gh: `gh api repos/R/actions/workflows/<wf>/runs?... --jq ...`.
# RUN_<wf with . and - as _> is what the query returns ("status conclusion",
# or empty for no run); the value FAIL makes the query itself fail.
mkdir -p "$work/bin"
cat > "$work/bin/gh" <<'GH'
#!/usr/bin/env bash
url="$2"
wf="${url#*/actions/workflows/}"; wf="${wf%%/runs*}"
var="RUN_$(printf '%s' "$wf" | tr '.-' '__')"
val="${!var-}"
[ "$val" = "FAIL" ] && { echo "HTTP 403" >&2; exit 1; }
[ -n "$val" ] && printf '%s\n' "$val"
exit 0
GH
chmod +x "$work/bin/gh"

pass=0 fail=0
# check <name> <expected proceed> <REQUIRED> <OPTIONAL> [VAR=value ...]
check() {
local name="$1" want="$2" req="$3" opt="$4"; shift 4
: > "$work/out"
env -i PATH="$work/bin:/usr/bin:/bin" GITHUB_OUTPUT="$work/out" \
REPO=o/r SHA=abc REQUIRED="$req" OPTIONAL="$opt" "$@" \
bash "$work/gate.sh" > "$work/log" 2>&1
local got
got=$(sed -n 's/^proceed=//p' "$work/out")
if [ "$got" = "$want" ]; then
pass=$((pass + 1))
else
fail=$((fail + 1))
echo "FAIL: $name: proceed=${got:-<unset>}, want $want"
sed 's/^/ /' "$work/log"
fi
}

ok="completed success"

check "all required passed" true "ci.yml secret-scan.yml" "" RUN_ci_yml="$ok" RUN_secret_scan_yml="$ok"
check "newline-separated input" true $'ci.yml\n secret-scan.yml\n' "" RUN_ci_yml="$ok" RUN_secret_scan_yml="$ok"
check "a required one failed" false "ci.yml secret-scan.yml" "" RUN_ci_yml="completed failure" RUN_secret_scan_yml="$ok"
check "a required one still running" false "ci.yml secret-scan.yml" "" RUN_ci_yml="in_progress -" RUN_secret_scan_yml="$ok"
check "a required one has no run yet" false "ci.yml secret-scan.yml" "" RUN_ci_yml="$ok"
check "a required one was skipped" false "ci.yml" "" RUN_ci_yml="completed skipped"
check "a required one was cancelled" false "ci.yml" "" RUN_ci_yml="completed cancelled"
check "the query failed (fail closed)" false "ci.yml secret-scan.yml" "" RUN_ci_yml="$ok" RUN_secret_scan_yml=FAIL
check "an optional one has no run" true "ci.yml" "lint.yml" RUN_ci_yml="$ok"
check "an optional one was skipped" true "ci.yml" "lint.yml" RUN_ci_yml="$ok" RUN_lint_yml="completed skipped"
check "an optional one failed" false "ci.yml" "lint.yml" RUN_ci_yml="$ok" RUN_lint_yml="completed failure"
check "an optional one still running" false "ci.yml" "lint.yml" RUN_ci_yml="$ok" RUN_lint_yml="queued -"
check "a name that only prefixes one" false "ci.yml" "ci.yml.bak" RUN_ci_yml_bak="$ok"

echo "dependabot-gate: $pass passed, $fail failed"
[ "$fail" -eq 0 ]
Loading