blueprint-shared-templates 0.2.0 → 0.3.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.
Files changed (74) 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 +21 -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 +30 -15
  10. package/deploy/.github/workflows/deploy-prerelease.yml +6 -50
  11. package/deploy/.github/workflows/deploy-prod.yml +103 -0
  12. package/deploy/scripts/deploy/plan-prod.sh +57 -0
  13. package/deploy/scripts/deploy/promote.sh +23 -7
  14. package/deploy/scripts/deploy/publish-bundle.sh +10 -1
  15. package/deploy/scripts/deploy/route-tag.sh +10 -12
  16. package/deploy/scripts/deploy/run-prerelease.sh +4 -7
  17. package/deploy/scripts/deploy/{run-production.sh → run-prod.sh} +15 -17
  18. package/deploy/scripts/deploy/writeback-ops.sh +71 -40
  19. package/hub/scripts/release/dispatch-release-lines.sh +25 -2
  20. package/hub/scripts/release/snapshot-members.sh +1 -1
  21. package/ops/.github/workflows/ops-deploy.yml +92 -11
  22. package/ops/schemas/targets.schema.json +209 -0
  23. package/ops/scripts/ci/check-targets.mjs +291 -0
  24. package/ops/scripts/deploy/apply-iac.sh +89 -0
  25. package/ops/scripts/deploy/apply-ops.sh +101 -48
  26. package/ops/scripts/deploy/ops-run.sh +50 -12
  27. package/ops/scripts/deploy/smoke.sh +35 -16
  28. package/ops/scripts/lib/ops-steps.sh +75 -0
  29. package/org/scripts/ci/check-org-values.mjs +91 -0
  30. package/package.json +2 -2
  31. package/publish/.github/workflows/publish.yml +12 -3
  32. package/publish/scripts/release/mirror-units.sh +45 -5
  33. package/publish/scripts/release/publish-units.sh +28 -4
  34. package/release/.github/workflows/advance-release.yml +107 -0
  35. package/release/.github/workflows/close-release.yml +133 -0
  36. package/release/.github/workflows/merge-back.yml +82 -0
  37. package/release/.github/workflows/merge-release-pr.yml +82 -0
  38. package/release/.github/workflows/open-release-line.yml +14 -5
  39. package/release/.github/workflows/release-line-finalize.yml +5 -3
  40. package/release/.github/workflows/release-please.yml +99 -15
  41. package/release/.github/workflows/sync-release-tools.yml +91 -0
  42. package/release/release-policy.json +1 -1
  43. package/release/schemas/release-policy.schema.json +13 -3
  44. package/release/scripts/ci/branch-exists.sh +27 -0
  45. package/release/scripts/ci/check-release-policy.sh +13 -2
  46. package/release/scripts/ci/pin-train.sh +5 -3
  47. package/release/scripts/ci/release-train.sh +22 -3
  48. package/release/scripts/lib/release-plan.sh +3 -1
  49. package/release/scripts/lib/release-pr.sh +275 -11
  50. package/release/scripts/lib/release-stages.sh +245 -27
  51. package/release/scripts/release/check-trigger-actor.sh +32 -0
  52. package/release/scripts/release/close-ready.mjs +72 -0
  53. package/release/scripts/release/close-ready.sh +70 -0
  54. package/release/scripts/release/cut-release.sh +34 -3
  55. package/release/scripts/release/ensure-stage-deploys.sh +3 -3
  56. package/release/scripts/release/finalize-release.sh +47 -15
  57. package/release/scripts/release/merge-back-ready.sh +27 -0
  58. package/release/scripts/release/merge-release-pr.sh +34 -107
  59. package/release/scripts/release/open-release-line.sh +28 -8
  60. package/release/scripts/release/pin-release-pr.sh +53 -0
  61. package/release/scripts/release/product-release-notes.sh +28 -0
  62. package/release/scripts/release/sync-release-tools.sh +137 -0
  63. package/release/scripts/release/{verify-production.sh → verify-prod.sh} +11 -10
  64. package/transform.mjs +2 -3
  65. package/verify/.github/workflows/{verify-production.yml → verify-prod.yml} +7 -7
  66. package/verify/scripts/release/verify-ops.sh +4 -4
  67. package/deploy/.github/workflows/deploy-production.yml +0 -173
  68. package/deploy/scripts/deploy/plan-production.sh +0 -43
  69. package/infra/.github/workflows/deploy-prerelease.yml +0 -157
  70. package/infra/.github/workflows/deploy-production.yml +0 -173
  71. package/infra/scripts/deploy/apply-infra.sh +0 -46
  72. package/infra/scripts/deploy/apply-prerelease.sh +0 -22
  73. package/infra/scripts/deploy/apply-production.sh +0 -33
  74. package/infra/scripts/deploy/route-tag.sh +0 -65
@@ -26,6 +26,15 @@ policy_require() {
26
26
  [[ -f "$(policy_file)" ]] || die "找不到政策檔 $(policy_file)(格式見 docs/release-policy.md)"
27
27
  }
28
28
 
29
+ # policy_require_product <script> → stops with a message that names the organization layer when the policy is the one of a
30
+ # repository of that layer. Such a repository belongs to no product, so a script that reads product.members has nothing to read.
31
+ policy_require_product() {
32
+ policy_require
33
+ if jq -e '.layer == "organization"' "$(policy_file)" >/dev/null 2>&1; then
34
+ die "$1 只用在產品的 hub:$(policy_file) 是組織層的倉(layer: organization),不屬於任何產品,沒有 product.members"
35
+ fi
36
+ }
37
+
29
38
  policy_unit_exists() { policy_require; jq -e --arg u "$1" '(.units // {}) | has($u)' "$(policy_file)" >/dev/null; }
30
39
 
31
40
  # policy_unit_stages <unit> → the stages the unit walks on its own, one per line. Stops when the policy
@@ -35,6 +44,24 @@ policy_unit_stages() {
35
44
  jq -r --arg u "$1" '.units[$u].stages[]' "$(policy_file)"
36
45
  }
37
46
 
