blueprint-shared-templates 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,8 +6,10 @@ name: ops-deploy
6
6
  # 要讀私有的 OpenTofu module 倉時(repository variable INFRA_MODULES_REPO),tofu init 前用 GitHub App 的權杖設 git 的 insteadOf。
7
7
  # - workloads:needs iac,在 self-hosted runner 依 ops/versions.json 的宣告上線:下載 ops/versions.lock.json 指定的 bundle,把
8
8
  # ops/workloads/ 底下該環境的設定接在 bundle 的 wrangler.toml 後面(R2 綁定的 bucket_name 從它 needs 的 iac 步驟讀入),
9
- # `wrangler versions upload`,再 `wrangler versions deploy <版本 id>@100 --env <環境>`,最後打 /healthz 核對版號與宣告的 bundle 雜湊。
10
- # 用 Workers 的 token。
9
+ # `wrangler deploy --env <環境>`(一步完成上傳、切 100% 流量與套用 routes、Custom Domain),最後打 /healthz 核對版號與宣告的 bundle 雜湊。
10
+ # 宣告的 Custom Domain 還不存在(第一次部署)時,部署前不探測 /healthz,因為對尚不存在的主機名問解析器,會讓它把 NXDOMAIN 負向快取
11
+ # zone 的 SOA MINIMUM(最多 1800 秒);已存在的 Custom Domain 用 Cloudflare edge 位址(curl --resolve)探測,不經解析器。
12
+ # 用 Workers 的 token,也用它查帳號的 Custom Domain。
11
13
  # 兩個步驟的先後都寫在 ops/needs.json。兩個觸發:
12
14
  # - push 到預設分支:宣告、lock、設定或 iac 有變的單元上線(只改 ops/workloads/ 的設定也會上線)。poly 的 app 與 service 用 GitHub App
13
15
  # 把宣告寫進 poly-ops,推上預設分支就觸發這裡。
@@ -1,10 +1,13 @@
1
1
  #!/usr/bin/env bash
2
2
  # Put what ops/versions.json declares for an environment online. Per declared unit: download its bundle from the
3
3
  # GitHub Release that ops/versions.lock.json names, check the bundle hash against the lock, join the settings of the
4
- # environment (ops/workloads/<purpose>/<env>/<unit>/wrangler.toml) to the wrangler.toml of the bundle, `wrangler versions upload`,
5
- # then `wrangler versions deploy <id>@100` and `wrangler triggers deploy` (the routes, custom domains and workers_dev of the settings).
4
+ # environment (ops/workloads/<purpose>/<env>/<unit>/wrangler.toml) to the wrangler.toml of the bundle, then `wrangler deploy` (one step: upload, 100% of traffic, and the routes,
5
+ # custom domains and workers_dev of the settings).
6
6
  # A unit whose /healthz already reports the declared version, the bundle hash and the hash of the joined settings is skipped, so a rerun
7
- # changes nothing.
7
+ # changes nothing. A Custom Domain that the settings declare (`custom_domain = true`) and that the account does not have yet means the unit is not
8
+ # online, and the probe is not made at all: asking the resolver for a hostname that does not exist yet makes it keep NXDOMAIN for the SOA
9
+ # MINIMUM of the zone (up to 1800 seconds), and smoke.sh could not reach the new hostname for that long (healthz.sh reaches an existing one
10
+ # by an edge address, without the resolver).
8
11
  #
9
12
  # Each environment is a separate Worker (<unit>-<env>), and a version id belongs to one Worker, so an
10
13
  # environment never reuses the id of another: what carries over is the bundle, and the hash tells it is the same.
@@ -24,11 +27,14 @@
24
27
  # place that has it, and it is added to the table when the settings are joined.
25
28
  #
26
29
  # usage: apply-ops.sh <env> [--only unit1,unit2]
27
- # env: WRANGLER the command that runs wrangler (default `pnpm exec wrangler`; local mode: the stand-in)
30
+ # env: WRANGLER the command that runs wrangler (default the repository's node_modules/.bin/wrangler, because the
31
+ # commands run in the unpacked bundle under a temporary directory, where pnpm exec finds no wrangler;
32
+ # local mode: the stand-in)
28
33
  # DEPLOY_LOG_DIR where <env>.log is appended (default tmp/deploy-log)
29
34
  # ops/urls.json the URL pattern of each environment, see healthz.sh
30
35
  # GH_TOKEN a token that can read the Releases the lock names
