返回 CodeWhale
require-release-tag-checkout.sh
根目录 / scripts / release / require-release-tag-checkout.sh
1 #!/usr/bin/env bash
2 # Fail closed before irreversible Cargo/npm publication unless this checkout is
3 # the clean, exact source commit anchored by the matching remote release tag.
4 set -euo pipefail
5
6 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
7 cd "${repo_root}"
8
9 version="${1:-}"
10 if [[ -z "${version}" ]]; then
11 version="$(grep -E '^version = "' Cargo.toml | head -n1 | sed -E 's/^version = "([^"]+)".*/\1/')"
12 fi
13 version="${version#v}"
14 if ! [[ "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
15 echo "error: release version '${version}' must use X.Y.Z" >&2
16 exit 2
17 fi
18
19 tag="v${version}"
20 head_sha="$(git rev-parse --verify 'HEAD^{commit}')"
21 tag_sha="$(git rev-parse --verify "refs/tags/${tag}^{commit}" 2>/dev/null || true)"
22 if [[ -z "${tag_sha}" ]]; then
23 echo "::error::Local release tag ${tag} does not exist." >&2
24 exit 1
25 fi
26 if [[ "${head_sha}" != "${tag_sha}" ]]; then
27 echo "::error::Refusing registry publish from HEAD ${head_sha}; ${tag} is ${tag_sha}." >&2
28 echo "Create a clean detached worktree at ${tag} and publish from there." >&2
29 exit 1
30 fi
31
32 dirty="$(git status --porcelain=v1 --untracked-files=all)"
33 if [[ -n "${dirty}" ]]; then
34 echo "::error::Refusing registry publish from a dirty ${tag} checkout:" >&2
35 printf '%s\n' "${dirty}" >&2
36 exit 1
37 fi
38
39 workspace_version="$(grep -E '^version = "' Cargo.toml | head -n1 | sed -E 's/^version = "([^"]+)".*/\1/')"
40 npm_version="$(node -p "require('./npm/codewhale/package.json').version")"
41 binary_version="$(node -p "require('./npm/codewhale/package.json').codewhaleBinaryVersion")"
42 for pair in "workspace:${workspace_version}" "npm:${npm_version}"; do
43 label="${pair%%:*}"
44 actual="${pair#*:}"
45 if [[ "${actual}" != "${version}" ]]; then
46 echo "::error::${label} version ${actual} does not match ${tag}." >&2
47 exit 1
48 fi
49 done
50 if [[ "${binary_version}" != "${version}" ]]; then
51 if [[ "${CODEWHALE_ALLOW_NPM_BINARY_MISMATCH:-0}" == "1" ]]; then
52 echo "Packaging-only release: ${tag} points at binary release ${binary_version}."
53 else
54 echo "::error::npm binary version ${binary_version} does not match ${tag}." >&2
55 echo "Set CODEWHALE_ALLOW_NPM_BINARY_MISMATCH=1 only for an intentional packaging-only npm release." >&2
56 exit 1
57 fi
58 fi
59
60 remote="${CODEWHALE_RELEASE_REMOTE:-origin}"
61 "${repo_root}/scripts/release/verify-remote-tag.sh" \
62 "${remote}" \
63 "${tag}" \
64 "${head_sha}"
65
66 echo "Release checkout gate OK: clean ${tag} at ${head_sha}."
67
67 lines BASH