47
+ # stages_override_check <json> → stops with the reason when <json> is not a valid stage override of this release. The format is a
48
+ # JSON object { "<unit>": ["rc", "ga"], ... }: every key a unit the policy lists, every value a non-empty list of stages
49
+ # (RELEASE_STAGES) that ends at ga. A workflow input is a string, so the override travels as this JSON text.
50
+ stages_override_check() {
51
+ local json="$1" bad
52
+ jq -e . <<<"$json" >/dev/null 2>&1 || die "stages_override 不是 JSON:${json}(格式 {\"<單元>\":[\"rc\",\"ga\"]})"
53
+ jq -e 'type == "object" and length > 0' <<<"$json" >/dev/null || die "stages_override 需為非空的 JSON 物件 {\"<單元>\":[\"rc\",\"ga\"]}:${json}"
54
+ bad="$(jq -r '. as $o | keys[] | select(($o[.] | type == "array" and length > 0 and all(.[]; IN("alpha","beta","rc","ga")) and .[-1] == "ga") | not)' <<<"$json" | paste -sd' ' -)"
55
+ [[ -z "$bad" ]] || die "stages_override 的 ${bad} 需為 alpha、beta、rc、ga 組成、以 ga 結尾的非空陣列"
56
+ bad="$(jq -r --slurpfile p "$(policy_file)" 'keys[] as $k | select(($p[0].units // {}) | has($k) | not) | $k' <<<"$json" | paste -sd' ' -)"
57
+ [[ -z "$bad" ]] || die "stages_override 指到政策檔沒有的單元:${bad}"
58
+ }
59
+
60
+ # stages_override_policy <json> → prints the policy file with the stages of the units in <json> replaced
61
+ stages_override_policy() {
62
+ jq --argjson o "$1" 'reduce ($o | to_entries[]) as $e (.; .units[$e.key].stages = $e.value)' "$(policy_file)"
63
+ }
64
+
38
65
  policy_unit_kind() { policy_require; jq -r --arg u "$1" '.units[$u].kind // empty' "$(policy_file)"; }
39
66
 
40
67
  # policy_unit_independent <unit> → succeeds when the policy marks the unit "independent": true, which means the
@@ -338,15 +365,23 @@ train_assign_stages() {
338
365
  for u in "$@"; do printf '%s %s\n' "$u" "${stage_of[$u]}"; done
339
366
  }
340
367
 
