publish-signed-assets.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env bash
  2. # Upload signed assets to an already-published GitHub release and add/replace
  3. # their lines in the release's checksums.txt.
  4. #
  5. # Used by the post-release signing jobs (sign-windows, sign-macos). Those jobs
  6. # share a concurrency group so two of them never read-modify-write
  7. # checksums.txt at the same time.
  8. set -euo pipefail
  9. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  10. REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
  11. DIST_DIR="${DIST_DIR:-${REPO_ROOT}/dist}"
  12. CHECKSUMS_NAME="checksums.txt"
  13. usage() {
  14. echo "Usage: $(basename "$0") <release-tag> <signed-file> [<signed-file>...]" >&2
  15. echo "Each file is uploaded under its basename." >&2
  16. exit 1
  17. }
  18. TAG="${1:-}"
  19. [[ -n "${TAG}" && "$#" -ge 2 ]] || usage
  20. shift
  21. if ! command -v gh >/dev/null; then
  22. echo "gh is required to update the GitHub release" >&2
  23. exit 1
  24. fi
  25. mkdir -p "${DIST_DIR}"
  26. names=()
  27. for src in "$@"; do
  28. if [[ ! -f "${src}" ]]; then
  29. echo "Signed file not found: ${src}" >&2
  30. exit 1
  31. fi
  32. name="$(basename "${src}")"
  33. if [[ "$(realpath "${src}")" != "$(realpath -m "${DIST_DIR}/${name}")" ]]; then
  34. cp -f "${src}" "${DIST_DIR}/${name}"
  35. fi
  36. names+=("${name}")
  37. done
  38. checksums_path="${DIST_DIR}/${CHECKSUMS_NAME}"
  39. # gh release upload --clobber deletes the old asset before uploading, so an
  40. # earlier run that failed to upload the manifest can leave the release without
  41. # one, or with an incomplete one. The manifest lists every other release asset, so it can be rebuilt from
  42. # the sha256 digests GitHub records for them.
  43. rebuild_checksums() {
  44. echo "${CHECKSUMS_NAME} is missing or incomplete on release ${TAG}; rebuilding it from release asset digests" >&2
  45. local assets
  46. assets="$(gh release view "${TAG}" --json assets \
  47. --jq ".assets[] | select(.name != \"${CHECKSUMS_NAME}\") | \"\(.digest // \"none\") \(.name)\"")"
  48. : > "${checksums_path}"
  49. local digest name
  50. while read -r digest name; do
  51. [[ -n "${name}" ]] || continue
  52. if [[ "${digest}" != sha256:* ]]; then
  53. echo "Release asset ${name} has no sha256 digest; cannot rebuild ${CHECKSUMS_NAME}" >&2
  54. exit 1
  55. fi
  56. printf '%s %s\n' "${digest#sha256:}" "${name}" >> "${checksums_path}"
  57. done <<< "${assets}"
  58. }
  59. # A failed upload can leave an empty asset in the "starter" state; only an
  60. # "uploaded" manifest is complete enough to update.
  61. uploaded_assets="$(gh release view "${TAG}" --json assets --jq '.assets[] | select(.state == "uploaded") | .name')"
  62. if grep -qxF "${CHECKSUMS_NAME}" <<< "${uploaded_assets}"; then
  63. if ! gh release download "${TAG}" --pattern "${CHECKSUMS_NAME}" --dir "${DIST_DIR}" --clobber; then
  64. echo "Failed to download ${CHECKSUMS_NAME} from release ${TAG}" >&2
  65. exit 1
  66. fi
  67. else
  68. rebuild_checksums
  69. fi
  70. if [[ ! -f "${checksums_path}" ]]; then
  71. echo "${CHECKSUMS_NAME} not found after download from release ${TAG}" >&2
  72. exit 1
  73. fi
  74. update_checksum() {
  75. local file_name="${1}"
  76. local new_checksum
  77. new_checksum="$(cd "${DIST_DIR}" && sha256sum "${file_name}")"
  78. local tmp
  79. tmp="$(mktemp)"
  80. grep -vF " ${file_name}" "${checksums_path}" > "${tmp}" || true
  81. printf '%s\n' "${new_checksum}" >> "${tmp}"
  82. mv "${tmp}" "${checksums_path}"
  83. }
  84. upload_paths=()
  85. for name in "${names[@]}"; do
  86. update_checksum "${name}"
  87. upload_paths+=("${DIST_DIR}/${name}")
  88. done
  89. # --clobber overwrites same-named assets (needed if the sign job is re-run).
  90. gh release upload "${TAG}" "${upload_paths[@]}" --clobber
  91. # The new archives are already on the release, so only the updated manifest
  92. # describes them. Retry it rather than restore the old one.
  93. checksums_attempts=3
  94. for attempt in $(seq 1 "${checksums_attempts}"); do
  95. if gh release upload "${TAG}" "${checksums_path}" --clobber; then
  96. break
  97. fi
  98. if [[ "${attempt}" -eq "${checksums_attempts}" ]]; then
  99. echo "Failed to upload updated ${CHECKSUMS_NAME} after ${checksums_attempts} attempts." >&2
  100. echo "Release ${TAG} now has signed ${names[*]} but a stale or missing ${CHECKSUMS_NAME}; re-run this job." >&2
  101. exit 1
  102. fi
  103. echo "Upload of ${CHECKSUMS_NAME} failed (attempt ${attempt}/${checksums_attempts}); retrying" >&2
  104. sleep $((attempt * 10))
  105. done
  106. echo "Uploaded signed ${names[*]} to release ${TAG}"