31
- # CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID read by wrangler; set only by the workflow step that deploys
36
+ # CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID read by wrangler, and by the Custom Domain lookup of the probe (scripts/lib/edge-address.sh);
37
+ # set only by the workflow step that deploys
32
38
  set -euo pipefail
33
39
  env_name="${1:?usage: apply-ops.sh <env> [--only u1,u2]}"
34
40
  shift
@@ -41,6 +47,7 @@ root="$(git rev-parse --show-toplevel)"
41
47
  here="$(dirname "$0")"
42
48
  source "$here/../lib/bundle.sh"
43
49
  source "$here/../lib/ops-steps.sh"
50
+ source "$here/../lib/edge-address.sh"
44
51
  decl="$root/ops/versions.json"
45
52
  lock="$root/ops/versions.lock.json"
46
53
  [[ -f "$decl" ]] || { printf '%s\n' >&2 "找不到 $decl"; exit 1; }
@@ -49,7 +56,7 @@ lock="$root/ops/versions.lock.json"
49
56
  instances_filter='to_entries[] | select((.key | startswith("hosts/") | not) and (.key | endswith("/" + $e))) | .key as $k | (.value // {}) | keys[] | "\($k) \(.)"'
50
57
  jq -e --arg e "$env_name" 'to_entries | any(.[]; (.key | startswith("hosts/") | not) and (.key | endswith("/" + $e)))' "$decl" >/dev/null \
51
58
  || { printf '%s\n' >&2 "$decl 沒有 $env_name 環境的實例"; exit 1; }
52
- read -r -a wrangler <<<"${WRANGLER:-pnpm exec wrangler}"
59
+ read -r -a wrangler <<<"${WRANGLER:-$root/node_modules/.bin/wrangler}"
53
60
  log_dir="${DEPLOY_LOG_DIR:-$root/tmp/deploy-log}"
54
61
  mkdir -p "$log_dir"
55
62
  work="$(mktemp -d)"; trap 'rm -rf "$work"' EXIT
@@ -133,7 +140,18 @@ for step in "${steps[@]:-}"; do
133
140
  [[ -z "$(tail -c1 "$dir/bundle/wrangler.toml")" ]] || printf '\n' >> "$dir/bundle/wrangler.toml"
134
141
  cat "$settings" >> "$dir/bundle/wrangler.toml"
135
142
  cfg="$(sha256_of "$dir/bundle/wrangler.toml")"
136
- now="$(bash "$here/healthz.sh" "$worker" "$env_name" 2>/dev/null || true)"
143
+ # a Custom Domain that is declared and does not exist (or cannot be looked up) is not online: no probe, so no lookup of a new hostname
144
+ online=1
145
+ if [[ -n "${CLOUDFLARE_API_TOKEN:-}" && -n "${CLOUDFLARE_ACCOUNT_ID:-}" ]]; then
146
+ while IFS= read -r domain; do
147
+ [[ -n "$domain" ]] || continue
148
+ if ! edge_custom_domain "$domain" >/dev/null; then
149
+ online=0; printf '%s\n' " · ${worker}:Custom Domain ${domain} 還不存在,不探測 /healthz,直接部署"; break
150
+ fi
151
+ done < <(edge_declared_custom_domains "$dir/bundle/wrangler.toml" "$env_name")
152
+ fi
153
+ now=""
154
+ if (( online )); then now="$(bash "$here/healthz.sh" "$worker" "$env_name" 2>/dev/null || true)"; fi
137
155
  if [[ "$(jq -r .version <<<"${now:-{\}}" 2>/dev/null || true)" == "$version" && "$(jq -r .bundleSha256 <<<"${now:-{\}}" 2>/dev/null || true)" == "$sha" && "$(jq -r .configSha256 <<<"${now:-{\}}" 2>/dev/null || true)" == "$cfg" ]]; then
138
156
  printf '%s\n' " ⊘ $unit ${version}:$worker 已在線上,略過"; continue
139
157
  fi
@@ -141,15 +159,13 @@ for step in "${steps[@]:-}"; do
141
159
  entry_args=()
142
160
  if [[ -f "$dir/bundle/dist/index.js" ]]; then entry_args=(dist/index.js --no-bundle); fi
143
161
  if [[ -d "$dir/bundle/public" ]]; then entry_args+=(--assets public); fi