368
+ # releasable_commits_in <range> [<path>...] → success when <range> has a commit release-please would release: a feat, fix,
369
+ # perf, deps or revert commit or a breaking change that touches one of the paths (any path when none is given). The one rule
370
+ # behind unit_changed, the merge-back wait list (train_units_to_ship) and the pin base check (release_pin_open_pr).
371
+ releasable_commits_in() {
372
+ local range="$1"; shift
373
+ [[ -z "$(git log -1 --format=%H -E \
374
+ --grep='^(feat|fix|perf|deps|revert)(\([^)]*\))?!?:' --grep='^BREAKING[ -]CHANGE:' "$range" -- "$@")" ]] && return 1
375
+ return 0
376
+ }
377
+
341
378
  # unit_changed <unit> <anchor> <ref> → whether <ref> has, since <anchor> (empty: the whole history), a
342
- # commit release-please would release for the unit: a feat, fix, perf, deps or revert commit or a
343
- # breaking change that touches the unit's path
379
+ # commit release-please would release for the unit (releasable_commits_in on the unit's path)
344
380
  unit_changed() {
345
381
  local p range="$3"
346
382
  p="$(unit_path "$1")" || return 1
347
383
  [[ -z "$2" ]] || range="$2..$3"
348
- [[ -n "$(git log -1 --format=%H -E \
349
- --grep='^(feat|fix|perf|deps|revert)(\([^)]*\))?!?:' --grep='^BREAKING[ -]CHANGE:' "$range" -- "$p")" ]]
384
+ releasable_commits_in "$range" "$p"
350
385
  }
351
386
 
352
387
  # stage_version <version> <stage> → the version a unit enters <stage> with, computed by the semver
@@ -383,6 +418,66 @@ train_stage() {
383
418
  printf '%s\n' "$stage"
384
419
  }
385
420
 
421
+ # train_units_to_ship [<ref>] → one component per line: the units the line opened with (the Release-Train-Units trailer of the opening
422
+ # commit on <ref>, default HEAD) minus those with no releasable commit since the start point of the line (unit_changed's rule, anchored
423
+ # where release-please counts from: last-release-sha of the config, the whole history when the config has none). Such a unit gets no
424
+ # Release PR entry and no tag, so waiting for its tag would never end. The list does not depend on the manifest: right after the
425
+ # line is opened no unit has been bumped yet, and a manifest-based list is empty and looks complete. Without a Release-Train-Units
426
+ # trailer the units of the config count.
427
+ train_units_to_ship() {
428
+ local ref="${1:-HEAD}" anchor u list
429
+ list="$(train_units_of "$ref")"
430
+ if [[ -z "$list" ]]; then list="$(config_units | tr '\n' ' ')"; fi
431
+ [[ -n "$list" ]] || return 0
432
+ anchor="$(git show "$ref:$CONFIG" 2>/dev/null | jq -r '."last-release-sha" // empty' 2>/dev/null || true)"
433
+ for u in $list; do
434
+ unit_changed "$u" "$anchor" "$ref" || continue
435
+ printf '%s\n' "$u"
436
+ done
437
+ return 0
438
+ }
439
+
440
+ # train_final_tags_missing [<ref>] → one line per unit the train ships (train_units_to_ship) whose final tag is not there yet, nothing
441
+ # when all are. A unit still on a prerelease version has not been through the GA Release PR; a unit whose manifest version is still
442
+ # the one origin/main has was not bumped yet (the line was just opened, and the tag of that old version exists); a unit on a new
443
+ # stable version needs its <component>/v<version> tag, which release-please makes when that PR is merged. Reads the local tags:
444
+ # fetch them first. A train that ships no unit has nothing missing.
445
+ train_final_tags_missing() {
446
+ local u p v mv
447
+ git rev-parse -q --verify origin/main >/dev/null || { printf '%s\n' "找不到 origin/main,無法判斷單元升版了沒"; return 0; }
448
+ while IFS= read -r u; do
449
+ [[ -n "$u" ]] || continue
450
+ p="$(unit_path "$u")" || { printf '%s\n' "$u 在設定裡找不到目錄"; continue; }
451
+ v="$(manifest_version "$p")"
452
+ if [[ "$v" == *-* ]]; then printf '%s\n' "$u 仍是 prerelease $v"; continue; fi
453
+ mv="$(git show "origin/main:$MANIFEST" | jq -r --arg p "$p" '.[$p] // empty')"
454
+ if [[ -n "$mv" && "$mv" == "$v" ]]; then printf '%s\n' "${u} 尚未升版(manifest 仍是 main 的 ${v})"; continue; fi
455
+ git rev-parse -q --verify "refs/tags/$u/v$v" >/dev/null || printf '%s\n' "$u/v$v"
456
+ done < <(train_units_to_ship "${1:-HEAD}")
457
+ return 0
458
+ }
459
+
460
+ # train_merge_back_ready [<ref>] → 0 when the merge-back of the line can be opened: the line stage on <ref> (default HEAD) is ga and
461
+ # train_final_tags_missing lists nothing. Otherwise 1, with TRAIN_MERGE_BACK_WAITING set to what is still missing. The release-please
462
+ # job asks it after every run, so it only looks and never waits. A train with no unit to ship counts as ready at ga.
463
+ TRAIN_MERGE_BACK_WAITING=""
464
+ # shellcheck disable=SC2034 # TRAIN_MERGE_BACK_WAITING is read by merge-back-ready.sh, which sources this file
465
+ train_merge_back_ready() {
466
+ local stage missing
467
+ TRAIN_MERGE_BACK_WAITING=""
468
+ stage="$(train_stage "${1:-HEAD}")"
469
+ if [[ "$stage" != ga ]]; then
470
+ TRAIN_MERGE_BACK_WAITING="發布線的階段是 ${stage},不是 ga"
471
+ return 1
472
+ fi
473
+ missing="$(train_final_tags_missing "${1:-HEAD}")"
474
+ if [[ -n "$missing" ]]; then
475
+ TRAIN_MERGE_BACK_WAITING="GA 尚未完成,缺:$(tr '\n' ' ' <<<"$missing")"
476
+ return 1
477
+ fi
478
+ return 0
479
+ }
480
+
386
481
  # train_boarded_units → every unit the train was opened with, deferred ones included, from the newest
387
482
  # Release-Train-Units trailer; the units of the config when there is none
388
483
  train_boarded_units() {
@@ -488,6 +583,34 @@ train_commits_not_on_main() {
488
583
  done < <(git cherry origin/main "$rev" "$open")
489
584
  }
490
585
 
586
+ # train_commits_after_merge_back <rev> <product> → "<short sha> <subject>" for each commit on <rev> that came after the
587
+ # merge-back product commit (chore(<branch>): product v<product>, the newest one) and that origin/main does not have,
588
+ # judged the way train_commits_not_on_main judges: by patch and by content, never by ancestry. Close runs this before it
589
+ # tags: a commit pushed to the release branch after merge-back (a late hotfix) is in the tree the product tag would name,
590
+ # and main never got it. Prints nothing when <rev> has no product commit, then the version check of close decides.
591
+ train_commits_after_merge_back() {
592
+ local rev="$1" product="$2" anchor mark sha
593
+ local -A MAIN_VERSIONS=()
594
+ # No pipe into an early-exiting reader here: under pipefail the writer's SIGPIPE fails the function.
595
+ anchor="$(git log -1 --format=%H -E --grep="^chore\(.+\): product v${product//./\.}\$" "$rev")"
596
+ [[ -n "$anchor" ]] || return 0
597
+ while read -r mark sha; do
598
+ [[ "$mark" == + ]] || continue
599
+ if commit_matches_main "$sha"; then continue; fi
600
+ git log -1 --format='%h %s' "$sha"
601
+ done < <(git cherry origin/main "$rev" "$anchor")
602
+ }
603
+
604
+ # main_has_train_base <base> → success when a commit of origin/main carries "Release-Train-Base: <base>" as a trailer.
605
+ # There is no count limit: main moves on while the merge-back PR waits for its checks, so the commit can be many
606
+ # commits back. --grep narrows the walk to the commits that mention the base, so the cost does not grow with main.
607
+ # The text is read into a variable first: under pipefail, grep -q exiting early would fail the git log feeding it.
608
+ main_has_train_base() {
609
+ local base="$1" trailers
610
+ trailers="$(git log origin/main --grep="Release-Train-Base: ${base}" --format='%(trailers:key=Release-Train-Base,valueonly)')"
611
+ grep -qx "$base" <<<"$trailers"
612
+ }
613
+
491
614
  # Bring the local train up to its remote. Fetch even under --dry-run; fetching only updates local refs.
492
615
  train_sync() { # train_sync <branch>
493
616
  local branch="$1"
@@ -497,6 +620,49 @@ train_sync() { # train_sync <branch>
497
620
  run git merge --ff-only "origin/$branch"
498
621
  }
499
622
 
623
+ # Helpers of the deploy gate in train_gates. They read and set the locals of train_gates (bash scopes locals dynamically).
624
+ # dg_need_slug <why>: sets dg_slug, the name of this repository, once; dies with <why> when it cannot be found.
625
+ dg_need_slug() {
626
+ [[ -z "$dg_slug" ]] || return 0
627
+ dg_slug="${GITHUB_REPOSITORY:-}"
628
+ [[ -n "$dg_slug" ]] || dg_slug="$(gh repo view --json nameWithOwner --jq .nameWithOwner 2>/dev/null)" \
629
+ || die "部署閘無法得知倉名,${1}(設 GITHUB_REPOSITORY,或在有 origin 的倉內執行;本機演練可用 DEPLOY_GATE=0 略過)"
630
+ }
631
+ # dg_ops_gh <gh args>...: gh for the calls that go to the ops repository. With DEPLOY_GATE_OPS_TOKEN set (advance-release.yml gets it from a
632
+ # second GitHub App token limited to the ops repository, read-only: the token of the release branch covers this repository alone, so
633
+ # a private ops repository answers 404 to it) the call uses that token; without it the environment's own GH_TOKEN, as before.
634
+ dg_ops_gh() {
635
+ if [[ -n "${DEPLOY_GATE_OPS_TOKEN:-}" ]]; then GH_TOKEN="$DEPLOY_GATE_OPS_TOKEN" gh "$@"; else gh "$@"; fi
636
+ }
637
+ # dg_variable <slug> <name>: the Actions variable <name> as a repository sees it, the repository's own first and the organization's only
638
+ # when the repository has none. Sets dg_val to "set:<value>" (empty when neither level has it) and dg_where to 倉層 or 組織層.
639
+ # Each list is read at most once per train_gates call, so DEPLOY_ENABLED and OPS_REPO share the queries. A list that cannot be read dies,
640
+ # because it must not be taken for "not set".
641
+ dg_variable() {
642
+ local slug="$1" name="$2" err text
643
+ if (( dg_repo_read == 0 )); then
644
+ err="$(mktemp)"
645
+ if ! dg_repo_vars="$(gh api --paginate "repos/${slug}/actions/variables" --jq '.variables[] | "\(.name)=\(.value)"' 2>"$err")"; then
646
+ text="$(cat "$err")"; rm -f "$err"
647
+ die "部署閘無法讀 ${slug} 的倉層變數 ${name}:${text:-gh api 失敗}(查 GitHub App 的 Variables 讀取權限;本機演練可用 DEPLOY_GATE=0 略過)"
648
+ fi
649
+ rm -f "$err"; dg_repo_read=1
650
+ fi
651
+ dg_where=倉層
652
+ dg_val="$(grep -m1 -E "^${name}=" <<<"$dg_repo_vars" | sed "s/^${name}=/set:/" || true)"
653
+ [[ -z "$dg_val" ]] || return 0
654
+ if (( dg_org_read == 0 )); then
655
+ err="$(mktemp)"
656
+ if ! dg_org_vars="$(gh api --paginate "repos/${slug}/actions/organization-variables" --jq '.variables[] | "\(.name)=\(.value)"' 2>"$err")"; then
657
+ text="$(cat "$err")"; rm -f "$err"
658
+ die "部署閘無法讀 ${slug} 可用的組織層變數 ${name}:${text:-gh api 失敗}(查 GitHub App 的 Variables 讀取權限;本機演練可用 DEPLOY_GATE=0 略過)"
659
+ fi
660
+ rm -f "$err"; dg_org_read=1
661
+ fi
662
+ dg_where=組織層
663
+ dg_val="$(grep -m1 -E "^${name}=" <<<"$dg_org_vars" | sed "s/^${name}=/set:/" || true)"
664
+ }
665
+
500
666
  # Gates a train passes before it moves to <stage>, run after train_sync. The ci gate is called the
501
667
  # GA gate when the train goes to ga, which is how the docs know it.
502
668
  train_gates() { # train_gates <branch> <line> <stage>
@@ -600,7 +766,8 @@ train_gates() { # train_gates <branch> <line> <stage>
600
766
  fi
601
767
 
602
768
  # deploy gate: the deploy of the stage the train leaves must have succeeded before the train moves on (decision
603
- # deploy-gate-before-advance). The units are those ensure-stage-deploys.sh looks at: every app, service and infra unit of
769
+ # deploy-gate-before-advance). A deploy is two runs: deploy-prerelease writes the declaration into ops, and an ops-deploy run that covers the
770
+ # declaration puts it online, so both must have succeeded. The units are those ensure-stage-deploys.sh looks at: every app and service unit of
604
771
  # the manifest with a prerelease version, whose tag is the stage before this one. A tag with nothing to deploy
605
772
  # (scripts/deploy/route-tag.sh) is left out. Per tag one query for the newest deploy-prerelease run, which passes only
606
773
  # when it is completed and successful: a run that is still going, no run, a tag that is not created and a failed query
@@ -612,11 +779,12 @@ train_gates() { # train_gates <branch> <line> <stage>
612
779
  # the gate, because it must not be taken for "not enabled". When the file has no such line, or does not exist, the repository removed the
613
780
  # condition and deploys whenever the workflow runs: the variable is not read and the gate checks the run as usual.
614
781
  if (( DRY_RUN == 0 )) && [[ "${DEPLOY_GATE:-1}" == 1 ]]; then
615
- local dg_unit dg_ver dg_tag dg_route dg_st dg_err dg_text dg_id dg_rest dg_concl dg_units dg_slug dg_enabled="" dg_val dg_why
782
+ local dg_slug="" dg_repo_vars="" dg_org_vars="" dg_repo_read=0 dg_org_read=0 dg_where dg_unit dg_ver dg_tag dg_route dg_st dg_err dg_text dg_id dg_rest dg_concl dg_units dg_enabled="" dg_val dg_why dg_ops="" dg_sha dg_env dg_list dg_status dg_event dg_title dg_hit dg_vers
783
+ local -A dg_declared=()
616
784
  dg_units="$(jq -r --slurpfile cfg "$REPO_ROOT/release-please-config.json" '
617
785
  ($cfg[0].packages) as $pkgs
618
786
  | to_entries[]
619
- | select(.key | startswith("apps/") or startswith("services/") or startswith("infra/"))
787
+ | select(.key | startswith("apps/") or startswith("services/"))
620
788
  | select(.value | test("-"))
621
789
  | "\($pkgs[.key].component // (.key | split("/")[1])) \(.value)"
622
790
  ' "$REPO_ROOT/.release-please-manifest.json")"
@@ -632,26 +800,10 @@ train_gates() { # train_gates <branch> <line> <stage>
632
800
  fi
633
801
  fi
634
802
  if [[ -z "$dg_enabled" ]]; then
635
- dg_slug="${GITHUB_REPOSITORY:-}"
636
- [[ -n "$dg_slug" ]] || dg_slug="$(gh repo view --json nameWithOwner --jq .nameWithOwner 2>/dev/null)" \
637
- || die "部署閘無法得知倉名,讀不到 DEPLOY_ENABLED(設 GITHUB_REPOSITORY,或在有 origin 的倉內執行;本機演練可用 DEPLOY_GATE=0 略過)"
638
- dg_err="$(mktemp)"
639
- if ! dg_val="$(gh api --paginate "repos/${dg_slug}/actions/variables" \
640
- --jq '.variables[] | select(.name == "DEPLOY_ENABLED") | "set:" + .value' 2>"$dg_err")"; then
641
- dg_text="$(cat "$dg_err")"; rm -f "$dg_err"
642
- die "部署閘無法讀 ${dg_slug} 的倉層變數 DEPLOY_ENABLED:${dg_text:-gh api 失敗}(查 GitHub App 的 Variables 讀取權限;本機演練可用 DEPLOY_GATE=0 略過)"
643
- fi
644
- dg_why="倉層設為「${dg_val#set:}」"
645
- if [[ -z "$dg_val" ]]; then
646
- if ! dg_val="$(gh api --paginate "repos/${dg_slug}/actions/organization-variables" \
647
- --jq '.variables[] | select(.name == "DEPLOY_ENABLED") | "set:" + .value' 2>"$dg_err")"; then
648
- dg_text="$(cat "$dg_err")"; rm -f "$dg_err"
649
- die "部署閘無法讀 ${dg_slug} 可用的組織層變數 DEPLOY_ENABLED:${dg_text:-gh api 失敗}(查 GitHub App 的 Variables 讀取權限;本機演練可用 DEPLOY_GATE=0 略過)"
650
- fi
651
- dg_why="組織層設為「${dg_val#set:}」"
652
- [[ -n "$dg_val" ]] || dg_why="倉層與組織層都沒設"
653
- fi
654
- rm -f "$dg_err"
803
+ dg_need_slug "讀不到 DEPLOY_ENABLED"
804
+ dg_variable "$dg_slug" DEPLOY_ENABLED
805
+ dg_why="${dg_where}設為「${dg_val#set:}」"
806
+ [[ -n "$dg_val" ]] || dg_why="倉層與組織層都沒設"
655
807
  dg_val="${dg_val#set:}"
656
808
  if [[ "$(printf '%s' "$dg_val" | tr 'A-Z' 'a-z')" == true ]]; then dg_enabled=1; else dg_enabled=0; fi
657
809
  fi
@@ -678,6 +830,72 @@ train_gates() { # train_gates <branch> <line> <stage>
678
830
  [[ "$dg_concl" == success ]] \
679
831
  || die "部署閘未通過:${dg_tag} 最新一筆 deploy-prerelease run ${dg_id} 結論為 ${dg_concl}(查 gh run view ${dg_id})。修好部署後重跑,或以 DEPLOY_GATE=0 略過"
680
832
  info "✓ ${dg_tag} 部署 run ${dg_id} 通過"
833
+ # deploy-prerelease only writes the declaration into ops; the unit goes online in an ops-deploy run, so one must have succeeded too.
834
+ # The repository that holds ops is found in this order: the environment variable OPS_REPO; the repository variable OPS_REPO, then the
835
+ # organization variable OPS_REPO (a poly repository sets it there for deploy-prerelease.yml, and a local run has no environment
836
+ # variable); this repository when none is set (a monorepo). The variable lists are the ones DEPLOY_ENABLED was read from, read once.
837
+ # The run that decides is the newest one that COVERS the unit, not the run of the declaration commit, for three reasons.
838
+ # - ops-deploy.yml has one concurrency group that cancels the older pending run, and every run aligns every unit the environment
839
+ # declares (ops-run.sh), so a cancelled run is normal and the next run puts its units online. A cancelled run is passed over.
840
+ # - A skipped run is not passed over. Its jobs carry the condition vars.DEPLOY_ENABLED == 'true' (templates/ops ops-deploy.yml), so a
841
+ # skipped newest run means ops-deploy is not enabled in that repository: nothing is deployed, nothing can be checked, and the gate
842
+ # lets the unit go with a line saying so, as it does for a deploy-prerelease that is not enabled. Skipping it to look at an older
843
+ # success would let a repository that switched ops-deploy off be judged by a run from before.
844
+ # - A monorepo dispatches ops-deploy at the head of main (run-prerelease.sh), so the headSha of the run is not the declaration commit.
845
+ # - When the declaration already exists writeback-ops.sh makes no commit, so there is no commit to look for by its message.
846
+ # A run covers the unit when the ops/versions.json at its headSha declares <unit> at <purpose>/<env> with the version of the tag,
847
+ # and the run aligns that environment: a push run aligns every environment, a workflow_dispatch run only the one in its input env,
848
+ # which ops-deploy.yml writes into the run name ("ops-deploy <env>", "ops-deploy push" on a push), the only place a run list shows it.
849
+ if [[ -z "$dg_ops" ]]; then
850
+ dg_ops="${OPS_REPO:-}"
851
+ if [[ -z "$dg_ops" ]]; then
852
+ dg_need_slug "讀不到 OPS_REPO;設環境變數 OPS_REPO 也可以"
853
+ dg_variable "$dg_slug" OPS_REPO
854
+ dg_ops="${dg_val#set:}"
855
+ [[ -n "$dg_ops" ]] || dg_ops="$dg_slug"
856
+ fi
857
+ fi
858
+ dg_env="$(sed -n 's/^env=//p' <<<"$dg_route")"
859
+ dg_err="$(mktemp)"
860
+ if ! dg_list="$(dg_ops_gh run list --repo "$dg_ops" --workflow ops-deploy.yml --branch "${DEFAULT_BRANCH:-main}" --limit 20 \
861
+ --json status,conclusion,databaseId,headSha,event,displayTitle \
862
+ --jq '.[] | "\(.status)|\(.conclusion // "")|\(.databaseId)|\(.headSha)|\(.event)|\(.displayTitle)"' 2>"$dg_err")"; then
863
+ dg_text="$(cat "$dg_err")"; rm -f "$dg_err"
864
+ die "部署閘無法查詢 ${dg_ops} 的 ops-deploy 執行紀錄:${dg_text:-gh run list 失敗}(查 gh 權限與 token;本機演練可用 DEPLOY_GATE=0 略過)"
865
+ fi
866
+ dg_hit=""
867
+ while IFS='|' read -r dg_status dg_concl dg_id dg_sha dg_event dg_title; do
868
+ [[ -n "$dg_id" ]] || continue
869
+ if [[ "$dg_event" == push ]]; then :
870
+ elif [[ "$dg_event" == workflow_dispatch && "$dg_title" == "ops-deploy ${dg_env}" ]]; then :
871
+ else continue; fi
872
+ if [[ -z "${dg_declared[$dg_sha]+x}" ]]; then
873
+ if ! dg_declared[$dg_sha]="$(dg_ops_gh api "repos/${dg_ops}/contents/ops/versions.json?ref=${dg_sha}" -H "Accept: application/vnd.github.raw+json" 2>"$dg_err")"; then
874
+ dg_text="$(cat "$dg_err")"; rm -f "$dg_err"
875
+ die "部署閘無法讀 ${dg_ops} 在 ${dg_sha:0:7} 的 ops/versions.json:${dg_text:-gh api 失敗}(查 gh 權限與 token;本機演練可用 DEPLOY_GATE=0 略過)"
876
+ fi
877
+ fi
878
+ dg_vers="$(jq -r --arg e "$dg_env" --arg u "$dg_unit" '
879
+ to_entries[] | select((.key | startswith("hosts/") | not) and (.key | endswith("/" + $e)))
880
+ | (.value // {})[$u] | select(. != null) | if type == "object" then .version else . end' <<<"${dg_declared[$dg_sha]}" 2>/dev/null || true)"
881
+ grep -Fxq -- "$dg_ver" <<<"$dg_vers" || continue
882
+ if [[ "$dg_status" == completed && "$dg_concl" == cancelled ]]; then continue; fi
883
+ dg_hit=1; break
884
+ done <<<"$dg_list"
885
+ rm -f "$dg_err"
886
+ if [[ -z "$dg_hit" ]]; then
887
+ die "部署閘未通過:${dg_ops} 最近 20 筆 ops-deploy run 沒有一筆成功套用 ${dg_unit} ${dg_ver} 到 ${dg_env}(被取消的不算)。派送 gh workflow run ops-deploy.yml --repo ${dg_ops} -f env=${dg_env},等它跑完再重跑,或以 DEPLOY_GATE=0 略過"
888
+ fi
889
+ if [[ "$dg_status" != completed ]]; then
890
+ die "部署閘未通過:${dg_tag} 的 ops-deploy run ${dg_id} 還在跑(狀態 ${dg_status})。等它跑完再重跑,或以 DEPLOY_GATE=0 略過"
891
+ fi
892
+ if [[ "$dg_concl" == skipped ]]; then
893
+ info "⊘ ${dg_tag} ops-deploy 沒啟用:${dg_ops} 的 ops-deploy run ${dg_id} 結論為 skipped(job 條件 vars.DEPLOY_ENABLED 不成立),沒有上線可核對,放行"
894
+ continue
895
+ fi
896
+ [[ "$dg_concl" == success ]] \
897
+ || die "部署閘未通過:${dg_tag} 最新一筆套用它的 ops-deploy run ${dg_id} 結論為 ${dg_concl}(查 gh run view ${dg_id} --repo ${dg_ops})。修好後重跑,或以 DEPLOY_GATE=0 略過"
898
+ info "✓ ${dg_tag} 的 ops-deploy run ${dg_id} 通過"
681
899
  done <<<"$dg_units"
682
900
  fi
683
901
  }
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env bash
2
+ # Check who started a manual release workflow. The workflows that advance a stage or merge a Release PR call this before
3
+ # they run anything that writes, because a private repository on the free plan has no environment with required reviewers.
4
+ #
5
+ # usage: check-trigger-actor.sh
6
+ # env: TRIGGERING_ACTOR github.triggering_actor of the run. It is the account that started this run attempt: on a re-run
7
+ # it is the person who re-ran, while github.actor and github.event.sender stay the person who
8
+ # started the first attempt. A name list read against those two would let an account outside the
9
+ # list re-run a run that a listed account dispatched, so never pass them here.
10
+ # ALLOWED_ACTORS the accounts that may start the run, separated by commas, spaces or new lines
11
+ # (the repository variable RELEASE_TRIGGER_ACTORS). A GitHub App appears as <slug>[bot].
12
+ # Empty or unset: nobody is excluded.
13
+ # Exit 0 when the account is listed or the list is empty, 1 otherwise. Logins are compared without regard to case,
14
+ # as GitHub does.
15
+ set -euo pipefail
16
+ actor="${TRIGGERING_ACTOR:-}"
17
+ allowed="${ALLOWED_ACTORS:-}"
18
+ list="$(tr ',[:space:]' '\n\n' <<<"$allowed" | sed '/^$/d' | tr '[:upper:]' '[:lower:]')"
19
+ if [[ -z "$list" ]]; then
20
+ printf '%s\n' "→ RELEASE_TRIGGER_ACTORS 沒有設定,不限制誰可以觸發(觸發者 ${actor:-未知})"
21
+ exit 0
22
+ fi
23
+ if [[ -z "$actor" ]]; then
24
+ printf '%s\n' "✗ 讀不到觸發者(TRIGGERING_ACTOR 是空的),名單有設定時不放行" >&2
25
+ exit 1
26
+ fi
27
+ if grep -qxF -- "$(tr '[:upper:]' '[:lower:]' <<<"$actor")" <<<"$list"; then
28
+ printf '%s\n' "→ 觸發者 ${actor} 在名單內"
29
+ exit 0
30
+ fi
31
+ printf '%s\n' "✗ 觸發者 ${actor} 不在 RELEASE_TRIGGER_ACTORS 的名單內($(tr '\n' ' ' <<<"$list" | sed 's/ $//')),不執行" >&2
32
+ exit 1
@@ -0,0 +1,72 @@
1
+ // Decides whether the close workflow (close-release.yml) may close a release line now: merge the merge-back PR, tag the product
2
+ // version and delete the branches. It only reads the facts of the PR and of its checks and prints the decision; close-ready.sh
3
+ // gathers the facts with `gh`, so this file stays testable without a network.
4
+ //
5
+ // usage: node scripts/release/close-ready.mjs --input <file|-> [--config scripts/merge/merge-config.json]
6
+ // input JSON { pr, checkedSha, eventHeadSha, checks, changedFiles }
7
+ // pr from `gh pr view --json number,state,isDraft,headRefOid,headRefName,baseRefName,isCrossRepository`, read last
8
+ // checkedSha the head commit of the PR when the checks were read
9
+ // eventHeadSha the head_sha of the workflow_run event that started the run (github.event.workflow_run.head_sha), or null when
10
+ // a person started it. It is the commit the finished ci or pr-message ran for. Never github.sha, which is main.
11
+ // checks from `gh pr checks --json name,bucket`
12
+ // changedFiles the paths the PR changes (for the checks with a path filter)
13
+ // config the one auto-merge reads (scripts/merge/merge-config.json): requiredChecks and pathFiltered
14
+ // Output: one JSON line { action, reason, ... }. action is "close" (pr, sha and line are set) or "skip". A skip is the normal answer of
15
+ // most events, because ci and pr-message finish one after the other and the PR is not ready until both have: the workflow ends
16
+ // successfully and the event of the later one asks again.
17
+ // Exit status: 0 whenever a decision was made, 2 when the input or the config is unusable.
18
+ //
19
+ // Whether the PR can be merged (a conflict with main) is not decided here: finalize-release.sh --phase close looks at it right before
20
+ // it merges and stops with the message to run merge-back again.
21
+ import { readFileSync } from "node:fs";
22
+ import { resolve } from "node:path";
23
+ import { fileURLToPath } from "node:url";
24
+ import { MERGE_BACK_HEAD, requiredCheckProblems } from "../merge/decide.mjs";
25
+
26
+ const skip = (reason, extra = {}) => ({ action: "skip", reason, ...extra });
27
+
28
+ export function decideClose({ pr, checkedSha, eventHeadSha = null, checks = [], changedFiles = [] }, config) {
29
+ const number = pr.number;
30
+ if (pr.state !== "OPEN") return skip(`the pull request is ${pr.state}, not OPEN: nothing to close`, { pr: number });
31
+ if (!MERGE_BACK_HEAD.test(pr.headRefName)) return skip(`${pr.headRefName} is not a merge-back branch (release/vX.Y--to-main)`, { pr: number });
32
+ if (pr.baseRefName !== "main") return skip(`a merge-back PR targets main, this one targets ${pr.baseRefName}`, { pr: number });
33
+ if (pr.isCrossRepository) return skip("the pull request comes from a fork and cannot get the App token", { pr: number });
34
+ if (pr.isDraft) return skip("the pull request is a draft", { pr: number });
35
+ if (eventHeadSha && eventHeadSha !== pr.headRefOid) {
36
+ return skip(`the head moved after the finished run (the run was for ${eventHeadSha}, head now ${pr.headRefOid}): the checks of the new head will start this again`, { pr: number });
37
+ }
38
+ if (checkedSha !== pr.headRefOid) {
39
+ return skip(`a commit was pushed after the checks were read (checked ${checkedSha}, head now ${pr.headRefOid})`, { pr: number });
40
+ }
41
+ const { failed, pending, missing } = requiredCheckProblems(checks, changedFiles, config);
42
+ if (failed.length > 0) return skip(`required check failed: ${failed.join(", ")}`, { pr: number, failed });
43
+ if (pending.length > 0) return skip(`required check still running: ${pending.join(", ")}`, { pr: number });
44
+ if (missing.length > 0) return skip(`required check has not run: ${missing.join(", ")}`, { pr: number });
45
+ return {
46
+ action: "close",
47
+ reason: "every required check passed or was skipped, and the head is the one that was checked",
48
+ pr: number,
49
+ sha: pr.headRefOid,
50
+ line: /^release\/v(\d+\.\d+)--to-main$/.exec(pr.headRefName)[1],
51
+ };
52
+ }
53
+
54
+ const read = (p) => readFileSync(p === "-" ? 0 : p, "utf8");
55
+
56
+ function main(argv) {
57
+ const opt = (k, d) => (argv.includes(`--${k}`) ? argv[argv.indexOf(`--${k}`) + 1] : d);
58
+ const input = opt("input");
59
+ if (!input) throw new Error("missing --input <file|->");
60
+ const config = JSON.parse(read(opt("config", "scripts/merge/merge-config.json")));
61
+ if (!Array.isArray(config.requiredChecks) || config.requiredChecks.length === 0) throw new Error("the config needs a non-empty requiredChecks");
62
+ console.log(JSON.stringify(decideClose(JSON.parse(read(input)), config)));
63
+ }
64
+
65
+ if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
66
+ try {
67
+ main(process.argv.slice(2));
68
+ } catch (e) {
69
+ console.error(`close-ready.mjs: ${e.message}`);
70
+ process.exit(2);
71
+ }
72
+ }
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env bash
2
+ # Whether the merge-back PR of a release line can be closed now (merged, product tag, branches deleted), read from GitHub. The close
3
+ # workflow (close-release.yml) runs it first and goes on only when it says ready=true. It looks once, reads only, and exits 0 for
4
+ # any answer, because a PR that is not ready yet is the normal case for most events (ci and pr-message finish one after the other).
5
+ #
6
+ # usage: close-ready.sh (--pr <number> | --line X.Y) [--head-sha <sha>] [--strict]
7
+ # --pr the PR of the event (github.event.workflow_run.pull_requests[0].number)
8
+ # --line a person started the run: the open PR of release/vX.Y--to-main is the one
9
+ # --head-sha the head_sha of the event (github.event.workflow_run.head_sha), the commit the finished ci or pr-message ran for.
10
+ # A PR whose head is another commit is not closed: the checks of its new head start this again.
11
+ # Leave it out when a person started the run; the head the checks were read for is then the head of the PR.
12
+ # --strict exit 1 when it is not ready (a person started the run and wants to see the failure), instead of 0
13
+ # stdout, to append to $GITHUB_OUTPUT: ready=true|false, reason=<one line>, and when ready: pr=, sha=, line=
14
+ # env: GH_TOKEN (read access to pull requests, checks and statuses), GH_REPO when the checkout has no remote of the repository,
15
+ # MERGE_CONFIG (default scripts/merge/merge-config.json, the file auto-merge reads for the required checks)
16
+ #
17
+ # The decision is scripts/release/close-ready.mjs, over the required checks of scripts/merge/decide.mjs (requiredCheckProblems), the
18
+ # same ones auto-merge reads. The order of the reads is the one of auto-merge: the head first, the checks, the PR last, so a commit
19
+ # pushed in between shows as a different head and the decision skips.
20
+ set -euo pipefail
21
+ source "$(dirname "$0")/../lib/common.sh"
22
+ cd "$REPO_ROOT"
23
+
24
+ pr="" line="" head_sha="" strict=0
25
+ while [[ $# -gt 0 ]]; do
26
+ case "$1" in
27
+ --pr) pr="$2"; shift 2 ;;
28
+ --line) line="$2"; shift 2 ;;
29
+ --head-sha) head_sha="$2"; shift 2 ;;
30
+ --strict) strict=1; shift ;;
31
+ -h|--help) sed -n '2,${/^#/!q; p;}' "$0"; exit 0 ;;
32
+ *) die "unknown argument: $1" ;;
33
+ esac
34
+ done
35
+ config="${MERGE_CONFIG:-scripts/merge/merge-config.json}"
36
+ out="$(mktemp -d)"
37
+ trap 'rm -rf "$out"' EXIT
38
+
39
+ not_ready() { # not_ready <reason>
40
+ warn "$1"
41
+ printf '%s\n' "ready=false" "reason=${1//$'\n'/ }"
42
+ (( strict == 0 )) || exit 1
43
+ exit 0
44
+ }
45
+
46
+ if [[ -z "$pr" ]]; then
47
+ [[ "$line" =~ ^[0-9]+\.[0-9]+$ ]] || die "--pr 或 --line X.Y 要給一個"
48
+ pr="$(gh pr list --base main --head "release/v${line}--to-main" --state open --json number --jq '.[0].number // empty')"
49
+ [[ -n "$pr" ]] || not_ready "release/v${line}--to-main 沒有開著的合回 PR:先開合回 PR(merge-back),或它已經合併了"
50
+ fi
51
+ [[ "$pr" =~ ^[0-9]+$ ]] || not_ready "沒有 PR 編號(${pr})"
52
+
53
+ checked_sha="$(gh pr view "$pr" --json headRefOid --jq .headRefOid)"
54
+ # gh pr checks exits 1 for a failed check and 8 for a pending one, and the decision reads both from the output
55
+ rc=0; gh pr checks "$pr" --json name,bucket > "$out/checks.json" 2> "$out/checks.err" || rc=$?
56
+ cat "$out/checks.err" >&2
57
+ # a PR without any check yet: exit 1, nothing on stdout, "no checks reported" on stderr; the decision reads that as "has not run"
58
+ if (( rc == 1 )) && [[ ! -s "$out/checks.json" ]] && grep -q 'no checks reported on the' "$out/checks.err"; then printf '[]\n' > "$out/checks.json"; fi
59
+ (( rc == 0 || rc == 1 || rc == 8 )) || die "gh pr checks 結束碼 $rc"
60
+ # an empty or non-array output means the checks could not be read; it must not reach the decision as "no check ran"
61
+ jq -e 'type == "array"' "$out/checks.json" > /dev/null || die "gh pr checks 結束碼 ${rc},也沒有印出 checks 的 JSON 陣列"
62
+ gh pr diff "$pr" --name-only | jq -R . | jq -s . > "$out/files.json"
63
+ gh pr view "$pr" --json number,state,isDraft,headRefOid,headRefName,baseRefName,isCrossRepository \
64
+ | jq --arg sha "$checked_sha" --arg event "$head_sha" --slurpfile checks "$out/checks.json" --slurpfile files "$out/files.json" \
65
+ '{pr: ., checkedSha: $sha, eventHeadSha: (if $event == "" then null else $event end), checks: $checks[0], changedFiles: $files[0]}' > "$out/input.json"
66
+ node "$(dirname "$0")/close-ready.mjs" --input "$out/input.json" --config "$config" > "$out/decision.json"
67
+ reason="$(jq -r .reason "$out/decision.json")"
68
+ if [[ "$(jq -r .action "$out/decision.json")" != close ]]; then not_ready "$reason"; fi
69
+ info "可以收尾 PR #${pr}:${reason}" >&2
70
+ printf '%s\n' "ready=true" "reason=$reason" "pr=$(jq -r .pr "$out/decision.json")" "sha=$(jq -r .sha "$out/decision.json")" "line=$(jq -r .line "$out/decision.json")"
@@ -3,7 +3,7 @@
3
3
  # units. Every unit gets the stage the policy file (release-policy.json) gives it at the train's first stage,
4
4
  # so units of one train can sit at different stages. advance-release.sh moves the train on, forward only.
5
5
  #
6
- # usage: cut-release.sh --plan vX.Y.Z --units product-web,product-api,product-sdk-core [--from main|vX.Y.Z] [--hub-dir <path> | --plan-file <path>] [--yes] [--dry-run]
6
+ # usage: cut-release.sh --plan vX.Y.Z --units product-web,product-api,product-sdk-core [--from main|vX.Y.Z] [--stages-override <json>] [--hub-dir <path> | --plan-file <path>] [--yes] [--dry-run]
7
7
  # --plan the Release Plan this train ships (releases/release-plan-vX.Y.Z.md). The plan must be the next one
8
8
  # after the latest plan (plan.sh check). The train's product line is X.Y of the plan and names the
9
9
  # branch release/vX.Y. The opening commit records the plan in a Release-Plan trailer, which
@@ -13,6 +13,10 @@
13
13
  # depend on a listed unit, and units a prerelease unit depends on that have changed (see below).
14
14
  # --from base: main (default) or a product tag such as v1.3.0, for a post-GA hotfix train.
15
15
  # Once the line has a vX.Y.Z tag, --from must name its latest one.
16
+ # --stages-override the stages of some units for this release only, a JSON object {"<unit>":["rc","ga"]}. The policy file is
17
+ # replaced by a copy with those stages before the start stages are computed, and the copy is committed as
18
+ # release-policy.json on the new release branch (the opening commit), so every workflow that stands on the branch reads
19
+ # it. finalize-release.sh leaves release-policy.json out of the merge-back, so main keeps its own policy.
16
20
  # --hub-dir local clone of the primary repository, where a member repository reads the plan from (default:
17
21
  # the directory next to this repository named after product.primary in the policy file).
18
22
  # --plan-file the Release Plan itself, a file named release-plan-vX.Y.Z.md. A member repository whose CI opens the
@@ -40,7 +44,7 @@ set -euo pipefail
40
44
  source "$(dirname "$0")/../lib/common.sh"
41
45
  cd "$REPO_ROOT"
42
46
 
43
- plan="" units_csv="" from="main" plan_file_arg=""
47
+ plan="" units_csv="" from="main" plan_file_arg="" stages_override=""
44
48
  while [[ $# -gt 0 ]]; do
45
49
  case "$1" in
46
50
  --plan) plan="$2"; shift 2 ;;
@@ -48,6 +52,7 @@ while [[ $# -gt 0 ]]; do
48
52
  --from) from="$2"; shift 2 ;;
49
53
  --hub-dir) HUB_DIR="$2"; export HUB_DIR; shift 2 ;;
50
54
  --plan-file) plan_file_arg="$2"; shift 2 ;;
55
+ --stages-override) stages_override="$2"; shift 2 ;;
51
56
  --yes) ASSUME_YES=1; export ASSUME_YES; shift ;;
