Skip to content

Commit 6dfe596

Browse files
committed
Add action for automatic update of Nix flake vendorHash
1 parent 5f05313 commit 6dfe596

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

.github/workflows/vendor-hash.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
name: Update vendorHash
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- go.mod
9+
- go.sum
10+
pull_request_target:
11+
paths:
12+
- go.mod
13+
- go.sum
14+
15+
permissions:
16+
contents: read
17+
18+
jobs:
19+
vendor-hash:
20+
name: Update vendorHash
21+
runs-on: ubuntu-24.04
22+
steps:
23+
# Decide whether we trust this run *before* checking out any code.
24+
# pull_request_target runs trusted base-repo workflow code with a
25+
# writable token, so we must gate it: only Dependabot, or an actor with
26+
# write access to the repo, may reach the checkout/build steps. Pushes
27+
# to main are implicitly trusted (you cannot push there without access).
28+
- name: Check authorization
29+
id: auth
30+
env:
31+
GH_TOKEN: ${{ secrets.PAT_TOKEN }}
32+
run: |
33+
if [ "${{ github.event_name }}" = "push" ]; then
34+
echo "authorized=true" >> "$GITHUB_OUTPUT"
35+
exit 0
36+
fi
37+
actor='${{ github.actor }}'
38+
if [ "$actor" = "dependabot[bot]" ]; then
39+
echo "authorized=true" >> "$GITHUB_OUTPUT"
40+
exit 0
41+
fi
42+
perm="$(gh api "repos/${{ github.repository }}/collaborators/$actor/permission" --jq '.permission' 2>/dev/null || echo none)"
43+
case "$perm" in
44+
admin | write | maintain)
45+
echo "authorized=true" >> "$GITHUB_OUTPUT"
46+
;;
47+
*)
48+
echo "::notice::skipping vendorHash update for untrusted actor $actor ($perm)"
49+
echo "authorized=false" >> "$GITHUB_OUTPUT"
50+
;;
51+
esac
52+
- uses: actions/checkout@v6
53+
if: steps.auth.outputs.authorized == 'true'
54+
with:
55+
# Push: the branch that was pushed (main). PR: the PR's head branch,
56+
# so the fixup commit lands on the PR. Both are checked out by branch
57+
# name (not detached) so we can commit and push back.
58+
ref: >-
59+
${{ github.event_name == 'pull_request_target'
60+
&& github.event.pull_request.head.ref
61+
|| github.ref_name }}
62+
token: ${{ secrets.PAT_TOKEN }}
63+
- uses: DeterminateSystems/nix-installer-action@main
64+
if: steps.auth.outputs.authorized == 'true'
65+
- name: Recompute vendorHash
66+
if: steps.auth.outputs.authorized == 'true'
67+
run: |
68+
# Force a mismatch so Nix prints the correct hash, then write it back.
69+
sed -i 's|vendorHash = "[^"]*"|vendorHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="|' flake.nix
70+
hash="$(nix build .#ldddns 2>&1 | sed -n 's/.*got:[[:space:]]*//p')"
71+
if [ -z "$hash" ]; then
72+
echo "::error::could not determine vendorHash from nix build output" >&2
73+
exit 1
74+
fi
75+
sed -i "s|vendorHash = \"sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"|vendorHash = \"$hash\"|" flake.nix
76+
- name: Commit if changed
77+
if: steps.auth.outputs.authorized == 'true'
78+
run: |
79+
if ! git diff --quiet -- flake.nix; then
80+
git config user.name 'github-actions[bot]'
81+
git config user.email 'github-actions[bot]@users.noreply.github.com'
82+
git commit -am 'Update vendorHash for Go module changes'
83+
git push
84+
fi

0 commit comments

Comments
 (0)