144
- out="$dir/upload.ndjson"
145
- (cd "$dir/bundle" && WRANGLER_OUTPUT_FILE_PATH="$out" "${wrangler[@]}" versions upload "${entry_args[@]}" --env "$env_name" \
162
+ # `wrangler deploy` and not `versions upload`: a Worker that does not exist yet (the first deploy) refuses `versions upload`. `deploy` uploads,
163
+ # moves 100% of traffic and applies the routes, custom domains and workers_dev of the settings in one step.
164
+ out="$dir/deploy.ndjson"
165
+ (cd "$dir/bundle" && WRANGLER_OUTPUT_FILE_PATH="$out" "${wrangler[@]}" deploy "${entry_args[@]}" --env "$env_name" \
146
166
  --message "$unit $version sha256:$sha" --var "BUNDLE_SHA256:$sha" --var "CONFIG_SHA256:$cfg")
147
- id="$(jq -r 'select(.type == "version-upload") | .version_id' "$out" 2>/dev/null | tail -1)"
148
- [[ -n "$id" ]] || { printf '%s\n' >&2 "✗ ${unit}:wrangler 的輸出檔沒有 version-upload 的 version_id"; exit 1; }
149
- (cd "$dir/bundle" && "${wrangler[@]}" versions deploy "$id@100" --env "$env_name" --yes --message "$unit $version")
150
- # `wrangler versions deploy` moves traffic to the version only. It does not apply routes, custom domains or workers_dev of the
151
- # settings, which `wrangler deploy` would; `wrangler triggers deploy` does, and it changes nothing when they are in place.
152
- (cd "$dir/bundle" && "${wrangler[@]}" triggers deploy --env "$env_name")
167
+ id="$(jq -r 'select(.type == "deploy") | .version_id' "$out" 2>/dev/null | tail -1)"
168
+ [[ -n "$id" ]] || { printf '%s\n' >&2 "✗ ${unit}:wrangler 的輸出檔沒有 deploy 的 version_id"; exit 1; }
153
169
  n=$(( n + 1 ))
154
170
  printf '%s\n' "$(date -u '+%Y-%m-%d %H:%M') | $env_name | $unit | $version | apply | $id | sha256:$sha | config:${cfg:0:12}" >> "$log_dir/$env_name.log"
155
171
  printf '%s\n' " [$n] $worker ← ${version}(版本 ${id},雜湊 ${sha:0:12}…,設定 ${cfg:0:12}…)"
@@ -5,6 +5,12 @@
5
5
  # reads: ops/urls.json { "<env>": "<base URL of a Worker, with {worker} where its name goes>" }
6
6
  # compose writes the file from the URL patterns that the ops unit of the repository publishes (the
7
7
  # interface of the unit), so the patterns are not a repository variable.
8
+ #
9
+ # A host that is not *.workers.dev is looked up as a Custom Domain of the account (CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID set). When it is one,
10
+ # the request goes to an edge address with curl --resolve (scripts/lib/edge-address.sh) and not through the resolver, because a hostname that was
11
+ # asked for before it existed is kept as NXDOMAIN for the SOA MINIMUM of its zone (up to 1800 seconds) and curl does not retry a failed lookup.
12
+ # When the host is no Custom Domain (a route of a Worker, a host of another DNS), or without the two variables, curl resolves it as usual.
13
+ # Only the JSON goes to stdout; the reason of a failure goes to stderr.
8
14
  set -euo pipefail
9
15
  worker="${1:?usage: healthz.sh <worker> <env>}"
10
16
  env_name="${2:?usage: healthz.sh <worker> <env>}"
@@ -13,4 +19,24 @@ file="$root/ops/urls.json"
13
19
  [[ -f "$file" ]] || { printf '%s\n' >&2 "找不到 ${file}:各環境的網址樣式由 compose 從 ops 單元公布的介面寫出"; exit 2; }
14
20
  pattern="$(jq -r --arg e "$env_name" '.[$e] // empty' "$file")"
15
21
  [[ -n "$pattern" ]] || { printf '%s\n' >&2 "$file 沒有 $env_name 的網址樣式"; exit 2; }
16
- curl -fsS --max-time 15 --retry 3 --retry-delay 2 "${pattern//\{worker\}/$worker}/healthz"
22
+ url="${pattern//\{worker\}/$worker}/healthz"
23
+ authority="${url#*://}"; authority="${authority%%/*}"
24
+ host="${authority%%:*}"
25
+ if [[ "$authority" == *:* ]]; then port="${authority##*:}"; elif [[ "$url" == http://* ]]; then port=80; else port=443; fi
26
+ resolve=()
27
+ if [[ "$host" != *.workers.dev && -n "${CLOUDFLARE_API_TOKEN:-}" && -n "${CLOUDFLARE_ACCOUNT_ID:-}" ]]; then
28
+ source "$(dirname "$0")/../lib/edge-address.sh"
29
+ status=0; record="$(edge_custom_domain "$host")" || status=$?
30
+ case "$status" in
31
+ 0)
32
+ zone="$(jq -r '.zone_id // empty' <<<"$record")"
33
+ ip="$(edge_address "$host" "$zone")" || {
34
+ printf '%s\n' >&2 "healthz: 找不到 ${host} 的 Cloudflare 位址:同 zone 的其他主機名和 ${host} 本身都解析不到。這個主機名可能剛建立,解析器依 zone 的 SOA MINIMUM 負向快取 NXDOMAIN,最多 1800 秒,過了再重跑"
35
+ exit 1
36
+ }
37
+ resolve=(--resolve "$host:$port:$ip") ;;
38
+ 1) ;;
39
+ *) printf '%s\n' >&2 "healthz: 查不到 ${host} 的 Custom Domain(Cloudflare API 呼叫失敗),改走一般 DNS" ;;
40
+ esac
41
+ fi
42
+ curl -fsS --max-time 15 --retry 3 --retry-delay 2 ${resolve[@]+"${resolve[@]}"} "$url"
@@ -10,7 +10,8 @@
10
10
  # when an older tag is rerun. plan-prod.sh compares the tag with what prod declares, so the rerun puts back every unit that differs.