52
57
  --dry-run) DRY_RUN=1; shift ;;
53
58
  -h|--help) sed -n '2,${/^#/!q; p;}' "$0"; exit 0 ;;
@@ -61,6 +66,23 @@ branch="release/v$line"
61
66
  IFS=, read -r -a units <<<"$units_csv"
62
67
 
63
68
  policy_require
69
+ # The stages of this release are read from the override copy of the policy for everything below (the start stages included), and the
70
+ # copy is written onto the release branch once it exists.
71
+ override_policy=""
72
+ if [[ -n "$stages_override" ]]; then
73
+ stages_override_check "$stages_override"
74
+ override_policy="$(mktemp)"
75
+ # Replaces the exit trap of common.sh, so it still calls report_skipped_external with the original exit status.
76
+ cleanup_override_policy() {
77
+ local rc=$?
78
+ rm -f "$override_policy"
79
+ (exit "$rc")
80
+ report_skipped_external
81
+ }
82
+ trap cleanup_override_policy EXIT
83
+ stages_override_policy "$stages_override" > "$override_policy"
84
+ export RELEASE_POLICY="$override_policy"
85
+ fi
64
86
  bash "$(dirname "$0")/../ci/check-release-policy.sh" || die "政策檔與倉庫不一致,先修正再開線"
