|
| 1 | +name: Update pnpm deps Nix hash |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - pnpm-lock.yaml |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: pnpm-hash-${{ github.event.pull_request.number }} |
| 10 | + cancel-in-progress: true |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + update-pnpm-hash: |
| 17 | + runs-on: |
| 18 | + - codebuild-defguard-client-runner-${{ github.run_id }}-${{ github.run_attempt }} |
| 19 | + |
| 20 | + steps: |
| 21 | + - uses: actions/checkout@v6 |
| 22 | + with: |
| 23 | + # Check out the exact PR head commit so the sha we pass to the API |
| 24 | + # matches what we read from disk - avoids a race if the branch is |
| 25 | + # updated while this job is in flight. |
| 26 | + ref: ${{ github.event.pull_request.head.sha }} |
| 27 | + submodules: recursive |
| 28 | + |
| 29 | + - uses: cachix/install-nix-action@v31 |
| 30 | + with: |
| 31 | + install_options: --no-daemon |
| 32 | + extra_nix_config: | |
| 33 | + experimental-features = nix-command flakes |
| 34 | +
|
| 35 | + - name: Compute correct pnpm deps hash |
| 36 | + id: hash |
| 37 | + run: | |
| 38 | + set -euo pipefail |
| 39 | +
|
| 40 | + echo "nix version: $(nix --version 2>/dev/null || echo 'unavailable')" |
| 41 | +
|
| 42 | + # A valid-format but always-wrong sha256 hash. |
| 43 | + # Identical to lib.fakeHash in nixpkgs. |
| 44 | + FAKE="sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" |
| 45 | +
|
| 46 | + # Extract the current pnpm hash. |
| 47 | + CURRENT=$(sed -n 's/^[[:space:]]*hash = "\(sha256-[^"]*\)".*/\1/p' nix/package.nix | head -1) |
| 48 | + if [ -z "$CURRENT" ]; then |
| 49 | + echo "::error::Could not extract current hash from nix/package.nix" |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | + echo "current hash: ${CURRENT}" |
| 53 | + echo "current=${CURRENT}" >> "$GITHUB_OUTPUT" |
| 54 | +
|
| 55 | + # Swap in the fake hash so Nix will fail and report the real one. |
| 56 | + sed -i "s|hash = \"${CURRENT}\"|hash = \"${FAKE}\"|" nix/package.nix |
| 57 | +
|
| 58 | + # Build only the pnpmDeps fixed-output derivation. |
| 59 | + SYSTEM=$(nix eval --impure --raw --expr 'builtins.currentSystem') |
| 60 | + echo "building pnpmDeps for ${SYSTEM}..." |
| 61 | + BUILD_LOG=$(nix build --no-link --no-write-lock-file \ |
| 62 | + ".#packages.${SYSTEM}.default.pnpmDeps" 2>&1 || true) |
| 63 | +
|
| 64 | + # Nix prints "got: sha256-..." in the hash mismatch error. |
| 65 | + NEW=$(printf '%s' "$BUILD_LOG" | sed -n 's/.*got:[[:space:]]*\(sha256-[^[:space:]]*\).*/\1/p' | head -1) |
| 66 | + if [ -z "$NEW" ]; then |
| 67 | + echo "::error::Could not extract the correct hash from nix output." |
| 68 | + echo "Full build log:" |
| 69 | + printf '%s\n' "$BUILD_LOG" |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | +
|
| 73 | + echo "new hash: ${NEW}" |
| 74 | + echo "new=${NEW}" >> "$GITHUB_OUTPUT" |
| 75 | +
|
| 76 | + # Write the correct hash back into the file. |
| 77 | + sed -i "s|hash = \"${FAKE}\"|hash = \"${NEW}\"|" nix/package.nix |
| 78 | +
|
| 79 | + # Only commit when the hash actually changed; skip if it was already correct. |
| 80 | + - name: Commit updated hash |
| 81 | + if: steps.hash.outputs.current != steps.hash.outputs.new |
| 82 | + uses: actions/github-script@v7 |
| 83 | + env: |
| 84 | + OLD_HASH: ${{ steps.hash.outputs.current }} |
| 85 | + NEW_HASH: ${{ steps.hash.outputs.new }} |
| 86 | + with: |
| 87 | + script: | |
| 88 | + const fs = require('fs'); |
| 89 | + const content = fs.readFileSync('nix/package.nix', 'utf8'); |
| 90 | + // GraphQL createCommitOnBranch requires base64-encoded file contents. |
| 91 | + const encoded = Buffer.from(content).toString('base64'); |
| 92 | +
|
| 93 | + // The GraphQL createCommitOnBranch mutation creates commits that |
| 94 | + // GitHub signs automatically - producing a Verified badge. |
| 95 | + await github.graphql(` |
| 96 | + mutation CreateCommit($input: CreateCommitOnBranchInput!) { |
| 97 | + createCommitOnBranch(input: $input) { |
| 98 | + commit { url } |
| 99 | + } |
| 100 | + } |
| 101 | + `, { |
| 102 | + input: { |
| 103 | + branch: { |
| 104 | + repositoryNameWithOwner: `${context.repo.owner}/${context.repo.repo}`, |
| 105 | + branchName: context.payload.pull_request.head.ref, |
| 106 | + }, |
| 107 | + message: { headline: 'chore(nix): update pnpm deps hash' }, |
| 108 | + fileChanges: { |
| 109 | + additions: [{ path: 'nix/package.nix', contents: encoded }], |
| 110 | + }, |
| 111 | + // Safety check - fails if the branch moved under us. |
| 112 | + expectedHeadOid: context.payload.pull_request.head.sha, |
| 113 | + }, |
| 114 | + }); |
0 commit comments