Skip to content

Commit faa8753

Browse files
committed
feat(ci): use REST API to make bot commit verified
1 parent cc1c60d commit faa8753

File tree

1 file changed

+77
-8
lines changed

1 file changed

+77
-8
lines changed
Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,102 @@
11
---
2-
name: "Flake.lock: update Nix dependencies"
2+
name: "Flake.lock: update Nix dependencies (Verified)"
33
on:
44
workflow_dispatch: # allows manual triggering
55
schedule:
6-
- cron: "0 0 * * 0" # runs weekly on Sunday at 00:00
6+
- cron: 0 0 * * 0 # runs weekly on Sunday at 00:00
7+
8+
env:
9+
BRANCH: "main"
10+
COMMIT_MESSAGE: "chore(lockfile): auto update flake.lock"
11+
GIT_NAME: "github-actions[bot]"
12+
GIT_EMAIL: "41898282+github-actions[bot]@users.noreply.github.com"
713

814
jobs:
915
nix-flake-update:
1016
permissions:
1117
contents: write
1218
id-token: write
1319
runs-on: ubuntu-latest
20+
1421
steps:
1522
- name: Checkout repository
1623
uses: actions/checkout@v5
24+
1725
- name: Install Nix
1826
uses: DeterminateSystems/determinate-nix-action@v3
27+
1928
- name: Check Nix flake inputs
2029
uses: DeterminateSystems/flake-checker-action@v12
2130
with:
2231
ignore-missing-flake-lock: false
2332
fail-mode: true
33+
2434
- name: Update flake.lock
2535
run: |
2636
nix flake update
27-
- uses: stefanzweifel/git-auto-commit-action@v7
37+
38+
- name: Detect modified files
39+
id: diff
40+
shell: bash
41+
run: |
42+
set -euo pipefail
43+
# List modified (tracked) files relative to HEAD.
44+
# If you only want specific patterns, add a grep here (e.g., grep -E '(^|/)flake\.lock$').
45+
mapfile -t changed < <(git ls-files -m --full-name)
46+
47+
if [ "${#changed[@]}" -eq 0 ]; then
48+
echo "changed=false" >> "$GITHUB_OUTPUT"
49+
echo "changed_files=" >> "$GITHUB_OUTPUT"
50+
exit 0
51+
fi
52+
53+
# Join into a comma-separated string for downstream steps.
54+
IFS=',' read -r -a _ <<< ""
55+
changed_csv="$(printf "%s," "${changed[@]}")"
56+
changed_csv="${changed_csv%,}"
57+
58+
echo "Changed files:"
59+
printf ' - %s\n' "${changed[@]}"
60+
61+
echo "changed=true" >> "$GITHUB_OUTPUT"
62+
echo "changed_files=${changed_csv}" >> "$GITHUB_OUTPUT"
63+
64+
- name: Commit via REST Contents API (server-signed → Verified)
65+
if: steps.diff.outputs.changed == 'true'
66+
uses: actions/github-script@v8
67+
env:
68+
CHANGED_FILES: ${{ steps.diff.outputs.changed_files }}
2869
with:
29-
commit_message: "chore(lockfile): auto update flake.lock"
30-
commit_user_name: "github-actions[bot]"
31-
commit_user_email: "32497323+github-actions[bot]@users.noreply.github.com"
32-
commit_author: "github-actions[bot] <32497323+github-actions[bot]@users.noreply.github.com>"
33-
file_pattern: "flake.lock"
70+
script: |
71+
const fs = require('fs');
72+
const owner = context.repo.owner;
73+
const repo = context.repo.repo;
74+
const branch = process.env.BRANCH;
75+
const message = process.env.COMMIT_MESSAGE;
76+
77+
const files = (process.env.CHANGED_FILES || '')
78+
.split(',')
79+
.map(s => s.trim())
80+
.filter(Boolean);
81+
82+
for (const path of files) {
83+
const content = fs.readFileSync(path, { encoding: 'base64' });
84+
85+
// Get existing sha if the file already exists
86+
let sha;
87+
try {
88+
const res = await github.rest.repos.getContent({ owner, repo, path, ref: branch });
89+
if (!Array.isArray(res.data)) sha = res.data.sha;
90+
} catch (e) {
91+
if (e.status !== 404) throw e;
92+
}
93+
94+
// NOTE: author/committer intentionally omitted to allow platform signing
95+
const r = await github.rest.repos.createOrUpdateFileContents({
96+
owner, repo, path, branch,
97+
message,
98+
content,
99+
sha
100+
});
101+
core.info(`Committed ${path}: ${r.data.commit.sha}`);
102+
}

0 commit comments

Comments
 (0)