65
87
  if [[ -n "$plan_file_arg" ]]; then
66
88
  [[ "${plan_file_arg##*/}" == "release-plan-$plan.md" ]] || die "--plan-file 的檔名需為 release-plan-$plan.md:${plan_file_arg##*/}"
@@ -266,6 +288,15 @@ fi
266
288
  info "起始階段:${stage}(本班單元走過的階段:${union[*]})"
267
289
  if (( DRY_RUN )); then info "[dry-run] $CONFIG 會縮為:"; jq -c '{"last-release-sha", plugins: [.plugins[].type], packages: (.packages | to_entries | map({(.key): {versioning: .value.versioning, "prerelease-type": .value."prerelease-type", "initial-version": .value."initial-version"}}) | add)}' "$tmp"; rm -f "$tmp"; else mv "$tmp" "$CONFIG"; fi
268
290
  run git add "$CONFIG"
291
+ override_note=""
292
+ if [[ -n "$override_policy" ]]; then
293
+ if (( DRY_RUN )); then info "[dry-run] 會把這次發布的階段覆寫寫進 ${branch} 的 release-policy.json:$(jq -c . <<<"$stages_override")"; else
294
+ cp "$override_policy" "$REPO_ROOT/release-policy.json"
295
+ git add -- "$REPO_ROOT/release-policy.json"
296
+ override_note=" 這次發布的階段覆寫 $(jq -c . <<<"$stages_override") 已寫進 release-policy.json,合回時不帶回 main。"
297
+ info "階段覆寫已寫進 ${branch} 的 release-policy.json:$(jq -c . <<<"$stages_override")"
298
+ fi
299
+ fi
269
300
  # The product layer of the document package (docs/product) says "next" for what this train ships; the plan
