|
| 1 | +name: Update Vendor Hash |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + paths: |
| 6 | + - "go.mod" |
| 7 | + - "go.sum" |
| 8 | + branches: |
| 9 | + - master |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +jobs: |
| 13 | + update-vendor-hash: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Checkout |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + - name: Install Nix |
| 22 | + uses: cachix/install-nix-action@v31 |
| 23 | + |
| 24 | + - name: Update vendorHash in flake.nix |
| 25 | + run: | |
| 26 | + set -euo pipefail |
| 27 | +
|
| 28 | + # Try to build and capture the expected hash from error message |
| 29 | + echo "Attempting nix build to get new vendorHash..." |
| 30 | + if output=$(nix build .#dgop 2>&1); then |
| 31 | + echo "Build succeeded, no hash update needed" |
| 32 | + exit 0 |
| 33 | + fi |
| 34 | +
|
| 35 | + # Extract the expected hash from the error message |
| 36 | + new_hash=$(echo "$output" | grep -oP "got:\s+\K\S+" | head -n1) |
| 37 | +
|
| 38 | + if [ -z "$new_hash" ]; then |
| 39 | + echo "Could not extract new vendorHash from build output" |
| 40 | + echo "Build output:" |
| 41 | + echo "$output" |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | +
|
| 45 | + echo "New vendorHash: $new_hash" |
| 46 | +
|
| 47 | + # Get current hash from flake.nix |
| 48 | + current_hash=$(grep -oP 'vendorHash = "\K[^"]+' flake.nix) |
| 49 | + echo "Current vendorHash: $current_hash" |
| 50 | +
|
| 51 | + if [ "$current_hash" = "$new_hash" ]; then |
| 52 | + echo "vendorHash is already up to date" |
| 53 | + exit 0 |
| 54 | + fi |
| 55 | +
|
| 56 | + # Update the hash in flake.nix |
| 57 | + sed -i "s|vendorHash = \"$current_hash\"|vendorHash = \"$new_hash\"|" flake.nix |
| 58 | +
|
| 59 | + # Verify the build works with the new hash |
| 60 | + echo "Verifying build with new vendorHash..." |
| 61 | + nix build .#dgop |
| 62 | +
|
| 63 | + echo "vendorHash updated successfully!" |
| 64 | +
|
| 65 | + - name: Commit and push vendorHash update |
| 66 | + run: | |
| 67 | + set -euo pipefail |
| 68 | +
|
| 69 | + if ! git diff --quiet flake.nix; then |
| 70 | + git config user.name "github-actions[bot]" |
| 71 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 72 | +
|
| 73 | + git add flake.nix |
| 74 | + git commit -m "flake: update vendorHash for go.mod changes" |
| 75 | +
|
| 76 | + for attempt in 1 2 3; do |
| 77 | + if git push; then |
| 78 | + echo "Successfully pushed vendorHash update" |
| 79 | + exit 0 |
| 80 | + fi |
| 81 | + echo "Push attempt $attempt failed, pulling and retrying..." |
| 82 | + git pull --rebase |
| 83 | + sleep $((attempt*2)) |
| 84 | + done |
| 85 | +
|
| 86 | + echo "Failed to push after retries" >&2 |
| 87 | + exit 1 |
| 88 | + else |
| 89 | + echo "No changes to flake.nix" |
| 90 | + fi |
0 commit comments