update-nix-hashes.yml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. name: Update Nix Hashes
  2. permissions:
  3. contents: write
  4. on:
  5. workflow_dispatch:
  6. push:
  7. paths:
  8. - "bun.lock"
  9. - "package.json"
  10. - "packages/*/package.json"
  11. - "flake.lock"
  12. - ".github/workflows/update-nix-hashes.yml"
  13. pull_request:
  14. paths:
  15. - "bun.lock"
  16. - "package.json"
  17. - "packages/*/package.json"
  18. - "flake.lock"
  19. - ".github/workflows/update-nix-hashes.yml"
  20. jobs:
  21. update-node-modules-hashes:
  22. if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
  23. runs-on: blacksmith-4vcpu-ubuntu-2404
  24. env:
  25. TITLE: node_modules hashes
  26. steps:
  27. - name: Checkout repository
  28. uses: actions/checkout@v6
  29. with:
  30. token: ${{ secrets.GITHUB_TOKEN }}
  31. fetch-depth: 0
  32. ref: ${{ github.head_ref || github.ref_name }}
  33. repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
  34. - name: Setup Nix
  35. uses: nixbuild/nix-quick-install-action@v34
  36. - name: Configure git
  37. run: |
  38. git config --global user.email "[email protected]"
  39. git config --global user.name "Github Action"
  40. - name: Pull latest changes
  41. env:
  42. TARGET_BRANCH: ${{ github.head_ref || github.ref_name }}
  43. run: |
  44. BRANCH="${TARGET_BRANCH:-${GITHUB_REF_NAME}}"
  45. git pull --rebase --autostash origin "$BRANCH"
  46. - name: Compute all node_modules hashes
  47. run: |
  48. set -euo pipefail
  49. HASH_FILE="nix/hashes.json"
  50. SYSTEMS="x86_64-linux aarch64-linux x86_64-darwin aarch64-darwin"
  51. if [ ! -f "$HASH_FILE" ]; then
  52. mkdir -p "$(dirname "$HASH_FILE")"
  53. echo '{"nodeModules":{}}' > "$HASH_FILE"
  54. fi
  55. for SYSTEM in $SYSTEMS; do
  56. echo "Computing hash for ${SYSTEM}..."
  57. BUILD_LOG=$(mktemp)
  58. trap 'rm -f "$BUILD_LOG"' EXIT
  59. # The updater derivations use fakeHash, so they will fail and reveal the correct hash
  60. UPDATER_ATTR=".#packages.x86_64-linux.${SYSTEM}_node_modules"
  61. nix build "$UPDATER_ATTR" --no-link 2>&1 | tee "$BUILD_LOG" || true
  62. CORRECT_HASH="$(grep -E 'got:\s+sha256-[A-Za-z0-9+/=]+' "$BUILD_LOG" | awk '{print $2}' | head -n1 || true)"
  63. if [ -z "$CORRECT_HASH" ]; then
  64. CORRECT_HASH="$(grep -A2 'hash mismatch' "$BUILD_LOG" | grep 'got:' | awk '{print $2}' | sed 's/sha256:/sha256-/' || true)"
  65. fi
  66. if [ -z "$CORRECT_HASH" ]; then
  67. echo "Failed to determine correct node_modules hash for ${SYSTEM}."
  68. cat "$BUILD_LOG"
  69. exit 1
  70. fi
  71. echo " ${SYSTEM}: ${CORRECT_HASH}"
  72. jq --arg sys "$SYSTEM" --arg h "$CORRECT_HASH" \
  73. '.nodeModules[$sys] = $h' "$HASH_FILE" > "${HASH_FILE}.tmp"
  74. mv "${HASH_FILE}.tmp" "$HASH_FILE"
  75. done
  76. echo "All hashes computed:"
  77. cat "$HASH_FILE"
  78. - name: Commit ${{ env.TITLE }} changes
  79. env:
  80. TARGET_BRANCH: ${{ github.head_ref || github.ref_name }}
  81. run: |
  82. set -euo pipefail
  83. HASH_FILE="nix/hashes.json"
  84. echo "Checking for changes..."
  85. summarize() {
  86. local status="$1"
  87. {
  88. echo "### Nix $TITLE"
  89. echo ""
  90. echo "- ref: ${GITHUB_REF_NAME}"
  91. echo "- status: ${status}"
  92. } >> "$GITHUB_STEP_SUMMARY"
  93. if [ -n "${GITHUB_SERVER_URL:-}" ] && [ -n "${GITHUB_REPOSITORY:-}" ] && [ -n "${GITHUB_RUN_ID:-}" ]; then
  94. echo "- run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" >> "$GITHUB_STEP_SUMMARY"
  95. fi
  96. echo "" >> "$GITHUB_STEP_SUMMARY"
  97. }
  98. FILES=("$HASH_FILE")
  99. STATUS="$(git status --short -- "${FILES[@]}" || true)"
  100. if [ -z "$STATUS" ]; then
  101. echo "No changes detected."
  102. summarize "no changes"
  103. exit 0
  104. fi
  105. echo "Changes detected:"
  106. echo "$STATUS"
  107. git add "${FILES[@]}"
  108. git commit -m "chore: update nix node_modules hashes"
  109. BRANCH="${TARGET_BRANCH:-${GITHUB_REF_NAME}}"
  110. git pull --rebase --autostash origin "$BRANCH"
  111. git push origin HEAD:"$BRANCH"
  112. echo "Changes pushed successfully"
  113. summarize "committed $(git rev-parse --short HEAD)"