270
301
  # names the product version, so pin it in the opening commit. The component layer is pinned with each unit's
271
302
  # version by merge-release-pr.sh (scripts/lib/docs.sh).
@@ -281,7 +312,7 @@ if has_docs && [[ -d "$REPO_ROOT/$DOCS_PRODUCT_DIR" ]]; then
281
312
  fi
282
313
  base_sha="$(git rev-parse "${base}^{commit}")"
283
314
  unit_stages="$(for u in "${units[@]}"; do printf '%s=%s ' "$u" "${stage_of[$u]:-待命}"; done)"
284
- body="起始階段 ${stage}:各單元依政策檔取自己的階段(${unit_stages% }),release-please 於本分支以各單元的 versioning 與 prerelease-type 計算版號;advance-release.sh 往後推進,到 ga 時由 finalize-release.sh 收斂。${docs_note}"
315
+ body="起始階段 ${stage}:各單元依政策檔取自己的階段(${unit_stages% }),release-please 於本分支以各單元的 versioning 與 prerelease-type 計算版號;advance-release.sh 往後推進,到 ga 時由 finalize-release.sh 收斂。${docs_note}${override_note}"
285
316
  run git commit -q -m "chore($branch): open train at $stage with ${units[*]}" -m "$body
286
317
 
287
318
  Release-Train-Base: $base_sha