blueprint-shared-templates 0.1.0 → 0.3.0

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.
Files changed (77) hide show
  1. package/base/.github/ISSUE_TEMPLATE/bug.yml +2 -2
  2. package/base/.github/ISSUE_TEMPLATE/hotfix.yml +1 -1
  3. package/base/.github/actions/setup-workspace/action.yml +2 -2
  4. package/base/.github/workflows/auto-merge.yml +4 -0
  5. package/base/.github/workflows/ci.yml +17 -1
  6. package/base/scripts/ci/run-affected.sh +6 -0
  7. package/base/scripts/lib/common.sh +1 -1
  8. package/base/scripts/lib/retry.mjs +36 -6
  9. package/base/scripts/merge/decide.mjs +32 -17
  10. package/base/scripts/setup/github-repo-settings.sh +1 -1
  11. package/deploy/.github/workflows/deploy-prerelease.yml +6 -50
  12. package/deploy/.github/workflows/deploy-prod.yml +103 -0
  13. package/deploy/scripts/deploy/plan-prod.sh +57 -0
  14. package/deploy/scripts/deploy/promote.sh +23 -7
  15. package/deploy/scripts/deploy/publish-bundle.sh +10 -1
  16. package/deploy/scripts/deploy/route-tag.sh +10 -12
  17. package/deploy/scripts/deploy/run-prerelease.sh +4 -7
  18. package/deploy/scripts/deploy/{run-production.sh → run-prod.sh} +15 -17
  19. package/deploy/scripts/deploy/writeback-ops.sh +71 -40
  20. package/facts-skeleton.mjs +1 -1
  21. package/hub/scripts/release/dispatch-release-lines.sh +25 -2
  22. package/hub/scripts/release/snapshot-members.sh +1 -1
  23. package/ops/.github/workflows/ops-deploy.yml +92 -11
  24. package/ops/schemas/targets.schema.json +209 -0
  25. package/ops/scripts/ci/check-targets.mjs +291 -0
  26. package/ops/scripts/deploy/apply-iac.sh +89 -0
  27. package/ops/scripts/deploy/apply-ops.sh +101 -48
  28. package/ops/scripts/deploy/ops-run.sh +50 -12
  29. package/ops/scripts/deploy/smoke.sh +35 -16
  30. package/ops/scripts/lib/ops-steps.sh +75 -0
  31. package/org/scripts/ci/check-org-values.mjs +91 -0
  32. package/package.json +2 -2
  33. package/publish/.github/workflows/publish.yml +12 -3
  34. package/publish/scripts/release/cleanup-private-packages.mjs +1 -1
  35. package/publish/scripts/release/mirror-units.sh +45 -5
  36. package/publish/scripts/release/publish-units.sh +28 -4
  37. package/release/.github/workflows/advance-release.yml +107 -0
  38. package/release/.github/workflows/close-release.yml +133 -0
  39. package/release/.github/workflows/merge-back.yml +82 -0
  40. package/release/.github/workflows/merge-release-pr.yml +82 -0
  41. package/release/.github/workflows/open-release-line.yml +14 -5
  42. package/release/.github/workflows/release-line-finalize.yml +5 -3
  43. package/release/.github/workflows/release-please.yml +99 -15
  44. package/release/.github/workflows/sync-release-tools.yml +91 -0
  45. package/release/release-policy.json +1 -1
  46. package/release/schemas/release-policy.schema.json +13 -3
  47. package/release/scripts/ci/branch-exists.sh +27 -0
  48. package/release/scripts/ci/check-release-policy.sh +13 -2
  49. package/release/scripts/ci/pin-train.sh +5 -3
  50. package/release/scripts/ci/release-train.sh +22 -3
  51. package/release/scripts/lib/release-plan.sh +3 -1
  52. package/release/scripts/lib/release-pr.sh +275 -11
  53. package/release/scripts/lib/release-stages.sh +265 -36
  54. package/release/scripts/release/check-trigger-actor.sh +32 -0
  55. package/release/scripts/release/close-ready.mjs +72 -0
  56. package/release/scripts/release/close-ready.sh +70 -0
  57. package/release/scripts/release/cut-release.sh +35 -4
  58. package/release/scripts/release/ensure-stage-deploys.sh +3 -3
  59. package/release/scripts/release/finalize-release.sh +47 -15
  60. package/release/scripts/release/merge-back-ready.sh +27 -0
  61. package/release/scripts/release/merge-release-pr.sh +34 -107
  62. package/release/scripts/release/open-release-line.sh +28 -8
  63. package/release/scripts/release/pin-release-pr.sh +53 -0
  64. package/release/scripts/release/product-release-notes.sh +28 -0
  65. package/release/scripts/release/sync-release-tools.sh +137 -0
  66. package/release/scripts/release/{verify-production.sh → verify-prod.sh} +11 -10
  67. package/transform.mjs +3 -4
  68. package/verify/.github/workflows/{verify-production.yml → verify-prod.yml} +7 -7
  69. package/verify/scripts/release/verify-ops.sh +4 -4
  70. package/deploy/.github/workflows/deploy-production.yml +0 -173
  71. package/deploy/scripts/deploy/plan-production.sh +0 -43
  72. package/infra/.github/workflows/deploy-prerelease.yml +0 -157
  73. package/infra/.github/workflows/deploy-production.yml +0 -173
  74. package/infra/scripts/deploy/apply-infra.sh +0 -46
  75. package/infra/scripts/deploy/apply-prerelease.sh +0 -22
  76. package/infra/scripts/deploy/apply-production.sh +0 -33
  77. package/infra/scripts/deploy/route-tag.sh +0 -65
