Skip to content

Commit d12b0f8

Browse files
committed
fix(ci): nix workflow hardening
1 parent 009464c commit d12b0f8

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@ name: CI
33
on:
44
push:
55
branches: [master]
6+
paths-ignore:
7+
- '*.md'
8+
- 'docs/**'
9+
- 'LICENSE'
10+
- '.github/workflows/docs.yml'
11+
- '.github/workflows/nix.yml'
612
pull_request:
713
branches: [master, 2.x-dev]
14+
paths-ignore:
15+
- '*.md'
16+
- 'docs/**'
17+
- 'LICENSE'
18+
- '.github/workflows/docs.yml'
19+
- '.github/workflows/nix.yml'
820

921
env:
1022
CARGO_TERM_COLOR: always

.github/workflows/nix.yml

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,80 @@
11
name: Nix Derivation
2+
23
on:
3-
pull_request:
4-
branches: [ master ]
5-
push:
6-
branches: [ master, 2.x-dev ]
4+
pull_request_target:
5+
branches: [master]
6+
paths:
7+
- 'Cargo.lock'
8+
- 'package.nix'
9+
- '.github/workflows/nix.yml'
710

811
jobs:
9-
build:
12+
fix-hashes:
13+
name: Fix Nix Hashes
1014
runs-on: ubuntu-latest
1115
permissions:
1216
contents: write
17+
pull-requests: write
1318
steps:
1419
- uses: actions/checkout@v6
1520
with:
16-
fetch-depth: 0
21+
ref: ${{ github.event.pull_request.head.ref }}
22+
repository: ${{ github.event.pull_request.head.repo.full_name }}
1723

1824
- name: Install Nix
1925
uses: cachix/install-nix-action@v31
2026

21-
- name: Install Rust
22-
uses: actions-rs/toolchain@v1
23-
with:
24-
toolchain: stable
25-
profile: minimal
27+
- uses: dtolnay/rust-toolchain@stable
2628

2729
- name: Install fixsha
2830
run: cargo install fixsha
2931

3032
- name: Run fixsha
3133
run: fixsha
3234

33-
- name: Commit hash fixes
34-
if: github.event_name == 'pull_request'
35+
- name: Push hash fix if needed
36+
if: github.event.pull_request.head.repo.full_name == github.repository
37+
env:
38+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3539
run: |
36-
if [[ -n $(git status -s package.nix) ]]; then
37-
git config user.name "github-actions[bot]"
38-
git config user.email "github-actions[bot]@users.noreply.github.com"
39-
git add package.nix
40-
41-
git commit --amend --no-edit
42-
43-
# Rebase onto latest base branch before pushing - prevents "Base branch was modified"
44-
# when multiple PRs merge in quick succession
45-
git fetch origin ${{ github.base_ref }}
46-
git rebase origin/${{ github.base_ref }}
47-
git push --force origin HEAD:${{ github.head_ref }}
40+
if [[ -z $(git status -s package.nix) ]]; then
41+
echo "No hash changes needed"
42+
exit 0
4843
fi
44+
45+
CONTENT=$(base64 -w 0 package.nix)
46+
SHA=$(gh api repos/${{ github.repository }}/contents/package.nix \
47+
--jq '.sha' \
48+
-H "Accept: application/vnd.github+json" \
49+
--method GET \
50+
-f ref=${{ github.event.pull_request.head.ref }})
51+
52+
gh api repos/${{ github.repository }}/contents/package.nix \
53+
--method PUT \
54+
-H "Accept: application/vnd.github+json" \
55+
-f message="fix(nix): update cargoHash" \
56+
-f content="$CONTENT" \
57+
-f sha="$SHA" \
58+
-f branch=${{ github.event.pull_request.head.ref }}
59+
60+
echo "Nix hashes updated via API commit"
61+
62+
- name: Comment on fork PRs
63+
if: github.event.pull_request.head.repo.full_name != github.repository
64+
env:
65+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
66+
run: |
67+
if [[ -z $(git status -s package.nix) ]]; then
68+
echo "No hash changes needed"
69+
exit 0
70+
fi
71+
72+
NEW_HASH=$(grep 'cargoHash' package.nix | sed 's/.*"\(.*\)".*/\1/')
73+
74+
gh pr comment ${{ github.event.pull_request.number }} \
75+
-R ${{ github.repository }} \
76+
--body "The \`cargoHash\` in \`package.nix\` needs updating. Please update it to:
77+
78+
\`\`\`nix
79+
cargoHash = \"$NEW_HASH\";
80+
\`\`\`"

0 commit comments

Comments
 (0)