Skip to content

Commit 2ff70ee

Browse files
authored
Harden Dependabot build workflow (#788)
## Summary - keep the Dependabot build workflow single-job, but harden it a bit - replace `git-auto-commit-action` with explicit `git` commands and step-scoped push auth - add concurrency, a timeout, stricter Dependabot gating, and a guard for moved PR heads ## Why The workflow currently fails in the commit step because `actions/checkout` uses `persist-credentials: false`, but `git-auto-commit-action` later tries to push via `origin` without any credentials: ``` fatal: could not read Username for 'https://github.com': No such device or address ``` This change fixes that failure while keeping credentials scoped to the push step instead of persisting them for the whole job. ## Details - require `github.event.pull_request.user.login == 'dependabot[bot]'` - also require the PR head repo to match `github.repository` - also require the head ref to start with `dependabot/` - check out the exact PR head SHA - run `npm ci --ignore-scripts` - disable git hooks before commit - skip the dist commit if the PR head moved during the run ## Validation - `actionlint .github/workflows/dependabot-build.yml`
1 parent 5ba8a7e commit 2ff70ee

1 file changed

Lines changed: 35 additions & 7 deletions

File tree

.github/workflows/dependabot-build.yml

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@ on:
44
pull_request:
55
types: [opened, synchronize, reopened]
66

7+
concurrency:
8+
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
9+
cancel-in-progress: true
10+
711
permissions:
812
contents: write
913

1014
jobs:
1115
build:
1216
runs-on: ubuntu-latest
13-
if: github.event.pull_request.user.login == 'dependabot[bot]'
17+
if: >-
18+
github.event.pull_request.user.login == 'dependabot[bot]' &&
19+
github.event.pull_request.head.repo.full_name == github.repository &&
20+
startsWith(github.head_ref, 'dependabot/')
21+
timeout-minutes: 15
1422
steps:
1523
- name: Checkout PR branch
1624
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
1725
with:
18-
ref: ${{ github.head_ref }}
26+
ref: ${{ github.event.pull_request.head.sha }}
1927
persist-credentials: false
2028

2129
- name: Setup Node.js
@@ -25,15 +33,35 @@ jobs:
2533
cache: npm
2634

2735
- name: Install dependencies
28-
run: npm ci
36+
run: npm ci --ignore-scripts
2937

3038
- name: Build and test
3139
run: npm run all
3240

3341
- name: Commit built dist
34-
uses: stefanzweifel/git-auto-commit-action@b863ae1933cb653a53c021fe36dbb774e1fb9403 # v5
3542
env:
43+
EXPECTED_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
3644
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37-
with:
38-
commit_message: "Build dist for Dependabot update"
39-
file_pattern: dist/
45+
run: |
46+
git config user.name "github-actions[bot]"
47+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
48+
git config --local core.hooksPath /dev/null
49+
50+
git fetch --no-tags --depth=1 origin "${GITHUB_HEAD_REF}"
51+
if [ "$(git rev-parse FETCH_HEAD)" != "${EXPECTED_HEAD_SHA}" ]; then
52+
echo "::notice::Skipping dist commit because ${GITHUB_HEAD_REF} moved after the workflow started."
53+
exit 0
54+
fi
55+
56+
git add --all dist/
57+
58+
if git diff --cached --quiet; then
59+
echo "No dist changes to commit."
60+
exit 0
61+
fi
62+
63+
git commit -m "Build dist for Dependabot update"
64+
65+
auth="$(printf 'x-access-token:%s' "$GITHUB_TOKEN" | base64 | tr -d '\n')"
66+
git -c "http.https://github.com/.extraheader=AUTHORIZATION: basic ${auth}" \
67+
push origin "HEAD:${GITHUB_HEAD_REF}"

0 commit comments

Comments
 (0)