返回 CodeWhale
publish-crates.sh
根目录 / scripts / release / publish-crates.sh
1 #!/usr/bin/env bash
2 set -euo pipefail
3
4 script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5 # shellcheck source=scripts/release/crates.sh
6 source "${script_dir}/crates.sh"
7
8 mode="${1:-dry-run}"
9 case "${mode}" in
10 dry-run|publish) ;;
11 *)
12 echo "usage: $0 [dry-run|publish]" >&2
13 exit 1
14 ;;
15 esac
16
17 # Multi-package publication verification was stabilized in Cargo 1.90.
18 # Release tooling can require a newer Cargo than the runtime's Rust MSRV.
19 cargo_version="$(cargo --version)"
20 if [[ ! "${cargo_version}" =~ ^cargo[[:space:]]+([0-9]+)\.([0-9]+)\. ]] ||
21 (( BASH_REMATCH[1] < 1 || (BASH_REMATCH[1] == 1 && BASH_REMATCH[2] < 90) )); then
22 echo "Release preflight requires Cargo 1.90 or newer; found ${cargo_version}. Run rustup update stable and use that toolchain." >&2
23 exit 1
24 fi
25
26 packages=("${release_crates[@]}")
27
28 workspace_version=""
29 build_dir=""
30 own_versions=""
31
32 metadata_inventory="$(
33 python3 "${script_dir}/validate-crate-publish-order.py" "${packages[@]}"
34 )"
35 while IFS=$'\t' read -r kind name value; do
36 case "${kind}" in
37 version)
38 workspace_version="${name}"
39 ;;
40 build-dir)
41 build_dir="${name}"
42 ;;
43 own-version)
44 own_versions+="${name}=${value}"$'\n'
45 ;;
46 esac
47 done <<<"${metadata_inventory}"
48
49 if [[ -z "${workspace_version}" ]]; then
50 echo "Could not determine workspace version." >&2
51 exit 1
52 fi
53 if [[ -z "${build_dir}" ]]; then
54 echo "Could not determine Cargo's build directory." >&2
55 exit 1
56 fi
57
58 crate_version() {
59 local own
60 own="$(sed -n "s/^$1=//p" <<<"${own_versions}")"
61 echo "${own:-${workspace_version}}"
62 }
63
64 echo "Crate publication order OK: ${#packages[@]} workspace crates."
65
66 cargo_home="${CARGO_HOME:-${HOME}/.cargo}"
67 reused=()
68 for package in "${packages[@]}"; do
69 for extracted in "${cargo_home}"/registry/src/*/"${package}-$(crate_version "${package}")"; do
70 if [[ -e "${extracted}" && "${extracted}" != "${cargo_home}"/registry/src/index.crates.io-*/* ]]; then
71 reused+=("extracted source ${extracted}")
72 fi
73 done
74 done
75 deps_dirs=("${build_dir}"/*/deps)
76 if [[ -d "${deps_dirs[0]}" ]]; then
77 include_args=()
78 for package in "${packages[@]}"; do
79 include_args+=("--include=${package//-/_}-*.d")
80 done
81 crate_pattern=""
82 for package in "${packages[@]}"; do
83 package_version="$(crate_version "${package}")"
84 package_version="${package_version//./\\.}"
85 package_version="${package_version//+/\\+}"
86 crate_pattern+="${crate_pattern:+|}${package}-${package_version}"
87 done
88 units="$(
89 grep -rHoE "${include_args[@]}" -- "[^ ]*/registry/src/[^/ ]+/(${crate_pattern})/" "${deps_dirs[@]}" ||
90 [[ $? -eq 1 ]]
91 )" || {
92 echo "Could not scan ${build_dir} for compiled release crates." >&2
93 exit 1
94 }
95 while read -r count extracted; do
96 reused+=("${count} compiled unit(s) in ${build_dir} built from ${extracted%/}")
97 done < <(grep -v -e '^$' -e '/registry/src/index\.crates\.io-' <<<"${units}" | sort -u | cut -d: -f2- | sort | uniq -c)
98 fi
99 if (( ${#reused[@]} > 0 )); then
100 cache_root="${CODEWHALE_CACHE_ROOT:-${XDG_CACHE_HOME:-${HOME}/.cache}/codewhale}"
101 verify_root="${cache_root}/release-verify-$(git rev-parse --short=10 HEAD 2>/dev/null || echo candidate)-${mode}-$(date -u +%Y%m%dT%H%M%SZ)"
102 {
103 echo "Refusing to package release crates: Cargo would reuse ${workspace_version} state from an earlier package run instead of the current sources:"
104 printf ' %s\n' "${reused[@]}"
105 echo "Cargo treats crates served from its temporary package registry as immutable by name and version. It skips extraction once .cargo-ok exists and keeps compiled units that do not track their sources, so this run would verify old code. Nothing was deleted."
106 echo "Rerun from a new, empty Cargo home and build directory. Every run leaves state the next run at this version refuses, so publish and each partial-publish rerun need their own root:"
107 echo " cd \"$(cd "${script_dir}/../.." && pwd)\" && mkdir -p \"${cache_root}\" && mkdir \"${verify_root}\" &&"
108 echo " CARGO_HOME=\"${verify_root}/cargo-home\" CARGO_TARGET_DIR=\"${verify_root}/target\" python3 \"$(dirname "${script_dir}")/build-lock.py\" \"${CODEWHALE_BUILD_LOCK_FILE:-${cache_root}/build.lock}\" -- \"${script_dir}/publish-crates.sh\" ${mode}"
109 echo "CARGO_TARGET_DIR moves the build directory with plain cargo and with scripts/dev-cargo.sh, and build-lock.py holds the machine build lock whichever cargo is on PATH."
110 echo "For publish mode, pass the crates.io token as CARGO_REGISTRY_TOKEN for that one command, so Cargo writes no credentials.toml under the new root. After cargo login with that CARGO_HOME instead, run cargo logout with it once the publish finishes. See docs/RELEASE_RUNBOOK.md."
111 } >&2
112 exit 1
113 fi
114
115 if [[ "${mode}" == "publish" ]]; then
116 "${script_dir}/require-release-tag-checkout.sh"
117 "${script_dir}/verify-release-assets.sh"
118 fi
119
120 # Verify the complete release together, including Cargo's publication checks.
121 # Unpublished dependencies resolve through a temporary local registry, and
122 # each unpacked tarball builds before the first real upload is attempted.
123 package_args=(--dry-run --locked --registry crates-io)
124 if [[ "${mode}" == "dry-run" ]]; then
125 package_args+=(--allow-dirty)
126 fi
127 for package in "${packages[@]}"; do
128 package_args+=(-p "${package}")
129 done
130
131 echo "Verifying all ${#packages[@]} release package tarballs before any upload..."
132 env -u CARGO_REGISTRY_TOKEN cargo publish "${package_args[@]}"
133
134 # crates.io refuses an upload over 10 MiB compressed with HTTP 413, and only
135 # when the upload happens: v0.10.1 uploaded 26 crates before codewhale-tui was
136 # refused at 11.93 MiB, stranding tui and its dependent cli. The dry run above
137 # already built every tarball, so check the real sizes before the first upload
138 # (and on every dry run, where the same failure would otherwise surface only
139 # at release time).
140 echo "Checking release tarball sizes against the crates.io upload cap..."
141 python3 "${script_dir}/check-crate-sizes.py" "${packages[@]}"
142 if [[ "${mode}" == "dry-run" ]]; then
143 echo "Release package verification OK; no crates uploaded."
144 exit 0
145 fi
146
147 wait_for_crate_version() {
148 local crate_name="$1"
149 local crate_version="$2"
150 local attempts=30
151
152 for ((attempt = 1; attempt <= attempts; attempt += 1)); do
153 if crate_index_has "${crate_name}" "${crate_version}"; then
154 return 0
155 fi
156 echo "Waiting for ${crate_name} ${crate_version} to appear in the crates.io index (${attempt}/${attempts})..."
157 sleep 10
158 done
159
160 echo "Timed out waiting for ${crate_name} ${crate_version} to appear in the crates.io index" >&2
161 return 1
162 }
163
164 utc_clock() {
165 python3 -c 'import sys, time; print(time.strftime("%H:%M:%S", time.gmtime(int(sys.argv[1]))))' "$1"
166 }
167
168 rate_limit_attempts=10
169 rate_limit_margin=30
170 rate_limit_fallback=600
171
172 publish_crate() {
173 local crate_name="$1"
174 local crate_version="$2"
175 local log now wake attempt
176 log="$(mktemp)"
177
178 for ((attempt = 1; attempt <= rate_limit_attempts; attempt += 1)); do
179 if cargo publish --locked --registry crates-io -p "${crate_name}" 2>&1 | tee "${log}"; then
180 rm -f "${log}"
181 return 0
182 fi
183 if grep -q "already exists" "${log}"; then
184 rm -f "${log}"
185 echo "${crate_name} ${crate_version} was already uploaded."
186 return 0
187 fi
188 if ! grep -qE "status 429|Too Many Requests" "${log}"; then
189 rm -f "${log}"
190 echo "Publishing ${crate_name} ${crate_version} failed; stopping." >&2
191 return 1
192 fi
193 now="$(date -u +%s)"
194 wake="$(crates_io_retry_after <"${log}")" || wake=$((now + rate_limit_fallback))
195 wake=$((wake + rate_limit_margin))
196 if ((wake < now + rate_limit_margin)); then
197 wake=$((now + rate_limit_margin))
198 fi
199 echo "Rate limited publishing ${crate_name} ${crate_version}; waiting until $(utc_clock "${wake}") UTC (attempt ${attempt}/${rate_limit_attempts})."
200 sleep $((wake - now))
201 done
202
203 rm -f "${log}"
204 echo "Publishing ${crate_name} ${crate_version} is still rate limited after ${rate_limit_attempts} attempts; stopping." >&2
205 return 1
206 }
207
208 for package in "${packages[@]}"; do
209 package_version="$(crate_version "${package}")"
210 if crate_index_has "${package}" "${package_version}"; then
211 echo "Skipping ${package} ${package_version}; already published."
212 continue
213 fi
214 publish_crate "${package}" "${package_version}"
215 wait_for_crate_version "${package}" "${package_version}"
216 echo "Published ${package} ${package_version}."
217 done
218
218 lines BASH