| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #!/usr/bin/env bash
- # Upload signed assets to an already-published GitHub release and add/replace
- # their lines in the release's checksums.txt.
- #
- # Used by the post-release signing jobs (sign-windows, sign-macos). Those jobs
- # share a concurrency group so two of them never read-modify-write
- # checksums.txt at the same time.
- set -euo pipefail
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
- REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
- DIST_DIR="${DIST_DIR:-${REPO_ROOT}/dist}"
- CHECKSUMS_NAME="checksums.txt"
- usage() {
- echo "Usage: $(basename "$0") <release-tag> <signed-file> [<signed-file>...]" >&2
- echo "Each file is uploaded under its basename." >&2
- exit 1
- }
- TAG="${1:-}"
- [[ -n "${TAG}" && "$#" -ge 2 ]] || usage
- shift
- if ! command -v gh >/dev/null; then
- echo "gh is required to update the GitHub release" >&2
- exit 1
- fi
- mkdir -p "${DIST_DIR}"
- names=()
- for src in "$@"; do
- if [[ ! -f "${src}" ]]; then
- echo "Signed file not found: ${src}" >&2
- exit 1
- fi
- name="$(basename "${src}")"
- if [[ "$(realpath "${src}")" != "$(realpath -m "${DIST_DIR}/${name}")" ]]; then
- cp -f "${src}" "${DIST_DIR}/${name}"
- fi
- names+=("${name}")
- done
- checksums_path="${DIST_DIR}/${CHECKSUMS_NAME}"
- # gh release upload --clobber deletes the old asset before uploading, so an
- # earlier run that failed to upload the manifest can leave the release without
- # one, or with an incomplete one. The manifest lists every other release asset, so it can be rebuilt from
- # the sha256 digests GitHub records for them.
- rebuild_checksums() {
- echo "${CHECKSUMS_NAME} is missing or incomplete on release ${TAG}; rebuilding it from release asset digests" >&2
- local assets
- assets="$(gh release view "${TAG}" --json assets \
- --jq ".assets[] | select(.name != \"${CHECKSUMS_NAME}\") | \"\(.digest // \"none\") \(.name)\"")"
- : > "${checksums_path}"
- local digest name
- while read -r digest name; do
- [[ -n "${name}" ]] || continue
- if [[ "${digest}" != sha256:* ]]; then
- echo "Release asset ${name} has no sha256 digest; cannot rebuild ${CHECKSUMS_NAME}" >&2
- exit 1
- fi
- printf '%s %s\n' "${digest#sha256:}" "${name}" >> "${checksums_path}"
- done <<< "${assets}"
- }
- # A failed upload can leave an empty asset in the "starter" state; only an
- # "uploaded" manifest is complete enough to update.
- uploaded_assets="$(gh release view "${TAG}" --json assets --jq '.assets[] | select(.state == "uploaded") | .name')"
- if grep -qxF "${CHECKSUMS_NAME}" <<< "${uploaded_assets}"; then
- if ! gh release download "${TAG}" --pattern "${CHECKSUMS_NAME}" --dir "${DIST_DIR}" --clobber; then
- echo "Failed to download ${CHECKSUMS_NAME} from release ${TAG}" >&2
- exit 1
- fi
- else
- rebuild_checksums
- fi
- if [[ ! -f "${checksums_path}" ]]; then
- echo "${CHECKSUMS_NAME} not found after download from release ${TAG}" >&2
- exit 1
- fi
- update_checksum() {
- local file_name="${1}"
- local new_checksum
- new_checksum="$(cd "${DIST_DIR}" && sha256sum "${file_name}")"
- local tmp
- tmp="$(mktemp)"
- grep -vF " ${file_name}" "${checksums_path}" > "${tmp}" || true
- printf '%s\n' "${new_checksum}" >> "${tmp}"
- mv "${tmp}" "${checksums_path}"
- }
- upload_paths=()
- for name in "${names[@]}"; do
- update_checksum "${name}"
- upload_paths+=("${DIST_DIR}/${name}")
- done
- # --clobber overwrites same-named assets (needed if the sign job is re-run).
- gh release upload "${TAG}" "${upload_paths[@]}" --clobber
- # The new archives are already on the release, so only the updated manifest
- # describes them. Retry it rather than restore the old one.
- checksums_attempts=3
- for attempt in $(seq 1 "${checksums_attempts}"); do
- if gh release upload "${TAG}" "${checksums_path}" --clobber; then
- break
- fi
- if [[ "${attempt}" -eq "${checksums_attempts}" ]]; then
- echo "Failed to upload updated ${CHECKSUMS_NAME} after ${checksums_attempts} attempts." >&2
- echo "Release ${TAG} now has signed ${names[*]} but a stale or missing ${CHECKSUMS_NAME}; re-run this job." >&2
- exit 1
- fi
- echo "Upload of ${CHECKSUMS_NAME} failed (attempt ${attempt}/${checksums_attempts}); retrying" >&2
- sleep $((attempt * 10))
- done
- echo "Uploaded signed ${names[*]} to release ${TAG}"
|