|
| 1 | +name: Update Nix Vendor Hash |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - 'go.mod' |
| 7 | + - 'go.sum' |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + update-nix-hash: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + ref: ${{ github.head_ref }} |
| 19 | + |
| 20 | + - uses: cachix/install-nix-action@v27 |
| 21 | + with: |
| 22 | + github_access_token: ${{ secrets.GITHUB_TOKEN }} |
| 23 | + |
| 24 | + - name: Update vendorHash |
| 25 | + id: update |
| 26 | + run: | |
| 27 | + echo "Attempting to build and check for hash mismatch..." |
| 28 | +
|
| 29 | + # Try to build. If it fails, capture the output. |
| 30 | + set +e |
| 31 | + OUTPUT=$(nix build --no-link 2>&1) |
| 32 | + EXIT_CODE=$? |
| 33 | + set -e |
| 34 | +
|
| 35 | + if [ $EXIT_CODE -eq 0 ]; then |
| 36 | + echo "Build successful, no hash update needed." |
| 37 | + exit 0 |
| 38 | + fi |
| 39 | +
|
| 40 | + # Check if failure is due to hash mismatch |
| 41 | + if echo "$OUTPUT" | grep -q "hash mismatch"; then |
| 42 | + echo "Detected hash mismatch. Extracting new hash..." |
| 43 | +
|
| 44 | + # Extract the 'got:' hash. |
| 45 | + # The output format usually contains: |
| 46 | + # got: sha256-...........................................= |
| 47 | + # or |
| 48 | + # got: sha256-...........................................= |
| 49 | + # Handle variable whitespace before and after "got:" |
| 50 | + NEW_HASH=$(echo "$OUTPUT" | grep -E "^\s*got:" | head -n1 | sed 's/.*got:\s*//' | xargs) |
| 51 | +
|
| 52 | + if [ -n "$NEW_HASH" ]; then |
| 53 | + echo "Found new hash: $NEW_HASH" |
| 54 | +
|
| 55 | + # Read current hash for comparison log |
| 56 | + CURRENT_HASH=$(grep "vendorHash =" flake.nix | cut -d'"' -f2) |
| 57 | + echo "Current hash: $CURRENT_HASH" |
| 58 | +
|
| 59 | + if [ "$NEW_HASH" != "$CURRENT_HASH" ]; then |
| 60 | + # Update flake.nix |
| 61 | + # Only match lines starting with optional whitespace followed by "vendorHash =" |
| 62 | + # This prevents accidentally matching comments or other occurrences |
| 63 | + sed -i '/^\s*vendorHash = /s|vendorHash = ".*"|vendorHash = "'$NEW_HASH'"|' flake.nix |
| 64 | + echo "flake.nix updated." |
| 65 | + echo "updated=true" >> $GITHUB_OUTPUT |
| 66 | + else |
| 67 | + echo "Hash extracted matches current hash. Weird." |
| 68 | + exit 1 |
| 69 | + fi |
| 70 | + else |
| 71 | + echo "Could not extract new hash from output." |
| 72 | + echo "Full output:" |
| 73 | + echo "$OUTPUT" |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | + else |
| 77 | + echo "Build failed for reason other than hash mismatch." |
| 78 | + echo "Full output:" |
| 79 | + echo "$OUTPUT" |
| 80 | + # Don't fail the workflow if it's a legitimate build error, |
| 81 | + # as this workflow's sole purpose is updating hashes. |
| 82 | + # Real CI will catch actual build errors. |
| 83 | + exit 0 |
| 84 | + fi |
| 85 | +
|
| 86 | + - name: Commit changes |
| 87 | + if: steps.update.outputs.updated == 'true' |
| 88 | + uses: stefanzweifel/git-auto-commit-action@v5 |
| 89 | + with: |
| 90 | + commit_message: "chore(nix): update vendorHash" |
| 91 | + file_pattern: flake.nix |
0 commit comments