blueprint-shared-templates 0.1.0-0 → 0.2.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 (100) hide show
  1. package/base/.github/CODEOWNERS.example +10 -0
  2. package/base/.github/ISSUE_TEMPLATE/bug.yml +15 -0
  3. package/base/.github/ISSUE_TEMPLATE/feature.yml +19 -0
  4. package/base/.github/ISSUE_TEMPLATE/hotfix.yml +17 -0
  5. package/base/.github/PULL_REQUEST_TEMPLATE.md +13 -0
  6. package/base/.github/actions/setup-workspace/action.yml +54 -0
  7. package/base/.github/version-control.md +22 -0
  8. package/base/.github/workflows/auto-merge.yml +138 -0
  9. package/base/.github/workflows/ci.yml +274 -0
  10. package/base/.github/workflows/main-commit-check.yml +42 -0
  11. package/base/.github/workflows/pr-message.yml +64 -0
  12. package/base/.gitignore +356 -0
  13. package/base/.oxlintrc.json +14 -0
  14. package/base/LICENSE +21 -0
  15. package/base/releases/release-plan.md +39 -0
  16. package/base/scripts/ci/affected.sh +15 -0
  17. package/base/scripts/ci/ai-attribution-list.txt +68 -0
  18. package/base/scripts/ci/check-commit-body.sh +110 -0
  19. package/base/scripts/ci/check-main-commits.sh +31 -0
  20. package/base/scripts/ci/check-pr-title.sh +10 -0
  21. package/base/scripts/ci/ci-scope.conf +23 -0
  22. package/base/scripts/ci/ci-scope.sh +106 -0
  23. package/base/scripts/ci/native-build.sh +40 -0
  24. package/base/scripts/ci/run-affected.sh +48 -0
  25. package/base/scripts/ci/run-units.sh +33 -0
  26. package/base/scripts/hooks/commit-msg +38 -0
  27. package/base/scripts/hooks/pre-push +63 -0
  28. package/base/scripts/lib/common.sh +233 -0
  29. package/base/scripts/lib/retry.mjs +178 -0
  30. package/base/scripts/merge/decide.mjs +130 -0
  31. package/base/scripts/setup/git-hooks.sh +16 -0
  32. package/base/scripts/setup/github-repo-settings.json +8 -0
  33. package/base/scripts/setup/github-repo-settings.sh +110 -0
  34. package/deploy/.github/workflows/deploy-prerelease.yml +157 -0
  35. package/deploy/.github/workflows/deploy-production.yml +173 -0
  36. package/deploy/scripts/deploy/build-bundle.sh +49 -0
  37. package/deploy/scripts/deploy/clone-ops.sh +14 -0
  38. package/deploy/scripts/deploy/plan-production.sh +43 -0
  39. package/deploy/scripts/deploy/promote.sh +35 -0
  40. package/deploy/scripts/deploy/publish-bundle.sh +21 -0
  41. package/deploy/scripts/deploy/route-tag.sh +65 -0
  42. package/deploy/scripts/deploy/run-prerelease.sh +49 -0
  43. package/deploy/scripts/deploy/run-production.sh +51 -0
  44. package/deploy/scripts/deploy/writeback-ops.sh +53 -0
  45. package/deploy/scripts/lib/bundle.sh +52 -0
  46. package/facts-describes.ts +53 -0
  47. package/facts-skeleton.mjs +84 -0
  48. package/hub/.github/workflows/open-release-lines.yml +63 -0
  49. package/hub/scripts/deploy/clone-ops.sh +14 -0
  50. package/hub/scripts/release/dispatch-release-lines.sh +54 -0
  51. package/hub/scripts/release/snapshot-members.sh +85 -0
  52. package/infra/.github/workflows/deploy-prerelease.yml +157 -0
  53. package/infra/.github/workflows/deploy-production.yml +173 -0
  54. package/infra/scripts/deploy/apply-infra.sh +46 -0
  55. package/infra/scripts/deploy/apply-prerelease.sh +22 -0
  56. package/infra/scripts/deploy/apply-production.sh +33 -0
  57. package/infra/scripts/deploy/route-tag.sh +65 -0
  58. package/ops/.github/workflows/ops-deploy.yml +88 -0
  59. package/ops/scripts/deploy/apply-ops.sh +100 -0
  60. package/ops/scripts/deploy/healthz.sh +16 -0
  61. package/ops/scripts/deploy/ops-run.sh +41 -0
  62. package/ops/scripts/deploy/smoke.sh +49 -0
  63. package/ops/scripts/lib/bundle.sh +52 -0
  64. package/package.json +21 -4
  65. package/publish/.github/workflows/publish.yml +299 -0
  66. package/publish/scripts/release/cleanup-private-packages.mjs +107 -0
  67. package/publish/scripts/release/mirror-units.sh +167 -0
  68. package/publish/scripts/release/publish-units.sh +489 -0
  69. package/release/.github/workflows/nightly.yml +93 -0
  70. package/release/.github/workflows/open-release-line.yml +70 -0
  71. package/release/.github/workflows/release-line-finalize.yml +101 -0
  72. package/release/.github/workflows/release-please.yml +148 -0
  73. package/release/release-please-config.json +39 -0
  74. package/release/release-policy.json +38 -0
  75. package/release/schemas/release-policy.schema.json +151 -0
  76. package/release/scripts/ci/check-facts-describes.sh +63 -0
  77. package/release/scripts/ci/check-facts.sh +46 -0
  78. package/release/scripts/ci/check-release-policy-schema.sh +59 -0
  79. package/release/scripts/ci/check-release-policy.sh +123 -0
  80. package/release/scripts/ci/pin-train.sh +29 -0
  81. package/release/scripts/ci/release-train.sh +39 -0
  82. package/release/scripts/ci/validate-json-schema.mjs +17 -0
  83. package/release/scripts/lib/docs.sh +71 -0
  84. package/release/scripts/lib/facts.sh +146 -0
  85. package/release/scripts/lib/release-plan.sh +128 -0
  86. package/release/scripts/lib/release-pr.sh +106 -0
  87. package/release/scripts/lib/release-stages.sh +903 -0
  88. package/release/scripts/release/advance-release.sh +86 -0
  89. package/release/scripts/release/cut-release.sh +313 -0
  90. package/release/scripts/release/dispatch-publish.sh +34 -0
  91. package/release/scripts/release/ensure-stage-deploys.sh +56 -0
  92. package/release/scripts/release/finalize-release.sh +351 -0
  93. package/release/scripts/release/merge-release-pr.sh +233 -0
  94. package/release/scripts/release/open-release-line.sh +22 -0
  95. package/release/scripts/release/plan.sh +45 -0
  96. package/release/scripts/release/snapshot.sh +57 -0
  97. package/release/scripts/release/verify-production.sh +63 -0
  98. package/transform.mjs +64 -0
  99. package/verify/.github/workflows/verify-production.yml +71 -0
  100. package/verify/scripts/release/verify-ops.sh +41 -0