@@ -1,20 +1,20 @@
1
1
  #!/usr/bin/env bash
2
2
  # Decide where a unit tag deploys. Prints key=value lines (usable as $GITHUB_OUTPUT):
3
- # unit, version, stage, env, kind (service|infra|none), skip (true|false), reason
3
+ # unit, version, stage, env, kind (service|none), skip (true|false), reason
4
4
  #
5
5
  # A prerelease tag {unit}/vX.Y.Z-<stage>.N goes where the policy file (release-policy.json) sends that stage
6
6
  # for that unit: the unit's own targets, else the repository targets (stage_target in
7
7
  # scripts/lib/release-stages.sh). The unit's kind in the policy file says how it deploys:
8
- # - service, backend, web: the target is an environment, and the unit needs an [env.<env>] table in its
9
- # wrangler.toml to deploy there (the Worker <name>-<env>)
8
+ # - service, backend, web: the target is an environment. Whether ops has settings for the unit in that environment
9
+ # (ops/workloads/<purpose>/<env>/<unit>/) is checked where the declaration is written (promote.sh), and the App's own
10
+ # wrangler.toml holds no environment (the settings of each environment live in ops)
10
11
  # - anything else (package, sdk, mobile, desktop): nothing to deploy here. Packages are published by
11
12
  # publish.yml, and native apps are not part of the first version (decision native-apps-not-in-first-version)
12
- # The directory of the unit tells which job of the workflow takes it, because the policy kind alone does not: an InfraName unit is
13
- # kind backend too. A unit under apps/ or services/ is kind=service (the deploy role builds it), a unit under infra/ is kind=infra
14
- # (the infra role applies it). A repository with both roles has this one file and both jobs; each job runs for its own kind
15
- # (decision monorepo-release-align).
13
+ # The directory of the unit tells whether it is built here, because the policy kind alone does not: an InfraName unit is kind
14
+ # backend too. A unit under apps/ or services/ is kind=service (the deploy role builds it). A unit under infra/ only publishes an
15
+ # interface, and the cloud resources are created by the iac steps of ops (decision ops-declares-cloud-resources), so it is skipped.
16
16
  # The deploy is skipped, with the reason printed, when the unit has nothing to deploy to at that stage.
17
- # A stable tag never deploys here: the product tag triggers it (deploy-production.yml).
17
+ # A stable tag never deploys here: the product tag triggers it (deploy-prod.yml).
18
18
  #
19
19
  # usage: route-tag.sh <tag>
20
20
  # env: RELEASE_POLICY the policy file to read (default release-policy.json at the repository root)
@@ -48,13 +48,11 @@ case "$(policy_unit_kind "$unit")" in
48
48
  path="$(unit_path "$unit" 2>/dev/null || true)"
49
49
  case "${path%%/*}" in
50
50
  apps|services) kind=service ;;
51
- infra) kind=infra ;;
52
- *) skip_with "$unit 的目錄(${path:-找不到})不在 apps/、services/ 或 infra/ 之下,不是部署或套用單元" ;;
51
+ infra) skip_with "$unit 在 infra/ 之下,只發布介面,雲端資源由 ops 的 iac 步驟建立,這裡不部署" ;;
52
+ *) skip_with "$unit 的目錄(${path:-找不到})不在 apps/ 或 services/ 之下,不是部署單元" ;;
53
53
  esac
54
54
  env="$(stage_target "$unit" "$stage" || true)"
55
55
  [[ -n "$env" ]] || skip_with "$unit 沒有 $stage 階段的去處(政策檔 units.$unit.targets 與 targets),略過部署"
56
- grep -q "^\[env\.${env}\]" "$REPO_ROOT/$path/wrangler.toml" 2>/dev/null \
57
- || skip_with "$unit 沒有 $env 環境的設定($path/wrangler.toml 的 [env.$env]),略過 $env 部署"
58
56
  ;;
59
57
  *)
60
58
  kind=none
@@ -23,7 +23,7 @@ branch="${DEFAULT_BRANCH:-main}"
23
23
  route="$(bash "$here/route-tag.sh" "$tag")"
24
24
  get() { sed -n "s/^$1=//p" <<<"$route"; }
25
25
  if [[ "$(get skip)" == true ]]; then info "⊘ $tag 無需部署:$(get reason)"; exit 0; fi
26
- # a unit under infra/ is applied by the infra role (apply-prerelease.sh), never built here
26
+ # a unit under infra/ only publishes an interface, so route-tag.sh skips it and nothing is built here
27
27
  if [[ "$(get kind)" != service ]]; then info "⊘ $tag 不是這裡部署的單元(kind $(get kind))"; exit 0; fi
28
28
  unit="$(get unit)" version="$(get version)" env_name="$(get env)"
29
29
 
@@ -37,13 +37,10 @@ if [[ -n "${OPS_REPO:-}" ]]; then
37
37
  ops_root="$out/ops"
38
38
  bash "$here/clone-ops.sh" "$ops_root"