11
11
  #
12
12
  # usage: smoke.sh <env> <unit>=<version> [<unit>=<version> ...]
13
- # env: DEPLOY_LOG_DIR (defaults to tmp/deploy-log), ops/urls.json (see healthz.sh)
13
+ # env: DEPLOY_LOG_DIR (defaults to tmp/deploy-log; healthz-<unit>-<env>.err there holds what healthz.sh wrote to stderr), ops/urls.json (see healthz.sh)
14
+ # CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID (optional) let healthz.sh reach a Custom Domain by an edge address, without the resolver
14
15
  set -euo pipefail
15
16
  env_name="${1:?usage: smoke.sh <env> <unit>=<version>...}"; shift
16
17
  (( $# )) || { printf '%s\n' >&2 "smoke: 至少要一個 <unit>=<version>"; exit 2; }
@@ -33,15 +34,18 @@ for spec in "$@"; do
33
34
  if [[ "$pinned" != "$version" ]]; then
34
35
  printf '%s\n' " ✗ ${unit}:ops 宣告釘的是 ${pinned:-(空)},預期版本 $version"; fail=1; failed+=("$unit"); continue
35
36
  fi
36
- probe="$(bash "$here/healthz.sh" "$unit-$env_name" "$env_name" 2>/dev/null || true)"
37
+ err_file="$log_dir/healthz-$unit-$env_name.err"
38
+ probe="$(bash "$here/healthz.sh" "$unit-$env_name" "$env_name" 2>"$err_file" || true)"
39
+ # why the probe failed, printed under the failure line of the unit
40
+ reason() { [[ -s "$err_file" ]] && sed 's/^/ 原因:/' "$err_file" | head -n 5 || true; }
37
41
  got_version="$(jq -r '.version // empty' <<<"${probe:-{\}}" 2>/dev/null || true)"
38
42
  got_sha="$(jq -r '.bundleSha256 // empty' <<<"${probe:-{\}}" 2>/dev/null || true)"
39
43
  got_config="$(jq -r '.configSha256 // empty' <<<"${probe:-{\}}" 2>/dev/null || true)"
40
44
  if [[ "$got_version" != "$version" || "$got_sha" != "$sha" ]]; then
41
- printf '%s\n' " ✗ ${unit}:/healthz 回報版號 ${got_version:-(無)}、雜湊 ${got_sha:-(無)},宣告是 ${version}、$sha"; fail=1; failed+=("$unit"); continue
45
+ printf '%s\n' " ✗ ${unit}:/healthz 回報版號 ${got_version:-(無)}、雜湊 ${got_sha:-(無)},宣告是 ${version}、$sha"; reason; fail=1; failed+=("$unit"); continue
42
46
  fi
43
47
  if [[ -z "$got_config" ]]; then
44
- printf '%s\n' " ✗ ${unit}:/healthz 沒有回報設定雜湊 configSha256"; fail=1; failed+=("$unit"); continue
48
+ printf '%s\n' " ✗ ${unit}:/healthz 沒有回報設定雜湊 configSha256"; reason; fail=1; failed+=("$unit"); continue
45
49
  fi
46
50
  printf '%s\n' " ✓ $unit ${version}(sha256:${sha:0:12}…,設定 ${got_config:0:12}…):宣告一致,/healthz 回報相同版號與 bundle 雜湊"
47
51
  printf '%s\n' "$(date -u '+%Y-%m-%d %H:%M') | $env_name | $unit | $version | smoke | ok" >> "$log_dir/smoke.log"
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env bash
2
+ # Find a Cloudflare edge address that serves a Custom Domain of a Worker, without asking the resolver about that hostname. Sourced by
3
+ # healthz.sh and apply-ops.sh; do not execute.
4
+ #
5
+ # Why: the first deploy of a Workers unit creates the hostname. A resolver that was asked for it earlier got NXDOMAIN and keeps that answer for the
6
+ # SOA MINIMUM of the zone (up to 1800 seconds), so a probe through DNS right after the deploy fails, and so does every browser on the same
7
+ # network. The hostnames of one zone that Cloudflare proxies resolve to the same addresses, so a hostname that already resolves gives an
8
+ # address that serves the new one too: curl --resolve <host>:443:<address> reaches the Worker without the resolver.
9
+ #
10
+ # needs: CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID (the Workers Custom Domain read of the token that wrangler uses), curl, jq
11
+ # env: CLOUDFLARE_API_BASE default https://api.cloudflare.com/client/v4
12
+ # EDGE_DOH_URL default https://cloudflare-dns.com/dns-query (DNS over HTTPS, JSON form)
13
+ # EDGE_ADDRESS_ATTEMPTS default 12, rounds of lookups before edge_address gives up
14
+ # EDGE_ADDRESS_INTERVAL default 5, seconds between two rounds (12 rounds of 5 seconds are about 60 seconds)
15
+ #
16
+ # edge_custom_domain <host> the Custom Domain record of <host> as one JSON line ({ hostname, zone_id, zone_name, service, enabled, ... });
17
+ # ends with 0 when it exists, 1 when it does not, 2 when the API call fails
18
+ # edge_zone_hosts <zone id> <host> the enabled Custom Domain hostnames of the zone except <host>, one per line; when the zone has none, the
19
+ # proxied DNS records of the zone (a call that fails is skipped)
20
+ # edge_doh <host> the first IPv4 address of <host> by DNS over HTTPS; only an AAAA answer gives [<ipv6>]; ends with 1 when none
21
+ # edge_address <host> [<zone id>] an address to give curl --resolve for <host>: the first one that a hostname of the same zone resolves to,
22
+ # then <host> itself; retries for about 60 seconds; ends with 1 when nothing resolves
23
+ # edge_declared_custom_domains <toml> <env> the hostnames that <toml> declares as `custom_domain = true` routes (inline tables or [[env.<env>.routes]])
24
+
25
+ # edge_api <path with query>: GET of the Cloudflare API; the body on stdout, a failed status makes it fail (curl -f)
26
+ edge_api() {
27
+ curl -fsS --max-time 15 -H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN:?}" -H "Accept: application/json" "${CLOUDFLARE_API_BASE:-https://api.cloudflare.com/client/v4}$1"
28
+ }
29
+
30
+ edge_custom_domain() {
31
+ local body out
32
+ body="$(edge_api "/accounts/${CLOUDFLARE_ACCOUNT_ID:?}/workers/domains?hostname=$1")" || return 2
33
+ out="$(jq -c --arg h "$1" '(.result // []) | map(select(.hostname == $h)) | .[0] // empty' <<<"$body" 2>/dev/null)" || return 2
34
+ [[ -n "$out" ]] || return 1
35
+ printf '%s\n' "$out"
36
+ }
37
+
38
+ edge_zone_hosts() {
39
+ local zone="$1" skip="$2" body hosts=""
40
+ if body="$(edge_api "/accounts/${CLOUDFLARE_ACCOUNT_ID:?}/workers/domains?zone_id=$zone")"; then
41
+ hosts="$(jq -r --arg x "$skip" '(.result // [])[] | select(.enabled != false and .hostname != $x) | .hostname' <<<"$body" 2>/dev/null || true)"
42
+ fi
43
+ if [[ -z "$hosts" ]] && body="$(edge_api "/zones/$zone/dns_records?proxied=true&per_page=100")"; then
44
+ hosts="$(jq -r --arg x "$skip" '(.result // [])[] | select(.proxied == true and .name != $x and (.type == "A" or .type == "AAAA" or .type == "CNAME")) | .name' <<<"$body" 2>/dev/null || true)"
45
+ fi
46
+ [[ -z "$hosts" ]] || printf '%s\n' "$hosts" | awk '!seen[$0]++'
47
+ return 0
48
+ }
49
+
50
+ edge_doh() {
51
+ local host="$1" type body ip
52
+ for type in A AAAA; do
53
+ body="$(curl -fsS --max-time 10 -H 'accept: application/dns-json' "${EDGE_DOH_URL:-https://cloudflare-dns.com/dns-query}?name=$host&type=$type" 2>/dev/null)" || continue
54
+ if [[ "$type" == A ]]; then
55
+ ip="$(jq -r '[.Answer[]? | select(.type == 1) | .data] | .[0] // empty' <<<"$body" 2>/dev/null || true)"
56
+ [[ -z "$ip" ]] || { printf '%s\n' "$ip"; return 0; }
57
+ else
58
+ ip="$(jq -r '[.Answer[]? | select(.type == 28) | .data] | .[0] // empty' <<<"$body" 2>/dev/null || true)"
59
+ [[ -z "$ip" ]] || { printf '[%s]\n' "$ip"; return 0; }
60
+ fi
61
+ done
62
+ return 1
63
+ }
64
+
65
+ edge_address() {
66
+ local host="$1" zone="${2:-}" record peers="" peer ip round attempts="${EDGE_ADDRESS_ATTEMPTS:-12}"
67
+ if [[ -z "$zone" ]] && record="$(edge_custom_domain "$host")"; then zone="$(jq -r '.zone_id // empty' <<<"$record")"; fi
68
+ [[ -z "$zone" ]] || peers="$(edge_zone_hosts "$zone" "$host")"
69
+ for (( round = 1; round <= attempts; round++ )); do
70
+ while IFS= read -r peer; do
71
+ [[ -n "$peer" ]] || continue
72
+ ip="$(edge_doh "$peer")" && { printf '%s\n' "$ip"; return 0; }
73
+ done <<<"$peers"
74
+ ip="$(edge_doh "$host")" && { printf '%s\n' "$ip"; return 0; }
75
+ (( round == attempts )) || sleep "${EDGE_ADDRESS_INTERVAL:-5}"
76
+ done
77
+ return 1
78
+ }
79
+
80
+ edge_declared_custom_domains() {
81
+ awk -v env="$2" '
82
+ function quoted(s, m) { if (match(s, /pattern[ \t]*=[ \t]*"[^"]*"/)) { m = substr(s, RSTART, RLENGTH); sub(/^[^"]*"/, "", m); sub(/"$/, "", m); return m } return "" }
83
+ function flush() { if (inroute && cd && pat != "") print pat; pat = ""; cd = 0 }
84
+ /^[ \t]*\[/ { flush(); inroute = ($0 ~ ("^[ \t]*\\[\\[env\\." env "\\.routes\\]\\][ \t]*$")); next }
85
+ inroute {
86
+ if ($0 ~ /^[ \t]*pattern[ \t]*=/) pat = quoted($0)
87
+ if ($0 ~ /^[ \t]*custom_domain[ \t]*=[ \t]*true/) cd = 1
88
+ next
89
+ }
90
+ {
91
+ rest = $0
92
+ while (match(rest, /\{[^}]*\}/)) {
93
+ obj = substr(rest, RSTART, RLENGTH); rest = substr(rest, RSTART + RLENGTH)
94
+ if (obj ~ /custom_domain[ \t]*=[ \t]*true/ && quoted(obj) != "") print quoted(obj)
95
+ }
96
+ }
97
+ END { flush() }' "$1"
98
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blueprint-shared-templates",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "A public reference implementation and specification of the shared templates, pinnable and readable, not a source to fetch files from",
5
5
  "license": "MIT",
6
6
  "author": "Carlos Space",
@@ -401,6 +401,10 @@ jobs:
401
401
  fetch-depth: 0
402
402
  fetch-tags: true
403
403
  persist-credentials: false
404
+ # 發布腳本經 retry.mjs 讀 GitHub,docker label 的 runner 可能是沒有 node 的 VM 映像。
405
+ - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
406
+ with:
407
+ node-version: 24
404
408
  - name: 推映像
405
409
  env:
406
410
  PATHS: ${{ needs.plan.outputs.paths }}
@@ -158,33 +158,53 @@ aggregate_unit() {
158
158
  # already shipped. The result is the newest cut that hides nothing:
159
159
  # - The candidates are the cuts on main, newest first. The merge-back commit of each release carries
160
160
  # Release-Train-Base (the cut of that release) and Release-Train-Units (the units of that train). A cut that is
161
- # not an ancestor of <base> (a patch train rebuilt from a tag) is skipped. A merge-back with no
162
- # Release-Train-Units covers every unit and ends the candidates.
161
+ # not an ancestor of <base> (a patch train rebuilt from a tag) is skipped and listed in RELEASE_SKIPPED; its
162
+ # merge-back commit, which is on main, stands for it as a candidate, so the start point can move past the commits
163
+ # the patch release shipped. That candidate is no unit's cut: the units of the patch train keep counting from the
164
+ # cut of an earlier release on main, so a commit of main the patch train did not carry still holds the start point
165
+ # back. A merge-back with no Release-Train-Units covers every unit and ends the candidates.
163
166
  # - A released unit (unit_released) counts from the newest cut that lists it (its cut). A candidate newer than
164
167
  # that cut suits the unit when the unit has no commit release-please would release (releasable_commits_in on its
165
- # path) between its cut and the candidate, because then nothing of it is hidden. A unit no cut lists counts from
166
- # the oldest candidate.
168
+ # path) between its cut and the candidate, because then nothing of it is hidden. A commit that already shipped does
169
+ # not count as one to release: it is an ancestor of the newest tag of the unit (<component>/v*, the highest semver,
170
+ # a prerelease before its release, read with release_unit_tag), or a commit of that tag's history says
171
+ # "(cherry picked from commit <sha>)" with its full sha, which is how a patch train rebuilt from a tag carries a
172
+ # fix of main. A unit without a readable tag counts every releasable commit. A unit no cut lists counts from the
173
+ # oldest candidate.
167
174
  # - No release lists a unit that has never been released, so a candidate suits such a unit when it is strictly
168
175
  # older than the unit's first commit: every commit of the unit then comes after the cut and counts toward its
169
176
  # first version.
170
177
  # When no candidate suits every unit it is the oldest candidate; with no cut at all it is empty (no previous
171
178
  # release). The walk has no depth limit: the cut is a fact of an earlier release, however far back.
172
179
  release_start_point() {
173
- local base="$1" u c b us row first i k n p
180
+ local base="$1" u c b mb us row first i k n p
174
181
  local -a released=() firsts=() cuts=() listed=() skipped_at=() skipped=()
175
- local -A covered_at=()
182
+ local -A covered_at=() latest_tag=() picked=()
176
183
  shift
177
184
  RELEASE_START=""; RELEASE_SKIPPED=()
178
185
  for u in "$@"; do
179
- if unit_released "$u"; then released+=("$u"); continue; fi
186
+ if unit_released "$u"; then
187
+ released+=("$u")
188
+ latest_tag[$u]="$(release_unit_tag "$u")"
189
+ [[ -z "${latest_tag[$u]}" ]] || picked[$u]="$(git log "refs/tags/${latest_tag[$u]}" --format=%B 2>/dev/null | sed -nE 's/.*\(cherry picked from commit ([0-9a-f]{40})\).*/\1/p')"
190
+ continue
191
+ fi
180
192
  first="$(git rev-list --reverse "$base" -- "$(unit_path "$u")" | sed -n 1p)"
181
193
  [[ -z "$first" ]] || firsts+=("$first")
182
194
  done
183
195
  while IFS= read -r row; do
196
+ mb="${row%%$'\x1f'*}"; row="${row#*$'\x1f'}"
184
197
  b="${row%%$'\x1f'*}"; us="${row#*$'\x1f'}"
185
198
  [[ "$b" =~ ^[0-9a-f]{7,40}$ ]] || continue
186
199
  c="$b"
187
- if ! git merge-base --is-ancestor "$c" "$base" 2>/dev/null; then skipped+=("$c"); skipped_at+=("${#cuts[@]}"); continue; fi
200
+ if ! git merge-base --is-ancestor "$c" "$base" 2>/dev/null; then
201
+ skipped+=("$c"); skipped_at+=("${#cuts[@]}")
202
+ # a patch train's cut is off main; its merge-back commit is on main and stands for it. It is only a candidate: its units
203
+ # keep counting from the cut of an earlier release on main, so a commit of main the patch train did not carry still counts
204
+ git merge-base --is-ancestor "$mb" "$base" 2>/dev/null || continue
205
+ cuts+=("$mb")
206
+ continue
207
+ fi
188
208
  cuts+=("$c")
189
209
  if [[ -z "$us" ]]; then
190
210
  for u in "${released[@]+"${released[@]}"}"; do [[ -n "${covered_at[$u]:-}" ]] || covered_at[$u]=$(( ${#cuts[@]} - 1 )); done
@@ -192,7 +212,7 @@ release_start_point() {
192
212
  fi
193
213
  read -r -a listed <<<"$us"
194
214
  for u in "${listed[@]}"; do [[ -n "${covered_at[$u]:-}" ]] || covered_at[$u]=$(( ${#cuts[@]} - 1 )); done
195
- done < <(git log origin/main --format='%(trailers:key=Release-Train-Base,valueonly,unfold,separator=%x20)%x1f%(trailers:key=Release-Train-Units,valueonly,unfold,separator=%x20)' 2>/dev/null || true)
215
+ done < <(git log origin/main --format='%H%x1f%(trailers:key=Release-Train-Base,valueonly,unfold,separator=%x20)%x1f%(trailers:key=Release-Train-Units,valueonly,unfold,separator=%x20)' 2>/dev/null || true)
196
216
  n=${#cuts[@]}
197
217
  (( n )) || return 0
198
218
  k=$(( n - 1 ))
@@ -201,7 +221,7 @@ release_start_point() {
201
221
  for u in "${released[@]+"${released[@]}"}"; do
202
222
  p="${covered_at[$u]:-$(( n - 1 ))}"
203
223
  (( i < p )) || continue
204
- releasable_commits_in "${cuts[$p]}..$c" "$(unit_path "$u")" && continue 2
224
+ release_unshipped_in "${cuts[$p]}..$c" "$(unit_path "$u")" "${latest_tag[$u]:-}" "${picked[$u]:-}" && continue 2
205
225
  done
206
226
  for first in "${firsts[@]+"${firsts[@]}"}"; do
207
227
  if [[ "$c" == "$first" ]] || ! git merge-base --is-ancestor "$c" "$first"; then continue 2; fi
@@ -217,6 +237,29 @@ release_start_point() {
217
237
  return 0
218
238
  }
219
239
 
240
+ # release_unit_tag <unit> → the newest tag of the unit (<unit>/v*), nothing when it has none. The highest semver wins, a
241
+ # prerelease below its release (versionsort.suffix). The tags are asked from origin first (the unit_released answer is the
242
+ # remote's), and a failed fetch leaves the tags already here.
243
+ release_unit_tag() {
244
+ read_retry git fetch --quiet origin "refs/tags/$1/v*:refs/tags/$1/v*" >/dev/null 2>&1 || true
245
+ git -c versionsort.suffix=- tag -l "$1/v*" --sort=-v:refname 2>/dev/null | sed -n 1p
246
+ }
247
+
248
+ # release_unshipped_in <range> <path> <tag> <picked shas> → succeeds when <range> has a commit release-please would release
249
+ # on <path> that <tag> did not ship yet. A commit shipped when it is an ancestor of <tag> or when <picked shas> (the
250
+ # cherry-picked-from shas in the history of <tag>, one per line) has it. With no <tag> it is releasable_commits_in.
251
+ release_unshipped_in() {
252
+ local range="$1" path="$2" tag="$3" picks="$4" x
253
+ [[ -n "$tag" ]] || { releasable_commits_in "$range" "$path"; return; }
254
+ while IFS= read -r x; do
255
+ [[ -n "$x" ]] || continue
256
+ git merge-base --is-ancestor "$x" "refs/tags/$tag" 2>/dev/null && continue
257
+ grep -qxF "$x" <<<"$picks" && continue
258
+ return 0
259
+ done < <(git log --format=%H -E --grep='^(feat|fix|perf|deps|revert)(\([^)]*\))?!?:' --grep='^BREAKING[ -]CHANGE:' "$range" -- "$path")
260
+ return 1
261
+ }
262
+
220
263
  # train_units_of <ref> → the units the train was opened with, space separated, from the newest
221
264
  # Release-Train-Units trailer reachable from <ref>; nothing when there is none
222
265
  train_units_of() {