@@ -0,0 +1,173 @@
1
+ name: deploy-production
2
+
3
+ # 這份檔在 deploy 與 infra 兩個角色的目錄各有一份,內容相同。倉有哪個角色,就留下那個角色的 job(compose 的角色區塊 # @if)。
4
+ # 產品 tag 是唯一的上線閘門,逐單元的正式 tag 不觸發,因此本版所有單元有單一的上線時刻。
5
+ # @if deploy
6
+ # release job 一次把本版全部入版單元用 GA 版號重建 bundle,掛在產品 tag 的 GitHub Release,再一起寫進 production 的 ops 宣告。
7
+ # 入版單元由 manifest 與前一個產品 tag 的差異推導,不需另行維護清單。
8
+ # 不沿用 rc 的 bundle:版號寫在 bundle 裡,rc 的 bundle 寫 rc 的版號,GA 要重建(decision ga-rebuild-bundle)。
9
+ # 這個 job 不呼叫 wrangler,也不讀 Cloudflare 的憑證。
10
+ # @end
11
+ # @if infra
12
+ # infra job 一次把本版全部 infra 單元套用到 production:用 wrangler 建立單元宣告的 R2 bucket(scripts/deploy/apply-production.sh)。
13
+ # 套用可以重複執行,已存在的 bucket 視為已套用。
14
+ # @end
15
+ # 兩個角色都有的倉(monorepo)先跑 infra job,release job 用 needs 等它成功才跑,因為 bucket 要在綁定它的 Worker 上線前存在
16
+ # (decision monorepo-release-align)。
17
+
18
+ on:
19
+ push:
20
+ tags: ["v[0-9]*"]
21
+ workflow_dispatch:
22
+ inputs:
23
+ tag:
24
+ description: "product tag, e.g. v1.3.0"
25
+ required: true
26
+ type: string
27
+
28
+ permissions:
29
+ contents: read
30
+
31
+ defaults:
32
+ run:
33
+ shell: bash
34
+
35
+ concurrency:
36
+ group: deploy-production
37
+ cancel-in-progress: false
38
+
39
+ env:
40
+ # a loop of a template script asks twice at most and the CI gate asks twice (decision self-hosted-job-no-wait)
41
+ POLL_TRIES: "2"
42
+ CI_GATE_TRIES: "2"
43
+ CI_GATE_QUEUE_TRIES: "0"
44
+
45
+ jobs:
46
+ # @if infra
47
+ infra:
48
+ # production 只能由產品 tag 套用。免費方案的 private repo 用不了 environment 的 deployment branches
49
+ # and tags,所以由這裡核對:觸發的 ref 必須是 tag,且要套用的 tag 是 v<數字>開頭的產品 tag。
50
+ # 這個檢查寫在 workflow 裡,改 workflow 就能繞過;GitHub 強制的白名單需要 public repo 或 Pro/Team。
51
+ # scripts/deploy/apply-production.sh 會再核對一次 tag 的格式。
52
+ if: >-
53
+ vars.DEPLOY_ENABLED == 'true'
54
+ && startsWith(github.ref, 'refs/tags/v')
55
+ && (
56
+ startsWith(inputs.tag || github.ref_name, 'v0') ||
57
+ startsWith(inputs.tag || github.ref_name, 'v1') ||
58
+ startsWith(inputs.tag || github.ref_name, 'v2') ||
59
+ startsWith(inputs.tag || github.ref_name, 'v3') ||
60
+ startsWith(inputs.tag || github.ref_name, 'v4') ||
61
+ startsWith(inputs.tag || github.ref_name, 'v5') ||
62
+ startsWith(inputs.tag || github.ref_name, 'v6') ||
63
+ startsWith(inputs.tag || github.ref_name, 'v7') ||
64
+ startsWith(inputs.tag || github.ref_name, 'v8') ||
65
+ startsWith(inputs.tag || github.ref_name, 'v9')
66
+ )
67
+ runs-on: self-hosted-linux
68
+ timeout-minutes: 20
69
+ steps:
70
+ - uses: actions/checkout@v7
71
+ with:
72
+ ref: ${{ inputs.tag || github.ref_name }}
73
+ fetch-depth: 0
74
+ - uses: ./.github/actions/setup-workspace
75
+ with:
76
+ restore-from-default: "scripts"
77
+ - name: 套用本版 infra 單元宣告的 R2 bucket 到 production
78
+ run: bash scripts/deploy/apply-production.sh "$TAG"
79
+ env:
80
+ TAG: ${{ inputs.tag || github.ref_name }}
81
+ CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
82
+ CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
83
+ DEPLOY_LOG_DIR: ${{ runner.temp }}/deploy-log
84
+ - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
85
+ if: always()
86
+ with:
87
+ name: apply-production-${{ inputs.tag || github.ref_name }}
88
+ path: ${{ runner.temp }}/deploy-log
89
+ if-no-files-found: ignore
90
+ retention-days: 7
91
+ # @end
92
+
93
+ # @if deploy
94
+ release:
95
+ # @end
96
+ # @if deploy+infra
97
+ needs: infra
98
+ # @end
99
+ # @if deploy
100
+ # production 只能由產品 tag 部署。免費方案的 private repo 用不了 environment 的 deployment branches
101
+ # and tags,所以由這裡核對:觸發的 ref 必須是 tag,且要部署的 tag 是 v<數字>開頭的產品 tag。
102
+ # 這個檢查寫在 workflow 裡,改 workflow 就能繞過;GitHub 強制的白名單需要 public repo 或 Pro/Team。
103
+ # scripts/deploy/run-production.sh 會再核對一次 tag 的格式。
104
+ # 兩個角色都有的倉:infra 被 skipped(DEPLOY_ENABLED 不是 true)時,沒有狀態函式的下游 job 也跟著 skipped,
105
+ # 所以那個倉的 if 用 !cancelled() 讓 release 照條件判斷,infra 失敗時仍不跑。只有 deploy 角色的倉沒有 infra job,那版的 if 不依賴 infra。
106
+ if: >-
107
+ # @end
108
+ # @if deploy+infra
109
+ needs.infra.result != 'failure' && !cancelled() &&
110
+ # @end
111
+ # @if deploy
112
+ startsWith(github.ref, 'refs/tags/v')
113
+ && (
114
+ startsWith(inputs.tag || github.ref_name, 'v0') ||
115
+ startsWith(inputs.tag || github.ref_name, 'v1') ||
116
+ startsWith(inputs.tag || github.ref_name, 'v2') ||
117
+ startsWith(inputs.tag || github.ref_name, 'v3') ||
118
+ startsWith(inputs.tag || github.ref_name, 'v4') ||
119
+ startsWith(inputs.tag || github.ref_name, 'v5') ||
120
+ startsWith(inputs.tag || github.ref_name, 'v6') ||
121
+ startsWith(inputs.tag || github.ref_name, 'v7') ||
122
+ startsWith(inputs.tag || github.ref_name, 'v8') ||
123
+ startsWith(inputs.tag || github.ref_name, 'v9')
124
+ )
125
+ runs-on: self-hosted-linux
126
+ timeout-minutes: 20
127
+ # 掛 Release 與回寫同倉的 ops 宣告都要寫入權限;read 會讓 push 失敗
128
+ permissions:
129
+ contents: write
130
+ actions: write
131
+ steps:
132
+ - uses: actions/checkout@v7
133
+ with:
134
+ ref: ${{ inputs.tag || github.ref_name }}
135
+ fetch-depth: 0
136
+ - uses: ./.github/actions/setup-workspace
137
+ with:
138
+ restore-from-default: "scripts"
139
+ - id: ops-name
140
+ if: vars.OPS_REPO != ''
141
+ env:
142
+ OPS_REPO: ${{ vars.OPS_REPO }}
143
+ run: echo "name=${OPS_REPO#*/}" >> "$GITHUB_OUTPUT"
144
+ - id: app-token
145
+ if: vars.OPS_REPO != ''
146
+ uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
147
+ with:
148
+ client-id: ${{ vars.@@clientIdVariable@@ }}
149
+ private-key: ${{ secrets.@@privateKeySecret@@ }}
150
+ owner: ${{ github.repository_owner }}
151
+ repositories: ${{ steps.ops-name.outputs.name }}
152
+ - name: 用 GA 版號重建入版單元,掛 Release,寫入 production 宣告
153
+ run: |
154
+ if [[ -n "$OPS_TOKEN" ]]; then
155
+ export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraheader"
156
+ GIT_CONFIG_VALUE_0="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$OPS_TOKEN" | base64 | tr -d '\n')"
157
+ export GIT_CONFIG_VALUE_0
158
+ fi
159
+ bash scripts/deploy/run-production.sh "$TAG"
160
+ env:
161
+ TAG: ${{ inputs.tag || github.ref_name }}
162
+ GH_TOKEN: ${{ github.token }}
163
+ OPS_TOKEN: ${{ steps.app-token.outputs.token }}
164
+ OPS_REPO: ${{ vars.OPS_REPO }}
165
+ DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
166
+ DEPLOY_LOG_DIR: ${{ runner.temp }}/deploy-log
167
+ BUNDLE_DIR: ${{ runner.temp }}/bundles
168
+ - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
169
+ with:
170
+ name: deploy-production-${{ inputs.tag || github.ref_name }}
171
+ path: ${{ runner.temp }}/deploy-log
172
+ retention-days: 7
173
+ # @end
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env bash
2
+ # Build a Workers unit and pack its bundle. The hash is what the ops declaration pins and what /healthz
3
+ # must report after the deploy (see scripts/lib/bundle.sh for what the bundle holds and how it is hashed; its package.json keeps only name and version).
4
+ # Real runs build with pnpm, the way the unit's stack builds it; a test puts a stub pnpm on PATH.
5
+ #
6
+ # usage: build-bundle.sh <unit> <version> [--out <dir>]
7
+ # The unit's package.json must carry <version>, so a GA rebuild is made from the checkout of the product
8
+ # tag, where release-please has already written the GA version.
9
+ # Prints key=value lines on stdout (usable as $GITHUB_OUTPUT): bundle (the .tgz), asset (its file name),
10
+ # sha256 (the bundle hash), path (the unit directory).
11
+ # env: DEPLOY_LOG_DIR where bundles.log is appended (default tmp/deploy-log)
12
+ set -euo pipefail
13
+ source "$(dirname "$0")/../lib/common.sh"
14
+ source "$(dirname "$0")/../lib/bundle.sh"
15
+ cd "$REPO_ROOT"
16
+ unit="${1:?usage: build-bundle.sh <unit> <version> [--out <dir>]}"
17
+ version="${2:?missing version}"
18
+ shift 2
19
+ out="${TMPDIR:-/tmp}/bundles"
20
+ while [[ $# -gt 0 ]]; do
21
+ case "$1" in
22
+ --out) out="$2"; shift 2 ;;
23
+ *) printf '%s\n' >&2 "unknown argument: $1"; exit 2 ;;
24
+ esac
25
+ done
26
+ path="$(unit_path "$unit")" || die "找不到單元 $unit 的目錄"
27
+ have="$(jq -r .version "$path/package.json")"
28
+ [[ "$have" == "$version" ]] || die "$path/package.json 的版號是 ${have},不是要建置的 ${version}:從對應的 tag 檢出再建置"
29
+
30
+ # {./dir}... builds the unit and the workspace packages it depends on, first; ./dir... would build the unit alone
31
+ pnpm --filter "{./$path}..." build >&2
32
+ # the bundle is staged, not packed from the unit directory, so its package.json can hold only name and version
33
+ [[ -d "$REPO_ROOT/$path/dist" || -d "$REPO_ROOT/$path/public" ]] || die "$path 建置後沒有 dist/ 或 public/"
34
+ stage="$(mktemp -d)"
35
+ trap 'rm -rf "$stage"' EXIT
36
+ bundle_stage "$REPO_ROOT/$path" "$stage"
37
+ hash="$(bundle_hash "$stage")" || die "$path 建置後沒有 dist/ 或 public/"
38
+ mkdir -p "$out"
39
+ asset="$unit-$version.tgz"
40
+ files=()
41
+ while IFS= read -r f; do files+=("$f"); done < <(bundle_files "$stage")
42
+ tar -czf "$out/$asset" -C "$stage" "${files[@]}"
43
+ printf '%s\n' "$hash" > "$out/$unit-$version.sha256"
44
+
45
+ log_dir="${DEPLOY_LOG_DIR:-$REPO_ROOT/tmp/deploy-log}"
46
+ mkdir -p "$log_dir"
47
+ printf '%s\n' "$(date -u '+%Y-%m-%d %H:%M') | build | $unit | $version | $asset | sha256:$hash" >> "$log_dir/bundles.log"
48
+ printf '%s\n' >&2 "[build-bundle] $unit $version → $asset sha256:$hash"
49
+ printf '%s=%s\n' bundle "$out/$asset" asset "$asset" sha256 "$hash" path "$path"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env bash
2
+ # Clone the repository that holds the ops declaration (poly-ops), for a repository that does not hold it
3
+ # itself. A repository that does (the monorepo) never calls this.
4
+ #
5
+ # usage: clone-ops.sh <dest>
6
+ # env: OPS_REPO owner/name of the ops repository
7
+ # OPS_REMOTE the URL to clone from instead of https://github.com/$OPS_REPO.git (a local bare repository in tests)
8
+ # The token goes to git through GIT_CONFIG_* in the environment of the step, never into a URL or into .git/config.
9
+ set -euo pipefail
10
+ dest="${1:?usage: clone-ops.sh <dest>}"
11
+ remote="${OPS_REMOTE:-https://github.com/${OPS_REPO:?OPS_REPO is required}.git}"
12
+ rm -rf "$dest"
13
+ git clone --quiet "$remote" "$dest"
14
+ printf '%s\n' >&2 "[clone-ops] $remote → $dest"
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env bash
2
+ # Which units a product tag puts online: the units whose version in .release-please-manifest.json differs from the
3
+ # previous product tag, and that deploy to the GA environment (a web, service or backend unit under apps/ or services/ whose
4
+ # wrangler.toml has an [env.<that environment>] table; a unit under infra/ is applied by the infra role, not built here). The units are derived from the manifest and the previous tag, so nobody keeps a list.
5
+ # Prints key=value lines (usable as $GITHUB_OUTPUT): product (X.Y.Z), previous (the previous product tag or empty),
6
+ # services (a JSON array of {unit, version, env}).
7
+ #
8
+ # usage: plan-production.sh <product tag> the tag must be checked out
9
+ set -euo pipefail
10
+ source "$(dirname "$0")/../lib/common.sh"
11
+ cd "$REPO_ROOT"
12
+ ref="${1:?usage: plan-production.sh <product tag>}"
13
+ [[ "$ref" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "$ref 不是產品 tag(vX.Y.Z)"
14
+ product="${ref#v}"
15
+ # 產品 tag 之間沒有祖先關係(發布分支以 squash 合回),以版本序取前一版;用 --merged 會查無,
16
+ # 讓「本版入版」退化為「全部單元」而每次都重新寫入所有服務的宣告
17
+ prev="$(git tag --list 'v[0-9]*' --sort=-v:refname | awk -v c="$ref" 'f{print;exit} $0==c{f=1}')"
18
+ if [[ -n "$prev" ]]; then
19
+ changed=$(jq -s -r '.[0] as $p | .[1] | to_entries[] | select(.value != $p[.key]) | .key' \
20
+ <(git show "$prev:$MANIFEST") "$MANIFEST")
21
+ else
22
+ changed=$(jq -r 'keys[]' "$MANIFEST")
23
+ fi
24
+ services="[]"
25
+ for path in $changed; do
26
+ # unit 名稱一律取設定的 component,設定沒有時才退回資料夾名稱
27
+ unit="$(unit_component "$path")"
28
+ ver="$(manifest_version "$path")"
29
+ case "$(policy_unit_kind "$unit")" in
30
+ service|backend|web) ;;
31
+ *) info "略過非部署單元:$path" >&2; continue ;;
32
+ esac
33
+ case "${path%%/*}" in
34
+ apps|services) ;;
35
+ *) info "略過不在 apps/、services/ 之下的單元:$path" >&2; continue ;;
36
+ esac
37
+ env="$(stage_target "$unit" ga || true)"
38
+ if [[ -z "$env" ]] || ! grep -q "^\[env\.${env}\]" "$path/wrangler.toml" 2>/dev/null; then
39
+ info "略過沒有 GA 環境設定的單元:$path" >&2; continue
40
+ fi
41
+ services="$(jq -c --arg u "$unit" --arg v "$ver" --arg e "$env" '. + [{unit: $u, version: $v, env: $e}]' <<<"$services")"
42
+ done
43
+ printf '%s=%s\n' product "$product" previous "$prev" services "$services"
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env bash
2
+ # Write a bundle into the ops declaration of an environment. This is the promotion of GitOps: what changes is
3
+ # the declaration, not the Worker. The declaration is ops/versions.json:
4
+ # { "<env>": null | { "<unit>": { "version": "...", "sha256": "<bundle hash>", "bundle": { "repo": "owner/name", "tag": "...", "asset": "..." } } } }
5
+ # A unit's entry is replaced as a whole, so a rerun leaves the same file.
6
+ #
7
+ # usage: promote.sh <env> <unit> <version> <sha256> --repo <owner/name> --tag <tag> --asset <file> [--ops-root <dir>]
8
+ # --ops-root the checkout that holds ops/ (default the repository root; a poly repository passes its clone of poly-ops)
9
+ set -euo pipefail
10
+ env_name="${1:?usage: promote.sh <env> <unit> <version> <sha256> --repo R --tag T --asset A}"
11
+ unit="${2:?missing unit}"
12
+ version="${3:?missing version}"
13
+ sha="${4:?missing sha256}"
14
+ shift 4
15
+ repo="" tag="" asset="" ops_root=""
16
+ while [[ $# -gt 0 ]]; do
17
+ case "$1" in
18
+ --repo) repo="$2"; shift 2 ;;
19
+ --tag) tag="$2"; shift 2 ;;
20
+ --asset) asset="$2"; shift 2 ;;
21
+ --ops-root) ops_root="$2"; shift 2 ;;
22
+ *) printf '%s\n' >&2 "unknown argument: $1"; exit 2 ;;
23
+ esac
24
+ done
25
+ [[ -n "$repo" && -n "$tag" && -n "$asset" ]] || { printf '%s\n' >&2 "--repo、--tag、--asset 必填"; exit 2; }
26
+ [[ "$env_name" =~ ^[a-z][a-z0-9-]*$ ]] || { printf '%s\n' >&2 "env 需為小寫英數與連字號(環境名由 release-policy.json 的 targets 決定)"; exit 2; }
27
+ [[ "$sha" =~ ^[0-9a-f]{64}$ ]] || { printf '%s\n' >&2 "sha256 需為 64 位小寫十六進位"; exit 2; }
28
+ root="${ops_root:-$(git rev-parse --show-toplevel)}"
29
+ f="$root/ops/versions.json"
30
+ [[ -f "$f" ]] || { printf '%s\n' >&2 "找不到 ${f}"; exit 1; }
31
+ jq -e --arg e "$env_name" 'has($e)' "$f" >/dev/null || { printf '%s\n' >&2 "$f 沒有 $env_name 環境的宣告"; exit 1; }
32
+ tmp="$(mktemp)"
33
+ jq --arg e "$env_name" --arg u "$unit" --arg v "$version" --arg s "$sha" --arg r "$repo" --arg t "$tag" --arg a "$asset" \
34
+ '.[$e] = ((.[$e] // {}) + {($u): {version: $v, sha256: $s, bundle: {repo: $r, tag: $t, asset: $a}}})' "$f" > "$tmp" && mv "$tmp" "$f"
35
+ printf '%s\n' "[promote] $env_name/$unit → $version sha256:${sha:0:12}…"
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env bash
2
+ # Attach a bundle and its hash to the GitHub Release of a tag, creating the Release when the tag has none
3
+ # (a product tag has none until now; release-please creates the Release of a unit tag). The Release is where
4
+ # the ops declaration says to fetch the bundle from.
5
+ #
6
+ # usage: publish-bundle.sh <tag> <bundle.tgz> (reads the hash from the .sha256 file next to the bundle)
7
+ # env: GH_TOKEN a token that can write Releases of this repository
8
+ # Prints the repository (owner/name) on stdout as repo=<owner/name>.
9
+ set -euo pipefail
10
+ tag="${1:?usage: publish-bundle.sh <tag> <bundle.tgz>}"
11
+ bundle="${2:?missing bundle}"
12
+ [[ -f "$bundle" ]] || { printf '%s\n' >&2 "找不到 bundle:$bundle"; exit 1; }
13
+ sum="${bundle%.tgz}.sha256"
14
+ [[ -f "$sum" ]] || { printf '%s\n' >&2 "找不到雜湊檔:$sum"; exit 1; }
15
+ repo="${GITHUB_REPOSITORY:-$(gh repo view --json nameWithOwner --jq .nameWithOwner)}"
16
+ if ! gh release view "$tag" --repo "$repo" >/dev/null 2>&1; then
17
+ gh release create "$tag" --repo "$repo" --verify-tag --title "$tag" --notes "bundles of $tag"
18
+ fi
19
+ gh release upload "$tag" "$bundle" "$sum" --repo "$repo" --clobber
20
+ printf '%s\n' >&2 "[publish-bundle] $tag ← $(basename "$bundle")"
21
+ printf '%s=%s\n' repo "$repo"
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env bash
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
4
+ #
5
+ # A prerelease tag {unit}/vX.Y.Z-<stage>.N goes where the policy file (release-policy.json) sends that stage
6
+ # for that unit: the unit's own targets, else the repository targets (stage_target in
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>)
10
+ # - anything else (package, sdk, mobile, desktop): nothing to deploy here. Packages are published by
11
+ # 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).
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).
18
+ #
19
+ # usage: route-tag.sh <tag>
20
+ # env: RELEASE_POLICY the policy file to read (default release-policy.json at the repository root)
21
+ set -euo pipefail
22
+ source "$(dirname "$0")/../lib/common.sh"
23
+ cd "$REPO_ROOT"
24
+ tag="${1:?usage: route-tag.sh <tag>}"
25
+ # Every value below lands in $GITHUB_OUTPUT as key=value; a newline in the tag would add a line of its
26
+ # own and could override skip. Tags only ever carry these characters, so anything else is refused.
27
+ [[ "$tag" =~ ^[A-Za-z0-9._/-]+$ ]] || die "tag 含不允許的字元,只接受英數與 . _ / -"
28
+
29
+ unit="" version="" stage="" env="" kind=none skip=true reason=""
30
+ emit() {
31
+ printf '%s=%s\n' unit "$unit" version "$version" stage "$stage" env "$env" kind "$kind" \
32
+ skip "$skip" reason "$reason"
33
+ exit 0
34
+ }
35
+ skip_with() { reason="$1"; emit; }
36
+
37
+ [[ "$tag" == */v* ]] || skip_with "tag $tag 不是 {unit}/vX.Y.Z 格式"
38
+ unit="${tag%%/*}"; version="${tag#*/v}"
39
+ [[ "$version" == *-* ]] || skip_with "正式版不在這裡部署:正式環境由產品 tag 統一觸發"
40
+ pre="${version#*-}"; stage="${pre%%.*}"
41
+ if ! is_stage "$stage" || [[ "$stage" == ga ]]; then
42
+ skip_with "版號 $version 的預發布階段「${stage}」不是 ${RELEASE_STAGES[*]:0:3} 之一"
43
+ fi
44
+ policy_unit_exists "$unit" || skip_with "$unit 不在政策檔($(basename "$(policy_file)"))的 units 裡,不是發布單元"
45
+
46
+ case "$(policy_unit_kind "$unit")" in
47
+ service|backend|web)
48
+ path="$(unit_path "$unit" 2>/dev/null || true)"
49
+ case "${path%%/*}" in
50
+ apps|services) kind=service ;;
51
+ infra) kind=infra ;;
52
+ *) skip_with "$unit 的目錄(${path:-找不到})不在 apps/、services/ 或 infra/ 之下,不是部署或套用單元" ;;
53
+ esac
54
+ env="$(stage_target "$unit" "$stage" || true)"
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
+ ;;
59
+ *)
60
+ kind=none
61
+ skip_with "$unit 不是部署單元(政策檔的 kind 是 $(policy_unit_kind "$unit"),由 publish.yml 發布,或不在第一版)"
62
+ ;;
63
+ esac
64
+ skip=false
65
+ emit
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env bash
2
+ # What deploy-prerelease.yml does for a unit tag {unit}/vX.Y.Z-<stage>.N, in one script so a local run and the
3
+ # workflow share it. Three steps, the same shape as git-flow-template's three (image, declaration, apply), with the
4
+ # last one moved to ops:
5
+ # 1. build the unit and pack its bundle, hash it, and attach both to the GitHub Release of the tag
6
+ # 2. write the bundle into the ops declaration of the stage's environment and commit it back
7
+ # (a monorepo writes its own ops/; a poly repository writes poly-ops, named by OPS_REPO)
8
+ # 3. a monorepo starts ops-deploy.yml for the environment; poly-ops starts itself on the push of step 2
9
+ # This script never calls wrangler and never reads a Cloudflare credential.
10
+ #
11
+ # usage: run-prerelease.sh <unit tag>
12
+ # env: DEFAULT_BRANCH the default branch of this repository (default main)
13
+ # OPS_REPO owner/name of poly-ops; empty for the monorepo, which holds ops/ itself
14
+ # OPS_REMOTE see clone-ops.sh
15
+ # BUNDLE_DIR where the bundle is written (default a temporary directory)
16
+ # GH_TOKEN a token that can write Releases and dispatch workflows of this repository
17
+ set -euo pipefail
18
+ source "$(dirname "$0")/../lib/common.sh"
19
+ cd "$REPO_ROOT"
20
+ here="$(dirname "$0")"
21
+ tag="${1:?usage: run-prerelease.sh <unit tag>}"
22
+ branch="${DEFAULT_BRANCH:-main}"
23
+ route="$(bash "$here/route-tag.sh" "$tag")"
24
+ get() { sed -n "s/^$1=//p" <<<"$route"; }
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
27
+ if [[ "$(get kind)" != service ]]; then info "⊘ $tag 不是這裡部署的單元(kind $(get kind))"; exit 0; fi
28
+ unit="$(get unit)" version="$(get version)" env_name="$(get env)"
29
+
30
+ out="${BUNDLE_DIR:-$(mktemp -d)}"
31
+ built="$(bash "$here/build-bundle.sh" "$unit" "$version" --out "$out")"
32
+ bundle="$(sed -n 's/^bundle=//p' <<<"$built")" asset="$(sed -n 's/^asset=//p' <<<"$built")" sha="$(sed -n 's/^sha256=//p' <<<"$built")"
33
+ repo="$(bash "$here/publish-bundle.sh" "$tag" "$bundle" | sed -n 's/^repo=//p')"
34
+
35
+ ops_root=""
36
+ if [[ -n "${OPS_REPO:-}" ]]; then
37
+ ops_root="$out/ops"
38
+ bash "$here/clone-ops.sh" "$ops_root"
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
+ 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 宣告"
47
+ if [[ -z "$ops_root" ]]; then
48
+ gh workflow run ops-deploy.yml --ref "$branch" -f env="$env_name" -f units="$unit"
49
+ fi
@@ -0,0 +1,51 @@
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
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
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.
7
+ # This script never calls wrangler and never reads a Cloudflare credential.
8
+ #
9
+ # usage: run-production.sh <product tag> the tag must be checked out
10
+ # env: the same as run-prerelease.sh
11
+ set -euo pipefail
12
+ source "$(dirname "$0")/../lib/common.sh"
13
+ cd "$REPO_ROOT"
14
+ here="$(dirname "$0")"
15
+ tag="${1:?usage: run-production.sh <product tag>}"
16
+ branch="${DEFAULT_BRANCH:-main}"
17
+ # a private repository on GitHub Free has no deployment branch and tag rules for environments, so the tag is
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
+
26
+ out="${BUNDLE_DIR:-$(mktemp -d)}"
27
+ ops_root=""
28
+ if [[ -n "${OPS_REPO:-}" ]]; then
29
+ ops_root="$out/ops"
30
+ bash "$here/clone-ops.sh" "$ops_root"
31
+ 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
+ fi
36
+ envs=""
37
+ for i in $(seq 0 $((n - 1))); do
38
+ unit="$(jq -r ".[$i].unit" <<<"$services")" version="$(jq -r ".[$i].version" <<<"$services")" env_name="$(jq -r ".[$i].env" <<<"$services")"
39
+ built="$(bash "$here/build-bundle.sh" "$unit" "$version" --out "$out")"
40
+ bundle="$(sed -n 's/^bundle=//p' <<<"$built")" asset="$(sed -n 's/^asset=//p' <<<"$built")" sha="$(sed -n 's/^sha256=//p' <<<"$built")"
41
+ 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"}
43
+ [[ ",$envs," == *",$env_name,"* ]] || envs="${envs:+$envs,}$env_name"
44
+ done
45
+ OPS_ROOT="$ops_root" DEFAULT_BRANCH="$branch" bash "$here/writeback-ops.sh" --require-change "chore(ops): 產品 ${tag} 的入版服務寫入 ${envs} 宣告"
46
+ if [[ -z "$ops_root" ]]; then
47
+ for e in ${envs//,/ }; do
48
+ units="$(jq -r --arg e "$e" '[.[] | select(.env == $e) | .unit] | join(",")' <<<"$services")"
49
+ gh workflow run ops-deploy.yml --ref "$branch" -f env="$e" -f units="$units"
50
+ done
51
+ fi
@@ -0,0 +1,53 @@
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.
7
+ #
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).
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)
19
+ set -euo pipefail
20
+ require=0; [[ "${1:-}" == --require-change ]] && { require=1; shift; }
21
+ msg="${1:?usage: writeback-ops.sh [--require-change] <commit message>}"
22
+ branch="${DEFAULT_BRANCH:?DEFAULT_BRANCH is required}"
23
+ root="${OPS_ROOT:-$(git rev-parse --show-toplevel)}"
24
+ g() { git -C "$root" "$@"; }
25
+
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
+ g config user.name "github-actions[bot]"
44
+ 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}"
52
+ done
53
+ printf '%s\n' >&2 "::error::ops 回寫推送失敗"; exit 1
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env bash
2
+ # The bundle of a Workers unit: what a deploy ships and what the ops declaration pins. Sourced by the
3
+ # deploy scripts; do not execute. templates/ops carries the same file, so a repository that only deploys
4
+ # (poly-ops) and one that only builds (poly-app, poly-service) compute the same hash. A test keeps the two
5
+ # copies byte for byte equal.
6
+ #
7
+ # A bundle is the unit's wrangler.toml and package.json plus the built output: dist/ (a service, and the
8
+ # output of `wrangler deploy --dry-run --outdir dist`) and public/ (the static assets of a web unit).
9
+ # The package.json in a bundle holds only the name and the version of the unit's. The built output has
10
+ # its dependencies bundled in and the version is already in public/healthz.json, so a bundle needs no
11
+ # dependency declaration, and a `workspace:*` one would make pnpm fail in the unpacked directory (it is
12
+ # outside the workspace). bundle_stage writes that file.
13
+ # Its hash is the SHA-256 of the sorted "<sha256> <path>" line of every file in it, not of the archive,
14
+ # because a tar and a gzip differ from one run to the next while the files do not. The hash is taken
15
+ # over the staged directory, so it covers the reduced package.json, not the unit's own.
16
+
17
+ # sha256_of <file> → the hex digest of a file
18
+ sha256_of() {
19
+ if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi
20
+ }
21
+
22
+ # bundle_files <unit dir> → the paths of the bundle relative to the unit directory, sorted, one per line
23
+ bundle_files() {
24
+ (
25
+ cd "$1" || exit 1
26
+ local f
27
+ for f in wrangler.toml package.json; do [[ -f "$f" ]] && printf '%s\n' "$f"; done
28
+ for f in dist public; do [[ -d "$f" ]] && find "$f" -type f; done
29
+ ) | LC_ALL=C sort
30
+ }
31
+
32
+ # bundle_stage <unit dir> <dest dir> → copies the bundle of the unit into <dest dir>, with a package.json
33
+ # that keeps only the name and the version of the unit's; hash and pack the staged directory
34
+ bundle_stage() {
35
+ local src="$1" dest="$2" f
36
+ while IFS= read -r f; do
37
+ mkdir -p "$dest/$(dirname "$f")"
38
+ if [[ "$f" == package.json ]]; then
39
+ jq '{name, version}' "$src/$f" > "$dest/$f"
40
+ else
41
+ cp "$src/$f" "$dest/$f"
42
+ fi
43
+ done < <(bundle_files "$src")
44
+ }
45
+
46
+ # bundle_hash <bundle dir> → the hash of the bundle in that directory (a staged one, or an unpacked one); fails when it has no built output
47
+ bundle_hash() {
48
+ local dir="$1" f lines=""
49
+ [[ -d "$dir/dist" || -d "$dir/public" ]] || return 1
50
+ while IFS= read -r f; do lines+="$(sha256_of "$dir/$f") $f"$'\n'; done < <(bundle_files "$dir")
51
+ if command -v sha256sum >/dev/null 2>&1; then printf '%s' "$lines" | sha256sum | cut -d' ' -f1; else printf '%s' "$lines" | shasum -a 256 | cut -d' ' -f1; fi
52
+ }