39
39
  branch="$(git -C "$ops_root" symbolic-ref --short HEAD)"
40
- else
41
- # a tag checkout has the ops/ of release time; take the declaration from the default branch
42
- git fetch --no-tags origin "$branch"
43
- git checkout FETCH_HEAD -- ops
44
40
  fi
45
- bash "$here/promote.sh" "$env_name" "$unit" "$version" "$sha" --repo "$repo" --tag "$tag" --asset "$asset" ${ops_root:+--ops-root "$ops_root"}
46
- OPS_ROOT="$ops_root" DEFAULT_BRANCH="$branch" bash "$here/writeback-ops.sh" "chore(ops): $unit $version 寫入 $env_name 宣告"
41
+ # writeback-ops.sh promotes on the newest default branch (a tag checkout has the ops/ of release time) and retries on a lost race
42
+ OPS_ROOT="$ops_root" DEFAULT_BRANCH="$branch" bash "$here/writeback-ops.sh" "chore(ops): $unit $version 寫入 $env_name 宣告" \
43
+ --promote "$env_name" "$unit" "$version" "$sha" --repo "$repo" --tag "$tag" --asset "$asset"
47
44
  if [[ -z "$ops_root" ]]; then
48
45
  gh workflow run ops-deploy.yml --ref "$branch" -f env="$env_name" -f units="$unit"
49
46
  fi
@@ -1,48 +1,46 @@
1
1
  #!/usr/bin/env bash
2
- # What deploy-production.yml does for a product tag vX.Y.Z. Only a product tag puts production online: the
2
+ # What deploy-prod.yml does for a product tag vX.Y.Z. Only a product tag puts prod online: the
3
3
  # stable tag of a single unit never does (git-flow-template release.ts:130-134). The units of the release come from
4
- # plan-production.sh. Each one is rebuilt from the product tag at its GA version, because the version is part of the
4
+ # plan-prod.sh, which compares the tag with the prod declaration of ops. Each one is rebuilt from the product tag at its GA version, because the version is part of the
5
5
  # bundle (it is in package.json and in /healthz), so the bundle of the rc is not the one that ships (decision
6
- # ga-rebuild-bundle). Then the bundles are written into the production declaration in one commit.
6
+ # ga-rebuild-bundle). Then the bundles are written into the prod declaration in one commit.
7
7
  # This script never calls wrangler and never reads a Cloudflare credential.
8
8
  #
9
- # usage: run-production.sh <product tag> the tag must be checked out
9
+ # usage: run-prod.sh <product tag> the tag must be checked out
10
10
  # env: the same as run-prerelease.sh
11
11
  set -euo pipefail
12
12
  source "$(dirname "$0")/../lib/common.sh"
13
13
  cd "$REPO_ROOT"
14
14
  here="$(dirname "$0")"
15
- tag="${1:?usage: run-production.sh <product tag>}"
15
+ tag="${1:?usage: run-prod.sh <product tag>}"
16
16
  branch="${DEFAULT_BRANCH:-main}"
17
17
  # a private repository on GitHub Free has no deployment branch and tag rules for environments, so the tag is
18
18
  # checked here; this is in the workflow file, and whoever can change the file can bypass it
