blueprint-shared-templates 0.7.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.
- package/deploy/scripts/deploy/route-tag.sh +4 -0
- package/ops/.github/workflows/ops-deploy.yml +4 -2
- package/ops/scripts/deploy/apply-ops.sh +32 -12
- package/ops/scripts/deploy/healthz.sh +27 -1
- package/ops/scripts/deploy/smoke.sh +8 -4
- package/ops/scripts/lib/edge-address.sh +98 -0
- package/package.json +1 -1
- package/publish/.github/workflows/publish.yml +114 -1
- package/publish/scripts/release/binary-units.sh +272 -0
- package/publish/scripts/release/image-units.sh +171 -0
- package/publish/scripts/release/publish-units.sh +90 -21
- package/release/.github/workflows/release-please.yml +2 -1
- package/release/schemas/release-policy.schema.json +50 -0
- package/release/scripts/ci/check-release-policy.sh +28 -3
- package/release/scripts/lib/release-stages.sh +69 -10
- package/release/scripts/release/dispatch-publish.sh +12 -2
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
# wrangler.toml holds no environment (the settings of each environment live in ops)
|
|
11
11
|
# - anything else (package, sdk, mobile, desktop): nothing to deploy here. Packages are published by
|
|
12
12
|
# publish.yml, and native apps are not part of the first version (decision native-apps-not-in-first-version)
|
|
13
|
+
# A unit whose publish.to in the policy file is image publishes a container image and is skipped here, whatever its kind.
|
|
13
14
|
# The directory of the unit tells whether it is built here, because the policy kind alone does not: an InfraName unit is kind
|
|
14
15
|
# backend too. A unit under apps/ or services/ is kind=service (the deploy role builds it). A unit under infra/ only publishes an
|
|
15
16
|
# interface, and the cloud resources are created by the iac steps of ops (decision ops-declares-cloud-resources), so it is skipped.
|
|
@@ -42,6 +43,9 @@ if ! is_stage "$stage" || [[ "$stage" == ga ]]; then
|
|
|
42
43
|
skip_with "版號 $version 的預發布階段「${stage}」不是 ${RELEASE_STAGES[*]:0:3} 之一"
|
|
43
44
|
fi
|
|
44
45
|
policy_unit_exists "$unit" || skip_with "$unit 不在政策檔($(basename "$(policy_file)"))的 units 裡,不是發布單元"
|
|
46
|
+
# a unit that publishes a container image (publish.to image) is not deployed by its tag: publish.yml pushes the image, and
|
|
47
|
+
# the deployment mounts the configuration and pulls it
|
|
48
|
+
[[ "$(policy_unit_publish_to "$unit")" != image ]] || skip_with "$unit 發布容器映像(政策檔的 publish.to 是 image),不經 tag 部署,映像由 publish.yml 推到 ghcr"
|
|
45
49
|
|
|
46
50
|
case "$(policy_unit_kind "$unit")" in
|
|
47
51
|
service|backend|web)
|
|
@@ -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
|
|
10
|
-
#
|
|
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,9 +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
|
|
5
|
-
#
|
|
6
|
-
# hash of the joined settings is skipped, so a rerun
|
|
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
|
+
# 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. 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).
|
|
7
11
|
#
|
|
8
12
|
# Each environment is a separate Worker (<unit>-<env>), and a version id belongs to one Worker, so an
|
|
9
13
|
# environment never reuses the id of another: what carries over is the bundle, and the hash tells it is the same.
|
|
@@ -23,11 +27,14 @@
|
|
|
23
27
|
# place that has it, and it is added to the table when the settings are joined.
|
|
24
28
|
#
|
|
25
29
|
# usage: apply-ops.sh <env> [--only unit1,unit2]
|
|
26
|
-
# env: WRANGLER the command that runs wrangler (default
|
|
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)
|
|
27
33
|
# DEPLOY_LOG_DIR where <env>.log is appended (default tmp/deploy-log)
|
|
28
34
|
# ops/urls.json the URL pattern of each environment, see healthz.sh
|
|
29
35
|
# GH_TOKEN a token that can read the Releases the lock names
|
|
30
|
-
# CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID read by wrangler
|
|
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
|
|
31
38
|
set -euo pipefail
|
|
32
39
|
env_name="${1:?usage: apply-ops.sh <env> [--only u1,u2]}"
|
|
33
40
|
shift
|
|
@@ -40,6 +47,7 @@ root="$(git rev-parse --show-toplevel)"
|
|
|
40
47
|
here="$(dirname "$0")"
|
|
41
48
|
source "$here/../lib/bundle.sh"
|
|
42
49
|
source "$here/../lib/ops-steps.sh"
|
|
50
|
+
source "$here/../lib/edge-address.sh"
|
|
43
51
|
decl="$root/ops/versions.json"
|
|
44
52
|
lock="$root/ops/versions.lock.json"
|
|
45
53
|
[[ -f "$decl" ]] || { printf '%s\n' >&2 "找不到 $decl"; exit 1; }
|
|
@@ -48,7 +56,7 @@ lock="$root/ops/versions.lock.json"
|
|
|
48
56
|
instances_filter='to_entries[] | select((.key | startswith("hosts/") | not) and (.key | endswith("/" + $e))) | .key as $k | (.value // {}) | keys[] | "\($k) \(.)"'
|
|
49
57
|
jq -e --arg e "$env_name" 'to_entries | any(.[]; (.key | startswith("hosts/") | not) and (.key | endswith("/" + $e)))' "$decl" >/dev/null \
|
|
50
58
|
|| { printf '%s\n' >&2 "$decl 沒有 $env_name 環境的實例"; exit 1; }
|
|
51
|
-
read -r -a wrangler <<<"${WRANGLER
|
|
59
|
+
read -r -a wrangler <<<"${WRANGLER:-$root/node_modules/.bin/wrangler}"
|
|
52
60
|
log_dir="${DEPLOY_LOG_DIR:-$root/tmp/deploy-log}"
|
|
53
61
|
mkdir -p "$log_dir"
|
|
54
62
|
work="$(mktemp -d)"; trap 'rm -rf "$work"' EXIT
|
|
@@ -132,7 +140,18 @@ for step in "${steps[@]:-}"; do
|
|
|
132
140
|
[[ -z "$(tail -c1 "$dir/bundle/wrangler.toml")" ]] || printf '\n' >> "$dir/bundle/wrangler.toml"
|
|
133
141
|
cat "$settings" >> "$dir/bundle/wrangler.toml"
|
|
134
142
|
cfg="$(sha256_of "$dir/bundle/wrangler.toml")"
|
|
135
|
-
|
|
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
|
|
136
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
|
|
137
156
|
printf '%s\n' " ⊘ $unit ${version}:$worker 已在線上,略過"; continue
|
|
138
157
|
fi
|
|
@@ -140,12 +159,13 @@ for step in "${steps[@]:-}"; do
|
|
|
140
159
|
entry_args=()
|
|
141
160
|
if [[ -f "$dir/bundle/dist/index.js" ]]; then entry_args=(dist/index.js --no-bundle); fi
|
|
142
161
|
if [[ -d "$dir/bundle/public" ]]; then entry_args+=(--assets public); fi
|
|
143
|
-
|
|
144
|
-
|
|
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" \
|
|
145
166
|
--message "$unit $version sha256:$sha" --var "BUNDLE_SHA256:$sha" --var "CONFIG_SHA256:$cfg")
|
|
146
|
-
id="$(jq -r 'select(.type == "
|
|
147
|
-
[[ -n "$id" ]] || { printf '%s\n' >&2 "✗ ${unit}:wrangler 的輸出檔沒有
|
|
148
|
-
(cd "$dir/bundle" && "${wrangler[@]}" versions deploy "$id@100" --env "$env_name" --yes --message "$unit $version")
|
|
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; }
|
|
149
169
|
n=$(( n + 1 ))
|
|
150
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"
|
|
151
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
|
-
|
|
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
|
-
|
|
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.
|
|
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",
|
|
@@ -4,8 +4,10 @@ name: publish
|
|
|
4
4
|
# npm、PyPI、crates.io Trusted Publishing(OIDC),不讀任何 registry token
|
|
5
5
|
# GitHub Packages 私有套件,用 GITHUB_TOKEN
|
|
6
6
|
# Go、Swift 沒有 registry,mirror job 推到公開鏡像倉並打 tag
|
|
7
|
+
# 執行檔 政策檔 publish.to 是 binary 的單元,binary job 交叉編譯後發到公開發布倉的 GitHub release,GA 才更新 Homebrew formula
|
|
8
|
+
# 容器映像 政策檔 publish.to 是 image 的單元,image job 推到 ghcr,digest 附在來源倉單元 tag 的 Release
|
|
7
9
|
# npm 以階段名作 dist-tag,GA 才是 latest;PyPI 用 PEP 440 版號,crates.io 用 SemVer,兩者的預發布版預設都不會被安裝。
|
|
8
|
-
# 判斷集中在 scripts/release/publish-units.sh 與
|
|
10
|
+
# 判斷集中在 scripts/release/publish-units.sh、mirror-units.sh、binary-units.sh 與 image-units.sh。版號不屬於派送的階段時,
|
|
9
11
|
# plan job 在任何 registry 被寫入前就停下。
|
|
10
12
|
#
|
|
11
13
|
# 這是閉源倉庫以 Trusted Publishing 發公開套件的結構:build job 跑 self-hosted,打包 npm tgz、PyPI dist 並驗證
|
|
@@ -74,6 +76,8 @@ jobs:
|
|
|
74
76
|
tp: ${{ steps.reg.outputs.tp }}
|
|
75
77
|
mirror: ${{ steps.reg.outputs.mirror }}
|
|
76
78
|
mirrors: ${{ steps.reg.outputs.mirrors }}
|
|
79
|
+
binary: ${{ steps.reg.outputs.binary }}
|
|
80
|
+
image: ${{ steps.reg.outputs.image }}
|
|
77
81
|
steps:
|
|
78
82
|
- uses: actions/checkout@v7
|
|
79
83
|
with:
|
|
@@ -306,3 +310,112 @@ jobs:
|
|
|
306
310
|
echo "::error::no GitHub App token, so nothing can be pushed to the mirror repositories"; exit 1
|
|
307
311
|
fi
|
|
308
312
|
bash scripts/release/mirror-units.sh ${flags[@]+"${flags[@]}"} "${paths[@]}"
|
|
313
|
+
|
|
314
|
+
# 政策檔 publish.to 是 binary 的單元:依政策檔的平台交叉編譯,連同 checksum 發到公開發布倉的 GitHub release,GA 才更新
|
|
315
|
+
# Homebrew formula(binary-units.sh)。跑預設 label 的 self-hosted runner,用 setup-go 裝 go.mod 指定的版本。
|
|
316
|
+
# 寫發布倉用 GitHub App 的權杖,範圍只有發布倉的 contents: write。發布倉名稱是組織值 binaryReleaseRepository,只寫倉庫名,
|
|
317
|
+
# owner 和這個倉相同;組織沒填這個值時,第一步就停下並寫原因,不讓空的 repositories 把權杖開到整個組織。
|
|
318
|
+
# 一次一個寫入者,兩次發版不會搶同一個 formula。dry_run 不需要權杖。
|
|
319
|
+
binary:
|
|
320
|
+
needs: plan
|
|
321
|
+
if: needs.plan.outputs.binary == 'true'
|
|
322
|
+
runs-on: self-hosted-linux
|
|
323
|
+
timeout-minutes: 30
|
|
324
|
+
permissions:
|
|
325
|
+
contents: read
|
|
326
|
+
concurrency:
|
|
327
|
+
group: binary
|
|
328
|
+
cancel-in-progress: false
|
|
329
|
+
steps:
|
|
330
|
+
# 要 tag:binary-units.sh 讀單元 tag 的 commit。
|
|
331
|
+
- uses: actions/checkout@v7
|
|
332
|
+
with:
|
|
333
|
+
fetch-depth: 0
|
|
334
|
+
fetch-tags: true
|
|
335
|
+
persist-credentials: false
|
|
336
|
+
- id: release-repo
|
|
337
|
+
env:
|
|
338
|
+
BINARY_RELEASE_REPOSITORY: "@@binaryReleaseRepository@@"
|
|
339
|
+
PATHS: ${{ needs.plan.outputs.paths }}
|
|
340
|
+
run: |
|
|
341
|
+
if [[ -z "$BINARY_RELEASE_REPOSITORY" ]]; then
|
|
342
|
+
echo "::error::the organization gives no binaryReleaseRepository, so there is no repository to release the executables to"
|
|
343
|
+
exit 1
|
|
344
|
+
fi
|
|
345
|
+
mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
|
|
346
|
+
file=""
|
|
347
|
+
for p in "${paths[@]}"; do
|
|
348
|
+
if [[ "$(bash scripts/release/publish-units.sh --report "$p")" == "unit $p -> binary" ]]; then file="$p/go.mod"; break; fi
|
|
349
|
+
done
|
|
350
|
+
echo "go-mod=$file" >> "$GITHUB_OUTPUT"
|
|
351
|
+
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
|
|
352
|
+
with:
|
|
353
|
+
go-version-file: ${{ steps.release-repo.outputs.go-mod }}
|
|
354
|
+
cache: false
|
|
355
|
+
- id: app-token
|
|
356
|
+
if: ${{ !inputs.dry_run }}
|
|
357
|
+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
|
358
|
+
with:
|
|
359
|
+
client-id: ${{ vars.@@clientIdVariable@@ }}
|
|
360
|
+
private-key: ${{ secrets.@@privateKeySecret@@ }}
|
|
361
|
+
owner: ${{ github.repository_owner }}
|
|
362
|
+
repositories: "@@binaryReleaseRepository@@"
|
|
363
|
+
permission-contents: write
|
|
364
|
+
- name: 發布執行檔
|
|
365
|
+
env:
|
|
366
|
+
PATHS: ${{ needs.plan.outputs.paths }}
|
|
367
|
+
BINARY_RELEASE_TOKEN: ${{ steps.app-token.outputs.token }}
|
|
368
|
+
BINARY_RELEASE_REPOSITORY: "@@binaryReleaseRepository@@"
|
|
369
|
+
BINARY_RELEASE_OWNER: ${{ github.repository_owner }}
|
|
370
|
+
BINARY_OUT: ${{ runner.temp }}/binary
|
|
371
|
+
DRY_RUN: ${{ inputs.dry_run && '1' || '0' }}
|
|
372
|
+
run: |
|
|
373
|
+
mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
|
|
374
|
+
flags=()
|
|
375
|
+
if [[ "$DRY_RUN" == 1 ]]; then flags=(--dry-run)
|
|
376
|
+
elif [[ -z "$BINARY_RELEASE_TOKEN" ]]; then
|
|
377
|
+
echo "::error::no GitHub App token, so nothing can be released to the release repository"; exit 1
|
|
378
|
+
fi
|
|
379
|
+
bash scripts/release/binary-units.sh ${flags[@]+"${flags[@]}"} "${paths[@]}"
|
|
380
|
+
|
|
381
|
+
# 政策檔 publish.to 是 image 的單元:用單元目錄的 Dockerfile 依政策檔平台建置,推到 ghcr.io/<owner>/<單元名>:<版號>,
|
|
382
|
+
# 把 digest 附在來源倉單元 tag 的 Release 並印在摘要(image-units.sh)。要 docker,所以跑組織值 selfHostedRunnerLabels.docker
|
|
383
|
+
# 的 runner;組織沒填這個值時,image-units.sh 在沒有 docker 的 runner 上停下並寫原因。
|
|
384
|
+
# 推映像和附 digest 都用這個 job 的 GITHUB_TOKEN:GitHub App 沒有 packages 權限,Release 在這個倉,contents: write 就夠。
|
|
385
|
+
# DOCKER_CONFIG 放在 job 的暫存目錄,image-units.sh 結束時刪掉。套件第一次推送是私有,要公開時由人改一次,改了收不回。
|
|
386
|
+
image:
|
|
387
|
+
needs: plan
|
|
388
|
+
if: needs.plan.outputs.image == 'true'
|
|
389
|
+
runs-on: "@@dockerRunnerLabel@@"
|
|
390
|
+
timeout-minutes: 60
|
|
391
|
+
permissions:
|
|
392
|
+
contents: write
|
|
393
|
+
packages: write
|
|
394
|
+
concurrency:
|
|
395
|
+
group: image
|
|
396
|
+
cancel-in-progress: false
|
|
397
|
+
steps:
|
|
398
|
+
# 要 tag:image-units.sh 讀單元 tag 的 commit。
|
|
399
|
+
- uses: actions/checkout@v7
|
|
400
|
+
with:
|
|
401
|
+
fetch-depth: 0
|
|
402
|
+
fetch-tags: true
|
|
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
|
|
408
|
+
- name: 推映像
|
|
409
|
+
env:
|
|
410
|
+
PATHS: ${{ needs.plan.outputs.paths }}
|
|
411
|
+
GH_TOKEN: ${{ github.token }}
|
|
412
|
+
IMAGE_TOKEN: ${{ github.token }}
|
|
413
|
+
IMAGE_ACTOR: ${{ github.actor }}
|
|
414
|
+
IMAGE_OWNER: ${{ github.repository_owner }}
|
|
415
|
+
IMAGE_OUT: ${{ runner.temp }}/image
|
|
416
|
+
DRY_RUN: ${{ inputs.dry_run && '1' || '0' }}
|
|
417
|
+
run: |
|
|
418
|
+
mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
|
|
419
|
+
flags=()
|
|
420
|
+
if [[ "$DRY_RUN" == 1 ]]; then flags=(--dry-run); fi
|
|
421
|
+
bash scripts/release/image-units.sh ${flags[@]+"${flags[@]}"} "${paths[@]}"
|