19
- [[ "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "production 只由產品 tag(vX.Y.Z)部署,收到 $tag"
20
- plan="$(bash "$here/plan-production.sh" "$tag")"
21
- services="$(sed -n 's/^services=//p' <<<"$plan")"
22
- n="$(jq 'length' <<<"$services")"
23
- if [[ "$n" == 0 ]]; then info "⊘ $tag 沒有要部署的單元,略過"; exit 0; fi
24
- info "本版入版服務 $n 個:$services"
25
-
19
+ [[ "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "prod 只由產品 tag(vX.Y.Z)部署,收到 $tag"
26
20
  out="${BUNDLE_DIR:-$(mktemp -d)}"
27
21
  ops_root=""
28
22
  if [[ -n "${OPS_REPO:-}" ]]; then
29
23
  ops_root="$out/ops"
30
24
  bash "$here/clone-ops.sh" "$ops_root"
31
25
  branch="$(git -C "$ops_root" symbolic-ref --short HEAD)"
32
- else
33
- git fetch --no-tags origin "$branch"
34
- git checkout FETCH_HEAD -- ops
35
26
  fi
27
+ # the plan compares the tag with the prod declaration of ops as it is now, so the clone comes first
28
+ plan="$(bash "$here/plan-prod.sh" "$tag" ${ops_root:+--ops-root "$ops_root"})"
29
+ services="$(sed -n 's/^services=//p' <<<"$plan")"
30
+ n="$(jq 'length' <<<"$services")"
31
+ if [[ "$n" == 0 ]]; then info "⊘ $tag 沒有要部署的單元(ops 宣告已和 $tag 一致),略過"; exit 0; fi
32
+ info "本版入版服務 $n 個:$services"
36
33
  envs=""
34
+ declare -a promotes=() # the promote.sh arguments of every unit, applied by writeback-ops.sh on the newest default branch
37
35
  for i in $(seq 0 $((n - 1))); do
38
36
  unit="$(jq -r ".[$i].unit" <<<"$services")" version="$(jq -r ".[$i].version" <<<"$services")" env_name="$(jq -r ".[$i].env" <<<"$services")"
39
37
  built="$(bash "$here/build-bundle.sh" "$unit" "$version" --out "$out")"
40
38
  bundle="$(sed -n 's/^bundle=//p' <<<"$built")" asset="$(sed -n 's/^asset=//p' <<<"$built")" sha="$(sed -n 's/^sha256=//p' <<<"$built")"
41
39
  repo="$(bash "$here/publish-bundle.sh" "$tag" "$bundle" | sed -n 's/^repo=//p')"
42
- bash "$here/promote.sh" "$env_name" "$unit" "$version" "$sha" --repo "$repo" --tag "$tag" --asset "$asset" ${ops_root:+--ops-root "$ops_root"}
40
+ promotes+=(--promote "$env_name" "$unit" "$version" "$sha" --repo "$repo" --tag "$tag" --asset "$asset")
43
41
  [[ ",$envs," == *",$env_name,"* ]] || envs="${envs:+$envs,}$env_name"
44
42
  done
45
- OPS_ROOT="$ops_root" DEFAULT_BRANCH="$branch" bash "$here/writeback-ops.sh" --require-change "chore(ops): 產品 ${tag} 的入版服務寫入 ${envs} 宣告"
43
+ OPS_ROOT="$ops_root" DEFAULT_BRANCH="$branch" bash "$here/writeback-ops.sh" --require-change "chore(ops): 產品 ${tag} 的入版服務寫入 ${envs} 宣告" "${promotes[@]}"
46
44
  if [[ -z "$ops_root" ]]; then
47
45
  for e in ${envs//,/ }; do
48
46
  units="$(jq -r --arg e "$e" '[.[] | select(.env == $e) | .unit] | join(",")' <<<"$services")"
@@ -1,53 +1,84 @@
1
1
  #!/usr/bin/env bash
2
- # Commit the promoted ops/ declaration back to the default branch of the repository that holds it: this
3
- # repository (monorepo), or a clone of poly-ops (OPS_ROOT). Shared by deploy-prerelease and deploy-production
4
- # so the retry-on-concurrent-push logic lives in one place. A retry loop that swallows every failure would
5
- # report "deployed" while the declaration never changed (observed 2026-09-08), so exhausted retries fail
6
- # the step.
2
+ # Commit the promoted ops/ declaration to the default branch of the repository that holds it: this
3
+ # repository (monorepo), or a clone of poly-ops (OPS_ROOT). Shared by deploy-prerelease and deploy-prod
4
+ # so the retry-on-concurrent-push logic lives in one place.
7
5
  #
8
- # The promoted declaration is an absolute desired state, not a patch, so it is carried across by
9
- # copying the files, never by stash/pop. These jobs check out a tag and then restore ops/ from the
10
- # default branch, so the working tree's diff against the tag contains both that restore and the
11
- # promotion; replaying it onto the default branch re-applies changes that branch already has and
12
- # conflicts on the very lines being promoted (observed 2026-09-09: v1.6.0's production writeback
13
- # died on a merge conflict in ops/charts/product-web/values-production.yaml, skipping apply and smoke).
6
+ # Every round starts from the newest origin/<branch> and runs this deploy's promotions again (promote.sh),
7
+ # then commits and pushes. A promotion is an absolute desired state that replaces the whole entry of one
8
+ # unit, so running it on a newer branch leaves the same entry for this unit and keeps the entries that
9
+ # concurrent deploys wrote for other units. Replaying the commit (git pull --rebase) is not used because
10
+ # concurrent deploys edit the same ops/versions.json and ops/versions.lock.json, and the rebase conflicts
11
+ # on those lines. Carrying the working tree across (stash, copying files) is not used because a job checks
12
+ # out a tag and restores ops/ from the default branch, so the tree's diff mixes the restore with the
13
+ # promotion and conflicts on the lines being promoted.
14
14
  #
15
- # usage: writeback-ops.sh [--require-change] "<commit message>" (env: DEFAULT_BRANCH, OPS_ROOT)
16
- # --require-change no diff under ops/ is an error (production: an empty promotion means the
17
- # scope derivation went wrong); without it, no diff is a no-op (prerelease re-runs)
18
- # OPS_ROOT the checkout whose ops/ was promoted (default: this repository)
15
+ # A monorepo deploys several units at once and each round lets one of them push, so the number of rounds
16
+ # has to cover the units that write back together (WRITEBACK_TRIES, 6). The pause between two rounds is
17
+ # WRITEBACK_RETRY_SLEEP plus a random part of at most WRITEBACK_RETRY_JITTER seconds, so writers that lost
18
+ # the same round do not fetch and push again at the same moment.
19
+ #
20
+ # A retry loop that swallows every failure would report "deployed" while the declaration never changed,
21
+ # so losing every round fails the step.
22
+ #
23
+ # usage: writeback-ops.sh [--require-change] "<commit message>" --promote <promote.sh arguments> [--promote ...]
24
+ # (env: DEFAULT_BRANCH, OPS_ROOT, WRITEBACK_TRIES, WRITEBACK_RETRY_SLEEP, WRITEBACK_RETRY_JITTER)
25
+ # --promote the arguments of one promote.sh call (<env> <unit> <version> <sha256> --repo ... --tag ... --asset ...),
26
+ # given once per promotion, up to the next --promote. --ops-root is added from OPS_ROOT.
27
+ # --require-change no diff under ops/ in the first round is an error (prod: an empty promotion means the
28
+ # scope derivation went wrong); without it, no diff is a no-op (prerelease re-runs).
29
+ # In a later round no diff means another deploy already wrote the same content: success.
30
+ # OPS_ROOT the checkout whose ops/ is promoted (default: this repository)
31
+ # WRITEBACK_TRIES how many rounds to try (default 6)
32
+ # WRITEBACK_RETRY_SLEEP seconds to wait after a lost round (default 5)
33
+ # WRITEBACK_RETRY_JITTER the most seconds added at random to that wait (default 5); 0 adds none
19
34
  set -euo pipefail
35
+ here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
20
36
  require=0; [[ "${1:-}" == --require-change ]] && { require=1; shift; }
21
- msg="${1:?usage: writeback-ops.sh [--require-change] <commit message>}"
37
+ msg="${1:?usage: writeback-ops.sh [--require-change] <commit message> --promote <promote.sh arguments>...}"
38
+ shift
39
+ # all promote arguments in one flat array; starts[k] and counts[k] locate the k-th promotion (no eval, no joined strings)
40
+ declare -a flat=() starts=() counts=()
41
+ for a in "$@"; do
42
+ if [[ "$a" == --promote ]]; then starts+=("${#flat[@]}"); continue; fi
43
+ (( ${#starts[@]} > 0 )) || { printf '%s\n' >&2 "::error::第一個參數之後要先寫 --promote"; exit 2; }
44
+ flat+=("$a")
45
+ done
46
+ (( ${#starts[@]} > 0 )) || { printf '%s\n' >&2 "::error::writeback-ops.sh 至少要一個 --promote"; exit 2; }
47
+ for k in "${!starts[@]}"; do
48
+ next=${starts[k+1]:-${#flat[@]}}
49
+ counts+=("$(( next - starts[k] ))")
50
+ (( counts[k] > 0 )) || { printf '%s\n' >&2 "::error::--promote 後面沒有參數"; exit 2; }
51
+ done
22
52
  branch="${DEFAULT_BRANCH:?DEFAULT_BRANCH is required}"
23
53
  root="${OPS_ROOT:-$(git rev-parse --show-toplevel)}"
24
54
  g() { git -C "$root" "$@"; }
25
55
 
26
- g fetch --no-tags origin "$branch"
27
- # What this job promoted, relative to the branch it is about to write to, not relative to the
28
- # checked-out tag. Files identical to the branch are left alone so a re-run is a clean no-op.
29
- declare -a changed=()
30
- while IFS= read -r line; do [[ -n "$line" ]] && changed+=("$line"); done < <(g diff --name-only "origin/$branch" -- ops)
31
- if (( ${#changed[@]} == 0 )); then
32
- (( require )) && { printf '%s\n' >&2 "::error::回寫時 ops 相對 origin/$branch 無變更"; exit 1; }
33
- printf '%s\n' "ops 相對 origin/$branch 無變更"; exit 0
34
- fi
35
- printf '%s\n' "回寫 ${#changed[@]} 個檔案:${changed[*]}"
36
-
37
- staged="$(mktemp -d)"
38
- for f in "${changed[@]}"; do mkdir -p "$staged/$(dirname "$f")"; cp "$root/$f" "$staged/$f"; done
39
- g checkout -q -f -B "$branch" "origin/$branch"
40
- for f in "${changed[@]}"; do mkdir -p "$root/$(dirname "$f")"; cp "$staged/$f" "$root/$f"; done
41
- rm -rf "$staged"
42
-
43
56
  g config user.name "github-actions[bot]"
44
57
  g config user.email "41898282+github-actions[bot]@users.noreply.github.com"
45
- g add ops
46
- if g diff --cached --quiet -- ops; then printf '%s\n' "宣告已是目標狀態,無需回寫"; exit 0; fi
47
- g commit -q -m "$msg"
48
- # 併發部署可能同時回寫,重試時先取回遠端最新狀態
49
- for i in 1 2 3; do
50
- if g pull --rebase origin "$branch" && g push origin "HEAD:$branch"; then exit 0; fi
51
- printf '%s\n' "回寫重試 $i"; sleep "${WRITEBACK_RETRY_SLEEP:-5}"
58
+
59
+ # Reset ops to the newest branch and run every promotion on it. Returns 0 with changes staged, 10 with none.
60
+ # The caller runs this inside ||, where set -e is off, so every step returns on its own failure.
61
+ reapply() {
62
+ local k
63
+ g fetch --no-tags origin "$branch" || return 1
64
+ g checkout -q -f -B "$branch" "origin/$branch" || return 1
65
+ for k in "${!starts[@]}"; do
66
+ bash "$here/promote.sh" "${flat[@]:starts[k]:counts[k]}" --ops-root "$root" || return 1
67
+ done
68
+ g add ops || return 1
69
+ if g diff --cached --quiet -- ops; then return 10; fi
70
+ }
71
+
72
+ for ((i=1; i<=${WRITEBACK_TRIES:-6}; i++)); do
73
+ rc=0; reapply || rc=$?
74
+ if (( rc == 10 )); then
75
+ if (( i == 1 && require )); then printf '%s\n' >&2 "::error::回寫時 ops 相對 origin/$branch 無變更"; exit 1; fi
76
+ printf '%s\n' "ops 相對 origin/$branch 無變更"; exit 0
77
+ fi
78
+ (( rc == 0 )) || exit "$rc"
79
+ g commit -q -m "$msg"
80
+ if g push origin "HEAD:$branch"; then exit 0; fi
81
+ printf '%s\n' "回寫重試 $i"
82
+ sleep "$(( ${WRITEBACK_RETRY_SLEEP:-5} + RANDOM % (${WRITEBACK_RETRY_JITTER:-5} + 1) ))"
52
83
  done
53
84
  printf '%s\n' >&2 "::error::ops 回寫推送失敗"; exit 1
@@ -71,7 +71,7 @@ export function factsSkeleton({ repoName, repoUrl, access, metaFactName, product
71
71
  );
72
72
  if (c.unit) out.set(`facts/${c.name}/fact-kit.config.json`, json({ implDirs: [`../../${c.unit.dir}`] }));
73
73
  const publish = c.unit?.publish ? ` publish: { target: '${c.unit.publish.target}', name: '${c.unit.publish.name}' },\n` : "";
74
- // Every facts entry declares its layer (blueprint.doc-layers.layers): a unit's facts are the implementation
74
+ // Every facts entry declares its layer (blueprint-shared.doc-layers.layers): a unit's facts are the implementation
75
75
  // layer, the product-level spec is the product layer.
76
76
  out.set(
77
77
  `facts/${c.name}/facts/${c.name}/index.ts`,
@@ -6,6 +6,7 @@
6
6
  # is sent a workflow_dispatch of open-release-line.yml with the product version, its units and the content of the
7
7
  # plan. A member with none of the units is not sent anything, so it opens no release branch and its product tag
8
8
  # skips the number. The token that can start workflows of the members lives only in this repository.
9
+ # A rerun after a dispatch that stopped halfway sends only to the members that have no release branch yet.
9
10
  #
10
11
  # The plan names its units on one line, "- 入版單元:a, b" or "- 這次發布的單元:a, b" (names separated by commas).
11
12
  #
@@ -18,7 +19,7 @@ dry=0
18
19
  while [[ $# -gt 0 ]]; do
19
20
  case "$1" in --dry-run) dry=1; shift ;; *) die "unknown argument: $1" ;; esac
20
21
  done
21
- policy_require
22
+ policy_require_product dispatch-release-lines.sh
22
23
  dir="$(plan_dir)"
23
24
  latest="$(plan_versions "$dir" | tail -1)"
24
25
  [[ -n "$latest" ]] || die "${dir} 沒有任何 Release Plan"
@@ -33,8 +34,12 @@ mapfile -t members < <(jq -r '.product.members[]?' "$(policy_file)")
33
34
  (( ${#members[@]} )) || die "政策檔的 product.members 是空的"
34
35
  declare -A units_of
35
36
  declare -A owner_of
37
+ # gh prints only "Not Found (HTTP 404)" on a failed read, which does not say which member or path it was, so a failed
38
+ # read stops with the member, the path and everything gh printed.
36
39
  for m in "${members[@]}"; do
37
- policy="$(gh api -H 'Accept: application/vnd.github.raw+json' "repos/$m/contents/release-policy.json")"
40
+ policy_path="repos/$m/contents/release-policy.json"
41
+ policy="$(gh api -H 'Accept: application/vnd.github.raw+json' "$policy_path" 2>&1)" \
42
+ || die "讀不到成員倉 ${m} 的政策檔(gh api ${policy_path}):${policy}"
38
43
  for u in "${plan_units[@]}"; do
39
44
  if jq -e --arg u "$u" '(.units // {}) | has($u)' <<<"$policy" >/dev/null; then
40
45
  [[ -z "${owner_of[$u]:-}" ]] || die "單元 $u 同時在 ${owner_of[$u]} 與 $m 的政策檔"
@@ -44,10 +49,28 @@ for m in "${members[@]}"; do
44
49
  done
45
50
  for u in "${plan_units[@]}"; do [[ -n "${owner_of[$u]:-}" ]] || die "plan 的單元 $u 不在任何成員倉的政策檔(product.members:${members[*]})"; done
46
51
 
52
+ # 成員倉的發布分支是 release/vX.Y,X.Y 取自產品版號。派送中途失敗後重跑時,已開好線的成員倉再收到一次派送,
53
+ # 它的 open-release-line.sh 會因為分支已存在而報錯,所以先問成員倉有沒有這條分支,有就略過。讀不到分支清單
54
+ # (權杖、網路、API 錯誤)不當成「還沒開」:停下來,不派送。
55
+ release_branch="release/v${plan#v}"
56
+ release_branch="${release_branch%.*}"
57
+ line_is_open() { # line_is_open <member> → success when the member has the release branch
58
+ local refs
59
+ refs="$(gh api "repos/$1/git/matching-refs/heads/${release_branch}" --jq '.[].ref' 2>&1)" \
60
+ || die "讀不到 ${1} 的分支清單,無法確認 ${release_branch} 開了沒有:${refs}"
61
+ grep -qx "refs/heads/${release_branch}" <<<"$refs"
62
+ }
63
+
47
64
  log "產品版號 ${plan}($(basename "$file")),入版單元:${plan_units[*]}"
48
65
  content="$(cat "$file")"
66
+ declare -A already_open=()
67
+ for m in "${members[@]}"; do
68
+ [[ -n "${units_of[$m]:-}" ]] || continue
69
+ if line_is_open "$m"; then already_open[$m]=1; fi
70
+ done
49
71
  for m in "${members[@]}"; do
50
72
  if [[ -z "${units_of[$m]:-}" ]]; then info "⊘ $m 沒有參與,不開線"; continue; fi
73
+ if [[ -n "${already_open[$m]:-}" ]]; then info "⊘ ${m} 已開線(${release_branch} 已存在),不再派送"; continue; fi
51
74
  if (( dry )); then info "[dry-run] $m ← open-release-line.yml plan=$plan units=${units_of[$m]}"; continue; fi
52
75
  info "→ ${m}:units=${units_of[$m]}"
53
76
  gh workflow run open-release-line.yml --repo "$m" -f plan="$plan" -f units="${units_of[$m]}" -f plan_content="$content"
@@ -19,7 +19,7 @@ product=""
19
19
  while [[ $# -gt 0 ]]; do
20
20
  case "$1" in --product) product="$2"; shift 2 ;; *) die "unknown argument: $1" ;; esac
21
21
  done
22
- policy_require
22
+ policy_require_product snapshot-members.sh
23
23
  [[ -n "$product" ]] || product="$(plan_versions "$(plan_dir)" | tail -1)"
24
24
  [[ -n "$product" ]] || die "沒有 Release Plan,也沒有帶 --product"
25
25
  # gh_read <fallback> <gh api args...>: the body of a gh api call. gh prints the error body of GitHub on stdout when a call fails
@@ -1,27 +1,41 @@
1
1
  name: ops-deploy
2
2
 
3
- # 依 ops/versions.json 的宣告上線:下載宣告指定的 bundle,`wrangler versions upload`,再
4
- # `wrangler versions deploy <版本 id>@100 --env <環境>`,最後打 /healthz 核對版號與宣告的 bundle 雜湊。
5
- # 兩個觸發:
6
- # - push 到預設分支:宣告有變的單元上線。poly 的 app 與 service 用 GitHub App 把宣告寫進 poly-ops,
7
- # 推上預設分支就觸發這裡。
3
+ # 兩個 job,iac 先、workloads 後:
4
+ # - iac:在 GitHub-hosted runner 套用 ops/iac/ 的 OpenTofu 步驟,建立雲端資源(例如 R2 bucket)。用另一把有 R2 寫入權限的 token,
5
+ # state 放在組織提供的 S3 相容 bucket(R2)。沒設 state backend 的設定時,這個 job 失敗並印缺哪一項,不呼叫 `tofu apply`。
6
+ # 要讀私有的 OpenTofu module 倉時(repository variable INFRA_MODULES_REPO),tofu init 前用 GitHub App 的權杖設 git 的 insteadOf。
7
+ # - workloads:needs iac,在 self-hosted runner 依 ops/versions.json 的宣告上線:下載 ops/versions.lock.json 指定的 bundle,把
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。
11
+ # 兩個步驟的先後都寫在 ops/needs.json。兩個觸發:
12
+ # - push 到預設分支:宣告、lock、設定或 iac 有變的單元上線(只改 ops/workloads/ 的設定也會上線)。poly 的 app 與 service 用 GitHub App
13
+ # 把宣告寫進 poly-ops,推上預設分支就觸發這裡。
8
14
  # - workflow_dispatch:monorepo 的 deploy workflow 寫完自己的 ops/ 後派送,也用來重跑。
9
15
  # /healthz 的網址樣式讀 ops/urls.json(compose 從 ops 單元公布的介面寫出),不是 repo 變數。
10
- # 只有這個 workflow 讀 Cloudflare 的 token。deploy-prerelease.yml 與 deploy-production.yml 只建置與寫宣告。
16
+ # 只有這個 workflow 讀 Cloudflare 的 token。deploy-prerelease.yml 與 deploy-prod.yml 只建置與寫宣告。
17
+
18
+ # run 名稱帶環境:run list 看不到 workflow_dispatch 的輸入,部署閘(release-stages.sh 的 train_gates)從這個名稱判斷一次 dispatch 對齊哪個環境。
19
+ # push 的 inputs.env 是空的,名稱是 `ops-deploy push`,push 會對齊所有環境。
20
+ run-name: ops-deploy ${{ inputs.env || 'push' }}
11
21
 
12
22
  on:
13
23
  push:
14
24
  branches: [main]
15
25
  paths:
16
26
  - "ops/versions.json"
27
+ - "ops/versions.lock.json"
28
+ - "ops/workloads/**"
29
+ - "ops/iac/**"
30
+ - "ops/needs.json"
17
31
  workflow_dispatch:
18
32
  inputs:
19
33
  env:
20
- description: "environment to deploy, e.g. staging"
34
+ description: "environment to deploy, e.g. stg"
21
35
  required: true
22
36
  type: string
23
37
  units:
24
- description: "comma list of units; empty deploys every unit the environment declares"
38
+ description: "comma list of units, a hint only; every run aligns every unit the environment declares"
25
39
  required: false
26
40
  default: ""
27
41
  type: string
@@ -33,6 +47,8 @@ defaults:
33
47
  run:
34
48
  shell: bash
35
49
 
50
+ # 整倉單一 group,GitHub 同一個 group 只保留一個排隊中的執行,新的進來會取消舊的。所以每次執行都對齊該環境宣告的全部單元
51
+ # (已上線的單元照 /healthz 略過),被取消的那次宣告的單元由下一次執行上線。
36
52
  concurrency:
37
53
  group: ops-deploy
38
54
  cancel-in-progress: false
@@ -44,7 +60,72 @@ env:
44
60
  CI_GATE_QUEUE_TRIES: "0"
45
61
 
46
62
  jobs:
47
- deploy:
63
+ iac:
64
+ if: vars.DEPLOY_ENABLED == 'true'
65
+ # 例外(特殊原因):tofu apply 要在每次都是空的 GitHub-hosted runner 上跑,和 Workers 的 self-hosted job 分開權限,用另一把 R2 token
66
+ runs-on: ubuntu-latest
67
+ timeout-minutes: 20
68
+ steps:
69
+ - uses: actions/checkout@v7
70
+ with:
71
+ fetch-depth: 0
72
+ persist-credentials: false
73
+ - uses: opentofu/setup-opentofu@a1320f892987e89d278cc92dc5adc984fb93aca4 # v2.0.2
74
+ with:
75
+ tofu_version: "1.10.0"
76
+ tofu_wrapper: false
77
+ # main.tf 的 module 放在組織層的 infra 倉(私有)。tofu init 用 git clone 它,所以先用 GitHub App 的權杖設 insteadOf。
78
+ # 倉名寫在 repository variable INFRA_MODULES_REPO,module 是公開的就不設。
79
+ - id: modules-name
80
+ if: vars.INFRA_MODULES_REPO != ''
81
+ env:
82
+ INFRA_MODULES_REPO: ${{ vars.INFRA_MODULES_REPO }}
83
+ run: echo "name=${INFRA_MODULES_REPO#*/}" >> "$GITHUB_OUTPUT"
84
+ - id: modules-token
85
+ if: vars.INFRA_MODULES_REPO != ''
86
+ uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
87
+ with:
88
+ client-id: ${{ vars.@@clientIdVariable@@ }}
89
+ private-key: ${{ secrets.@@privateKeySecret@@ }}
90
+ owner: ${{ github.repository_owner }}
91
+ repositories: ${{ steps.modules-name.outputs.name }}
92
+ permission-contents: read
93
+ - name: 設 git 的 insteadOf,讓 tofu init 讀得到私有 module 倉
94
+ if: vars.INFRA_MODULES_REPO != ''
95
+ env:
96
+ MODULES_TOKEN: ${{ steps.modules-token.outputs.token }}
97
+ INFRA_MODULES_REPO: ${{ vars.INFRA_MODULES_REPO }}
98
+ run: |
99
+ git config --global "url.https://x-access-token:${MODULES_TOKEN}@github.com/${INFRA_MODULES_REPO}.insteadOf" "https://github.com/${INFRA_MODULES_REPO}"
100
+ - name: 套用 iac 步驟
101
+ run: |
102
+ if [[ "$EVENT" == push ]]; then
103
+ bash scripts/deploy/ops-run.sh --phase iac --since "$BEFORE"
104
+ else
105
+ bash scripts/deploy/ops-run.sh --phase iac --env "$ENV_NAME" --units "$UNITS"
106
+ fi
107
+ env:
108
+ EVENT: ${{ github.event_name }}
109
+ BEFORE: ${{ github.event.before }}
110
+ ENV_NAME: ${{ inputs.env }}
111
+ UNITS: ${{ inputs.units }}
112
+ CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_R2_API_TOKEN }}
113
+ CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
114
+ TOFU_STATE_BUCKET: ${{ vars.TOFU_STATE_BUCKET }}
115
+ TOFU_STATE_ENDPOINT: ${{ vars.TOFU_STATE_ENDPOINT }}
116
+ TOFU_STATE_ACCESS_KEY_ID: ${{ secrets.TOFU_STATE_ACCESS_KEY_ID }}
117
+ TOFU_STATE_SECRET_ACCESS_KEY: ${{ secrets.TOFU_STATE_SECRET_ACCESS_KEY }}
118
+ DEPLOY_LOG_DIR: ${{ runner.temp }}/deploy-log
119
+ - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
120
+ if: always()
121
+ with:
122
+ name: ops-iac-${{ github.run_id }}
123
+ path: ${{ runner.temp }}/deploy-log
124
+ if-no-files-found: ignore
125
+ retention-days: 7
126
+
127
+ workloads:
128
+ needs: iac
48
129
  if: vars.DEPLOY_ENABLED == 'true'
49
130
  runs-on: self-hosted-linux
50
131
  timeout-minutes: 20
@@ -68,9 +149,9 @@ jobs:
68
149
  - name: 上線並核對 /healthz
69
150
  run: |
70
151
  if [[ "$EVENT" == push ]]; then
71
- bash scripts/deploy/ops-run.sh --since "$BEFORE"
152
+ bash scripts/deploy/ops-run.sh --phase workloads --since "$BEFORE"
72
153
  else
73
- bash scripts/deploy/ops-run.sh --env "$ENV_NAME" --units "$UNITS"
154
+ bash scripts/deploy/ops-run.sh --phase workloads --env "$ENV_NAME" --units "$UNITS"
74
155
  fi
75
156
  env:
76
157
  EVENT: ${{ github.event_name }}