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.
- package/base/.github/CODEOWNERS.example +10 -0
- package/base/.github/ISSUE_TEMPLATE/bug.yml +15 -0
- package/base/.github/ISSUE_TEMPLATE/feature.yml +19 -0
- package/base/.github/ISSUE_TEMPLATE/hotfix.yml +17 -0
- package/base/.github/PULL_REQUEST_TEMPLATE.md +13 -0
- package/base/.github/actions/setup-workspace/action.yml +54 -0
- package/base/.github/version-control.md +22 -0
- package/base/.github/workflows/auto-merge.yml +138 -0
- package/base/.github/workflows/ci.yml +274 -0
- package/base/.github/workflows/main-commit-check.yml +42 -0
- package/base/.github/workflows/pr-message.yml +64 -0
- package/base/.gitignore +356 -0
- package/base/.oxlintrc.json +14 -0
- package/base/LICENSE +21 -0
- package/base/releases/release-plan.md +39 -0
- package/base/scripts/ci/affected.sh +15 -0
- package/base/scripts/ci/ai-attribution-list.txt +68 -0
- package/base/scripts/ci/check-commit-body.sh +110 -0
- package/base/scripts/ci/check-main-commits.sh +31 -0
- package/base/scripts/ci/check-pr-title.sh +10 -0
- package/base/scripts/ci/ci-scope.conf +23 -0
- package/base/scripts/ci/ci-scope.sh +106 -0
- package/base/scripts/ci/native-build.sh +40 -0
- package/base/scripts/ci/run-affected.sh +48 -0
- package/base/scripts/ci/run-units.sh +33 -0
- package/base/scripts/hooks/commit-msg +38 -0
- package/base/scripts/hooks/pre-push +63 -0
- package/base/scripts/lib/common.sh +233 -0
- package/base/scripts/lib/retry.mjs +178 -0
- package/base/scripts/merge/decide.mjs +130 -0
- package/base/scripts/setup/git-hooks.sh +16 -0
- package/base/scripts/setup/github-repo-settings.json +8 -0
- package/base/scripts/setup/github-repo-settings.sh +110 -0
- package/deploy/.github/workflows/deploy-prerelease.yml +157 -0
- package/deploy/.github/workflows/deploy-production.yml +173 -0
- package/deploy/scripts/deploy/build-bundle.sh +49 -0
- package/deploy/scripts/deploy/clone-ops.sh +14 -0
- package/deploy/scripts/deploy/plan-production.sh +43 -0
- package/deploy/scripts/deploy/promote.sh +35 -0
- package/deploy/scripts/deploy/publish-bundle.sh +21 -0
- package/deploy/scripts/deploy/route-tag.sh +65 -0
- package/deploy/scripts/deploy/run-prerelease.sh +49 -0
- package/deploy/scripts/deploy/run-production.sh +51 -0
- package/deploy/scripts/deploy/writeback-ops.sh +53 -0
- package/deploy/scripts/lib/bundle.sh +52 -0
- package/facts-describes.ts +53 -0
- package/facts-skeleton.mjs +84 -0
- package/hub/.github/workflows/open-release-lines.yml +63 -0
- package/hub/scripts/deploy/clone-ops.sh +14 -0
- package/hub/scripts/release/dispatch-release-lines.sh +54 -0
- package/hub/scripts/release/snapshot-members.sh +85 -0
- package/infra/.github/workflows/deploy-prerelease.yml +157 -0
- package/infra/.github/workflows/deploy-production.yml +173 -0
- package/infra/scripts/deploy/apply-infra.sh +46 -0
- package/infra/scripts/deploy/apply-prerelease.sh +22 -0
- package/infra/scripts/deploy/apply-production.sh +33 -0
- package/infra/scripts/deploy/route-tag.sh +65 -0
- package/ops/.github/workflows/ops-deploy.yml +88 -0
- package/ops/scripts/deploy/apply-ops.sh +100 -0
- package/ops/scripts/deploy/healthz.sh +16 -0
- package/ops/scripts/deploy/ops-run.sh +41 -0
- package/ops/scripts/deploy/smoke.sh +49 -0
- package/ops/scripts/lib/bundle.sh +52 -0
- package/package.json +21 -4
- package/publish/.github/workflows/publish.yml +299 -0
- package/publish/scripts/release/cleanup-private-packages.mjs +107 -0
- package/publish/scripts/release/mirror-units.sh +167 -0
- package/publish/scripts/release/publish-units.sh +489 -0
- package/release/.github/workflows/nightly.yml +93 -0
- package/release/.github/workflows/open-release-line.yml +70 -0
- package/release/.github/workflows/release-line-finalize.yml +101 -0
- package/release/.github/workflows/release-please.yml +148 -0
- package/release/release-please-config.json +39 -0
- package/release/release-policy.json +38 -0
- package/release/schemas/release-policy.schema.json +151 -0
- package/release/scripts/ci/check-facts-describes.sh +63 -0
- package/release/scripts/ci/check-facts.sh +46 -0
- package/release/scripts/ci/check-release-policy-schema.sh +59 -0
- package/release/scripts/ci/check-release-policy.sh +123 -0
- package/release/scripts/ci/pin-train.sh +29 -0
- package/release/scripts/ci/release-train.sh +39 -0
- package/release/scripts/ci/validate-json-schema.mjs +17 -0
- package/release/scripts/lib/docs.sh +71 -0
- package/release/scripts/lib/facts.sh +146 -0
- package/release/scripts/lib/release-plan.sh +128 -0
- package/release/scripts/lib/release-pr.sh +106 -0
- package/release/scripts/lib/release-stages.sh +903 -0
- package/release/scripts/release/advance-release.sh +86 -0
- package/release/scripts/release/cut-release.sh +313 -0
- package/release/scripts/release/dispatch-publish.sh +34 -0
- package/release/scripts/release/ensure-stage-deploys.sh +56 -0
- package/release/scripts/release/finalize-release.sh +351 -0
- package/release/scripts/release/merge-release-pr.sh +233 -0
- package/release/scripts/release/open-release-line.sh +22 -0
- package/release/scripts/release/plan.sh +45 -0
- package/release/scripts/release/snapshot.sh +57 -0
- package/release/scripts/release/verify-production.sh +63 -0
- package/transform.mjs +64 -0
- package/verify/.github/workflows/verify-production.yml +71 -0
- 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,46 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Create the R2 buckets that the wrangler.toml of an infra unit declares for one environment. A bucket that exists
|
|
3
|
+
# already counts as applied, so a rerun changes nothing. "Exists" is told by the message "already exists" or the code 10004 (owned by you)
|
|
4
|
+
# or 10073 (BucketConflict, the code the R2 error table lists). This is the only script of the infra role that runs wrangler, and
|
|
5
|
+
# the only one that reads the Cloudflare credentials (decision infra-apply-on-tag).
|
|
6
|
+
#
|
|
7
|
+
# The declaration is the array of tables [[env.<env>.r2_buckets]] of <unit dir>/wrangler.toml, one `bucket_name` each.
|
|
8
|
+
# Nothing is deleted: a bucket that the declaration no longer names stays where it is.
|
|
9
|
+
#
|
|
10
|
+
# usage: apply-infra.sh <env> <unit dir>
|
|
11
|
+
# env: WRANGLER the command that runs wrangler (default `pnpm exec wrangler`; offline: the stand-in)
|
|
12
|
+
# DEPLOY_LOG_DIR where <env>.log is appended (default tmp/deploy-log)
|
|
13
|
+
# CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID read by wrangler; set only by the workflow step that applies
|
|
14
|
+
set -euo pipefail
|
|
15
|
+
env_name="${1:?usage: apply-infra.sh <env> <unit dir>}"
|
|
16
|
+
dir="${2:?usage: apply-infra.sh <env> <unit dir>}"
|
|
17
|
+
[[ "$env_name" =~ ^[a-z][a-z0-9-]*$ ]] || { printf '%s\n' >&2 "env 需為小寫英數與連字號(環境名由 release-policy.json 的 targets 決定)"; exit 2; }
|
|
18
|
+
root="$(git rev-parse --show-toplevel)"
|
|
19
|
+
toml="$root/$dir/wrangler.toml"
|
|
20
|
+
[[ -f "$toml" ]] || { printf '%s\n' >&2 "找不到 $toml"; exit 1; }
|
|
21
|
+
read -r -a wrangler <<<"${WRANGLER:-pnpm exec wrangler}"
|
|
22
|
+
log_dir="${DEPLOY_LOG_DIR:-$root/tmp/deploy-log}"
|
|
23
|
+
mkdir -p "$log_dir"
|
|
24
|
+
|
|
25
|
+
names=()
|
|
26
|
+
while IFS= read -r n; do names+=("$n"); done < <(awk -v table="[[env.${env_name}.r2_buckets]]" '
|
|
27
|
+
/^[[:space:]]*\[/ { inside = ($0 == table); next }
|
|
28
|
+
inside && /^[[:space:]]*bucket_name[[:space:]]*=/ { sub(/^[^"]*"/, ""); sub(/".*$/, ""); print }
|
|
29
|
+
' "$toml")
|
|
30
|
+
if (( ${#names[@]} == 0 )); then
|
|
31
|
+
printf '%s\n' "⊘ $dir/wrangler.toml 沒有 $env_name 環境的 R2 bucket 宣告,沒有要套用的資源" | tee -a "$log_dir/$env_name.log"
|
|
32
|
+
exit 0
|
|
33
|
+
fi
|
|
34
|
+
|
|
35
|
+
for name in "${names[@]}"; do
|
|
36
|
+
if out="$("${wrangler[@]}" r2 bucket create "$name" 2>&1)"; then
|
|
37
|
+
result=created
|
|
38
|
+
elif grep -qiE 'already exists|\[code: (10004|10073)\]' <<<"$out"; then
|
|
39
|
+
result=exists
|
|
40
|
+
else
|
|
41
|
+
printf '%s\n' "$out" >&2
|
|
42
|
+
printf '%s\n' "✗ 建立 R2 bucket $name 失敗" >&2
|
|
43
|
+
exit 1
|
|
44
|
+
fi
|
|
45
|
+
printf '%s\n' "r2 bucket $name ($env_name): $result" | tee -a "$log_dir/$env_name.log"
|
|
46
|
+
done
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# What the apply job of deploy-prerelease.yml does for a unit tag {unit}/vX.Y.Z-<stage>.N in a repository with the infra role: apply the
|
|
3
|
+
# resources of the unit to the environment that the stage goes to. The tag is routed by route-tag.sh, the same file as the deploy
|
|
4
|
+
# role has (the target of the stage in release-policy.json, an [env.<env>] table in the unit's wrangler.toml, and the directory
|
|
5
|
+
# infra/ for kind=infra). The script is named apply-* so that a repository with both roles has no file of the same name from the
|
|
6
|
+
# deploy role (decision monorepo-release-align). Where the deploy role builds a bundle and writes it into the ops declaration, this one applies resources
|
|
7
|
+
# directly (apply-infra.sh), because a bucket is not a bundle that something else puts online (decision infra-apply-on-tag).
|
|
8
|
+
#
|
|
9
|
+
# usage: apply-prerelease.sh <unit tag>
|
|
10
|
+
# env: see apply-infra.sh
|
|
11
|
+
set -euo pipefail
|
|
12
|
+
source "$(dirname "$0")/../lib/common.sh"
|
|
13
|
+
cd "$REPO_ROOT"
|
|
14
|
+
here="$(dirname "$0")"
|
|
15
|
+
tag="${1:?usage: apply-prerelease.sh <unit tag>}"
|
|
16
|
+
route="$(bash "$here/route-tag.sh" "$tag")"
|
|
17
|
+
get() { sed -n "s/^$1=//p" <<<"$route"; }
|
|
18
|
+
if [[ "$(get skip)" == true ]]; then info "⊘ $tag 無需套用:$(get reason)"; exit 0; fi
|
|
19
|
+
# a unit under apps/ or services/ is built by the deploy role (run-prerelease.sh), never applied here
|
|
20
|
+
if [[ "$(get kind)" != infra ]]; then info "⊘ $tag 不是這裡套用的單元(kind $(get kind))"; exit 0; fi
|
|
21
|
+
unit="$(get unit)" env_name="$(get env)"
|
|
22
|
+
bash "$here/apply-infra.sh" "$env_name" "$(unit_path "$unit")"
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# What the infra job of deploy-production.yml does for a product tag vX.Y.Z in a repository with the infra role. Only a product tag applies
|
|
3
|
+
# to production: the stable tag of a single unit never does (git-flow-template release.ts:130-134). Every infra unit of the
|
|
4
|
+
# release (a unit under infra/ whose wrangler.toml has an [env.<the GA environment>] table) is applied. The units under apps/ and
|
|
5
|
+
# services/ are the deploy role's (plan-production.sh), so a unit of kind backend or service is told apart by its directory. Applying is
|
|
6
|
+
# idempotent, so the units that did not change in this release are applied again without effect.
|
|
7
|
+
#
|
|
8
|
+
# usage: apply-production.sh <product tag> the tag must be checked out
|
|
9
|
+
# env: see apply-infra.sh
|
|
10
|
+
set -euo pipefail
|
|
11
|
+
source "$(dirname "$0")/../lib/common.sh"
|
|
12
|
+
cd "$REPO_ROOT"
|
|
13
|
+
here="$(dirname "$0")"
|
|
14
|
+
tag="${1:?usage: apply-production.sh <product tag>}"
|
|
15
|
+
# a private repository on GitHub Free has no deployment branch and tag rules for environments, so the tag is
|
|
16
|
+
# checked here; this is in the workflow file, and whoever can change the file can bypass it
|
|
17
|
+
[[ "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "production 只由產品 tag(vX.Y.Z)套用,收到 $tag"
|
|
18
|
+
applied=0
|
|
19
|
+
for path in $(jq -r 'keys[]' "$MANIFEST"); do
|
|
20
|
+
unit="$(unit_component "$path")"
|
|
21
|
+
[[ "$path" == infra/* ]] || { info "略過不在 infra/ 之下的單元:$path"; continue; }
|
|
22
|
+
case "$(policy_unit_kind "$unit")" in
|
|
23
|
+
service|backend|web) ;;
|
|
24
|
+
*) info "略過非套用單元:$path"; continue ;;
|
|
25
|
+
esac
|
|
26
|
+
env_name="$(stage_target "$unit" ga || true)"
|
|
27
|
+
if [[ -z "$env_name" ]] || ! grep -q "^\[env\.${env_name}\]" "$path/wrangler.toml" 2>/dev/null; then
|
|
28
|
+
info "略過沒有 GA 環境設定的單元:$path"; continue
|
|
29
|
+
fi
|
|
30
|
+
bash "$here/apply-infra.sh" "$env_name" "$path"
|
|
31
|
+
applied=$((applied + 1))
|
|
32
|
+
done
|
|
33
|
+
if (( applied == 0 )); then info "⊘ $tag 沒有要套用的單元,略過"; fi
|
|
@@ -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,88 @@
|
|
|
1
|
+
name: ops-deploy
|
|
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
|
+
# 推上預設分支就觸發這裡。
|
|
8
|
+
# - workflow_dispatch:monorepo 的 deploy workflow 寫完自己的 ops/ 後派送,也用來重跑。
|
|
9
|
+
# /healthz 的網址樣式讀 ops/urls.json(compose 從 ops 單元公布的介面寫出),不是 repo 變數。
|
|
10
|
+
# 只有這個 workflow 讀 Cloudflare 的 token。deploy-prerelease.yml 與 deploy-production.yml 只建置與寫宣告。
|
|
11
|
+
|
|
12
|
+
on:
|
|
13
|
+
push:
|
|
14
|
+
branches: [main]
|
|
15
|
+
paths:
|
|
16
|
+
- "ops/versions.json"
|
|
17
|
+
workflow_dispatch:
|
|
18
|
+
inputs:
|
|
19
|
+
env:
|
|
20
|
+
description: "environment to deploy, e.g. staging"
|
|
21
|
+
required: true
|
|
22
|
+
type: string
|
|
23
|
+
units:
|
|
24
|
+
description: "comma list of units; empty deploys every unit the environment declares"
|
|
25
|
+
required: false
|
|
26
|
+
default: ""
|
|
27
|
+
type: string
|
|
28
|
+
|
|
29
|
+
permissions:
|
|
30
|
+
contents: read
|
|
31
|
+
|
|
32
|
+
defaults:
|
|
33
|
+
run:
|
|
34
|
+
shell: bash
|
|
35
|
+
|
|
36
|
+
concurrency:
|
|
37
|
+
group: ops-deploy
|
|
38
|
+
cancel-in-progress: false
|
|
39
|
+
|
|
40
|
+
env:
|
|
41
|
+
# a loop of a template script asks twice at most and the CI gate asks twice (decision self-hosted-job-no-wait)
|
|
42
|
+
POLL_TRIES: "2"
|
|
43
|
+
CI_GATE_TRIES: "2"
|
|
44
|
+
CI_GATE_QUEUE_TRIES: "0"
|
|
45
|
+
|
|
46
|
+
jobs:
|
|
47
|
+
deploy:
|
|
48
|
+
if: vars.DEPLOY_ENABLED == 'true'
|
|
49
|
+
runs-on: self-hosted-linux
|
|
50
|
+
timeout-minutes: 20
|
|
51
|
+
steps:
|
|
52
|
+
- uses: actions/checkout@v7
|
|
53
|
+
with:
|
|
54
|
+
fetch-depth: 0
|
|
55
|
+
persist-credentials: false
|
|
56
|
+
- uses: ./.github/actions/setup-workspace
|
|
57
|
+
# poly-ops 的 bundle 掛在 app 與 service 倉的 GitHub Release,下載要 GitHub App 的唯讀權杖,
|
|
58
|
+
# 倉名寫在 repository variable BUNDLE_REPOS。monorepo 的 bundle 在本倉,沒設這個變數,用 github.token。
|
|
59
|
+
- id: app-token
|
|
60
|
+
if: vars.BUNDLE_REPOS != ''
|
|
61
|
+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
|
62
|
+
with:
|
|
63
|
+
client-id: ${{ vars.@@clientIdVariable@@ }}
|
|
64
|
+
private-key: ${{ secrets.@@privateKeySecret@@ }}
|
|
65
|
+
owner: ${{ github.repository_owner }}
|
|
66
|
+
repositories: ${{ vars.BUNDLE_REPOS }}
|
|
67
|
+
permission-contents: read
|
|
68
|
+
- name: 上線並核對 /healthz
|
|
69
|
+
run: |
|
|
70
|
+
if [[ "$EVENT" == push ]]; then
|
|
71
|
+
bash scripts/deploy/ops-run.sh --since "$BEFORE"
|
|
72
|
+
else
|
|
73
|
+
bash scripts/deploy/ops-run.sh --env "$ENV_NAME" --units "$UNITS"
|
|
74
|
+
fi
|
|
75
|
+
env:
|
|
76
|
+
EVENT: ${{ github.event_name }}
|
|
77
|
+
BEFORE: ${{ github.event.before }}
|
|
78
|
+
ENV_NAME: ${{ inputs.env }}
|
|
79
|
+
UNITS: ${{ inputs.units }}
|
|
80
|
+
GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }}
|
|
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
|
+
with:
|
|
86
|
+
name: ops-deploy-${{ github.run_id }}
|
|
87
|
+
path: ${{ runner.temp }}/deploy-log
|
|
88
|
+
retention-days: 7
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Put what ops/versions.json declares for an environment online. Per declared unit: download its bundle from the
|
|
3
|
+
# GitHub Release the declaration names, check the bundle hash against the declaration, `wrangler versions upload`,
|
|
4
|
+
# then `wrangler versions deploy <id>@100`. A unit whose /healthz already reports the declared version and
|
|
5
|
+
# bundle hash is skipped, so a rerun changes nothing.
|
|
6
|
+
#
|
|
7
|
+
# The four environments are four separate Workers (<unit>-<env>), and a version id belongs to one Worker, so an
|
|
8
|
+
# environment never reuses the id of another: what carries over is the bundle, and the hash tells it is the same.
|
|
9
|
+
#
|
|
10
|
+
# The order is the one of ops/needs.json, as git-flow-template orders the releases of ops/helmfile.yaml by needs.
|
|
11
|
+
#
|
|
12
|
+
# usage: apply-ops.sh <env> [--only unit1,unit2]
|
|
13
|
+
# env: WRANGLER the command that runs wrangler (default `pnpm exec wrangler`; local mode: the stand-in)
|
|
14
|
+
# DEPLOY_LOG_DIR where <env>.log is appended (default tmp/deploy-log)
|
|
15
|
+
# ops/urls.json the URL pattern of each environment, see healthz.sh
|
|
16
|
+
# GH_TOKEN a token that can read the Releases the declaration names
|
|
17
|
+
# CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID read by wrangler; set only by the workflow step that deploys
|
|
18
|
+
set -euo pipefail
|
|
19
|
+
env_name="${1:?usage: apply-ops.sh <env> [--only u1,u2]}"
|
|
20
|
+
shift
|
|
21
|
+
only=""
|
|
22
|
+
while [[ $# -gt 0 ]]; do
|
|
23
|
+
case "$1" in --only) only="$2"; shift 2 ;; *) printf '%s\n' >&2 "unknown argument: $1"; exit 2 ;; esac
|
|
24
|
+
done
|
|
25
|
+
[[ "$env_name" =~ ^[a-z][a-z0-9-]*$ ]] || { printf '%s\n' >&2 "env 需為小寫英數與連字號(環境名由 release-policy.json 的 targets 決定)"; exit 2; }
|
|
26
|
+
root="$(git rev-parse --show-toplevel)"
|
|
27
|
+
here="$(dirname "$0")"
|
|
28
|
+
source "$here/../lib/bundle.sh"
|
|
29
|
+
decl="$root/ops/versions.json"
|
|
30
|
+
[[ -f "$decl" ]] || { printf '%s\n' >&2 "找不到 $decl"; exit 1; }
|
|
31
|
+
jq -e --arg e "$env_name" 'has($e)' "$decl" >/dev/null || { printf '%s\n' >&2 "$decl 沒有 $env_name 環境"; exit 1; }
|
|
32
|
+
read -r -a wrangler <<<"${WRANGLER:-pnpm exec wrangler}"
|
|
33
|
+
log_dir="${DEPLOY_LOG_DIR:-$root/tmp/deploy-log}"
|
|
34
|
+
mkdir -p "$log_dir"
|
|
35
|
+
work="$(mktemp -d)"; trap 'rm -rf "$work"' EXIT
|
|
36
|
+
|
|
37
|
+
in_only() { [[ -z "$only" ]] && return 0; [[ ",$only," == *",$1,"* ]]; }
|
|
38
|
+
|
|
39
|
+
# 順序宣告在 ops/needs.json:{ "<unit>": ["<unit it needs>", ...] },對應範本 ops/helmfile.yaml 各 release 的 needs。
|
|
40
|
+
# 相依的單元先於自身(後端先於前端),拓撲排序;沒有宣告的單元沒有相依。不在這次要套的單元裡的相依不套,
|
|
41
|
+
# 但仍參與排序與成環檢查。一次 run 只套一個單元(預發布)時順序沒有作用,production 一次套全部入版單元時才生效。
|
|
42
|
+
needs_file="$root/ops/needs.json"
|
|
43
|
+
declare -A needs_of state
|
|
44
|
+
declare -a nodes order
|
|
45
|
+
if [[ -f "$needs_file" ]]; then
|
|
46
|
+
while read -r k d; do needs_of[$k]="${needs_of[$k]:-} $d"; done < <(jq -r 'to_entries[] | .key as $k | .value[] | "\($k) \(.)"' "$needs_file")
|
|
47
|
+
fi
|
|
48
|
+
mapfile -t declared < <(jq -r --arg e "$env_name" '(.[$e] // {}) | keys[]' "$decl")
|
|
49
|
+
mapfile -t nodes < <({ printf '%s\n' "${declared[@]:-}"; [[ -f "$needs_file" ]] && jq -r 'to_entries[] | .key, .value[]' "$needs_file"; } | sed '/^$/d' | LC_ALL=C sort -u)
|
|
50
|
+
declare -a stack=()
|
|
51
|
+
visit() {
|
|
52
|
+
local r="$1" d
|
|
53
|
+
[[ "${state[$r]:-}" == "done" ]] && return 0
|
|
54
|
+
if [[ "${state[$r]:-}" == visiting ]]; then
|
|
55
|
+
local path="" x started=0
|
|
56
|
+
for x in "${stack[@]}" "$r"; do [[ "$x" == "$r" ]] && started=1; (( started )) && path+="${path:+ → }$x"; done
|
|
57
|
+
printf '%s\n' >&2 "✗ 相依成環:$path"; exit 1
|
|
58
|
+
fi
|
|
59
|
+
state[$r]=visiting; stack+=("$r")
|
|
60
|
+
for d in ${needs_of[$r]:-}; do visit "$d"; done # 刻意不加引號:needs 是空白分隔字串
|
|
61
|
+
unset 'stack[${#stack[@]}-1]'
|
|
62
|
+
state[$r]="done"; order+=("$r")
|
|
63
|
+
}
|
|
64
|
+
for r in "${nodes[@]:-}"; do [[ -n "$r" ]] && visit "$r"; done
|
|
65
|
+
units=()
|
|
66
|
+
for r in "${order[@]:-}"; do in_declared=0; for d in "${declared[@]:-}"; do [[ "$d" == "$r" ]] && in_declared=1; done; (( in_declared )) && in_only "$r" && units+=("$r"); done
|
|
67
|
+
printf '%s\n' "[apply-ops] $env_name 套用順序:${units[*]:-(無)}"
|
|
68
|
+
n=0
|
|
69
|
+
for unit in "${units[@]:-}"; do
|
|
70
|
+
[[ -n "$unit" ]] && in_only "$unit" || continue
|
|
71
|
+
entry="$(jq -c --arg e "$env_name" --arg u "$unit" '.[$e][$u]' "$decl")"
|
|
72
|
+
version="$(jq -r .version <<<"$entry")"; sha="$(jq -r .sha256 <<<"$entry")"
|
|
73
|
+
repo="$(jq -r .bundle.repo <<<"$entry")"; tag="$(jq -r .bundle.tag <<<"$entry")"; asset="$(jq -r .bundle.asset <<<"$entry")"
|
|
74
|
+
worker="$unit-$env_name"
|
|
75
|
+
now="$(bash "$here/healthz.sh" "$worker" "$env_name" 2>/dev/null || true)"
|
|
76
|
+
if [[ "$(jq -r .version <<<"${now:-{\}}" 2>/dev/null || true)" == "$version" && "$(jq -r .bundleSha256 <<<"${now:-{\}}" 2>/dev/null || true)" == "$sha" ]]; then
|
|
77
|
+
printf '%s\n' " ⊘ $unit ${version}:$worker 已在線上,略過"; continue
|
|
78
|
+
fi
|
|
79
|
+
dir="$work/$unit"; mkdir -p "$dir/bundle"
|
|
80
|
+
gh release download "$tag" --repo "$repo" --pattern "$asset" --dir "$dir" --clobber
|
|
81
|
+
tar -xzf "$dir/$asset" -C "$dir/bundle"
|
|
82
|
+
got="$(bundle_hash "$dir/bundle")" || { printf '%s\n' >&2 "✗ ${unit}:bundle 沒有 dist/ 或 public/"; exit 1; }
|
|
83
|
+
if [[ "$got" != "$sha" ]]; then
|
|
84
|
+
printf '%s\n' >&2 "✗ ${unit}:下載的 bundle 雜湊 $got 不是宣告的 $sha"; exit 1
|
|
85
|
+
fi
|
|
86
|
+
# every unit ships its built Worker script, and a web unit its static assets too (the layout of scripts/lib/bundle.sh)
|
|
87
|
+
entry_args=()
|
|
88
|
+
if [[ -f "$dir/bundle/dist/index.js" ]]; then entry_args=(dist/index.js --no-bundle); fi
|
|
89
|
+
if [[ -d "$dir/bundle/public" ]]; then entry_args+=(--assets public); fi
|
|
90
|
+
out="$dir/upload.ndjson"
|
|
91
|
+
(cd "$dir/bundle" && WRANGLER_OUTPUT_FILE_PATH="$out" "${wrangler[@]}" versions upload "${entry_args[@]}" --env "$env_name" \
|
|
92
|
+
--message "$unit $version sha256:$sha" --var "BUNDLE_SHA256:$sha")
|
|
93
|
+
id="$(jq -r 'select(.type == "version-upload") | .version_id' "$out" 2>/dev/null | tail -1)"
|
|
94
|
+
[[ -n "$id" ]] || { printf '%s\n' >&2 "✗ ${unit}:wrangler 的輸出檔沒有 version-upload 的 version_id"; exit 1; }
|
|
95
|
+
(cd "$dir/bundle" && "${wrangler[@]}" versions deploy "$id@100" --env "$env_name" --yes --message "$unit $version")
|
|
96
|
+
n=$(( n + 1 ))
|
|
97
|
+
printf '%s\n' "$(date -u '+%Y-%m-%d %H:%M') | $env_name | $unit | $version | apply | $id | sha256:$sha" >> "$log_dir/$env_name.log"
|
|
98
|
+
printf '%s\n' " [$n] $worker ← ${version}(版本 ${id},雜湊 ${sha:0:12}…)"
|
|
99
|
+
done
|
|
100
|
+
printf '%s\n' "[apply-ops] $env_name 完成,上線 $n 個單元"
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Print the /healthz JSON of a deployed Worker: { "version": "...", "bundleSha256": "..." }.
|
|
3
|
+
#
|
|
4
|
+
# usage: healthz.sh <worker> <env> <worker> is <unit>-<env>, the name wrangler.toml gives the environment
|
|
5
|
+
# reads: ops/urls.json { "<env>": "<base URL of a Worker, with {worker} where its name goes>" }
|
|
6
|
+
# compose writes the file from the URL patterns that the ops unit of the repository publishes (blueprint.demos.repos, the
|
|
7
|
+
# interface of the unit), so the patterns are not a repository variable.
|
|
8
|
+
set -euo pipefail
|
|
9
|
+
worker="${1:?usage: healthz.sh <worker> <env>}"
|
|
10
|
+
env_name="${2:?usage: healthz.sh <worker> <env>}"
|
|
11
|
+
root="$(git rev-parse --show-toplevel)"
|
|
12
|
+
file="$root/ops/urls.json"
|
|
13
|
+
[[ -f "$file" ]] || { printf '%s\n' >&2 "找不到 ${file}:各環境的網址樣式由 compose 從 ops 單元公布的介面寫出"; exit 2; }
|
|
14
|
+
pattern="$(jq -r --arg e "$env_name" '.[$e] // empty' "$file")"
|
|
15
|
+
[[ -n "$pattern" ]] || { printf '%s\n' >&2 "$file 沒有 $env_name 的網址樣式"; exit 2; }
|
|
16
|
+
curl -fsS --max-time 15 --retry 3 --retry-delay 2 "${pattern//\{worker\}/$worker}/healthz"
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# What ops-deploy.yml does: put declared bundles online and check each one with /healthz. Either the environment
|
|
3
|
+
# and units are given (a workflow_dispatch, which a monorepo's deploy workflows send after they write ops/), or the
|
|
4
|
+
# declarations that changed between two commits are found (a push to the default branch of poly-ops).
|
|
5
|
+
#
|
|
6
|
+
# usage: ops-run.sh --env <env> [--units u1,u2]
|
|
7
|
+
# ops-run.sh --since <sha> the declarations that differ from <sha> in ops/versions.json
|
|
8
|
+
# env: see apply-ops.sh and smoke.sh. Only apply-ops.sh runs wrangler, so only this workflow needs the Cloudflare credentials.
|
|
9
|
+
set -euo pipefail
|
|
10
|
+
here="$(dirname "$0")"
|
|
11
|
+
root="$(git rev-parse --show-toplevel)"
|
|
12
|
+
env_name="" units="" since=""
|
|
13
|
+
while [[ $# -gt 0 ]]; do
|
|
14
|
+
case "$1" in
|
|
15
|
+
--env) env_name="$2"; shift 2 ;;
|
|
16
|
+
--units) units="$2"; shift 2 ;;
|
|
17
|
+
--since) since="$2"; shift 2 ;;
|
|
18
|
+
*) printf '%s\n' >&2 "unknown argument: $1"; exit 2 ;;
|
|
19
|
+
esac
|
|
20
|
+
done
|
|
21
|
+
declare -a todo=() # "<env> <u1,u2>"
|
|
22
|
+
if [[ -n "$since" ]]; then
|
|
23
|
+
[[ "$since" =~ ^0+$ ]] && since="$(git hash-object -t tree /dev/null)"
|
|
24
|
+
old="$(git show "$since:ops/versions.json" 2>/dev/null || printf '%s\n' '{}')"
|
|
25
|
+
while read -r e us; do [[ -n "$e" ]] && todo+=("$e $us"); done < <(jq -r -n --argjson old "$old" --slurpfile new "$root/ops/versions.json" '
|
|
26
|
+
$new[0] | to_entries[] | .key as $e | (.value // {}) as $now | ($old[$e] // {}) as $was
|
|
27
|
+
| [$now | to_entries[] | select(.value != $was[.key]) | .key] | select(length > 0) | "\($e) \(join(","))"')
|
|
28
|
+
else
|
|
29
|
+
[[ -n "$env_name" ]] || { printf '%s\n' >&2 "--env 或 --since 必填"; exit 2; }
|
|
30
|
+
todo+=("$env_name $units")
|
|
31
|
+
fi
|
|
32
|
+
if (( ${#todo[@]} == 0 )); then printf '%s\n' "ops/versions.json 沒有變更的宣告,不部署"; exit 0; fi
|
|
33
|
+
for line in "${todo[@]}"; do
|
|
34
|
+
e="${line%% *}"; us="${line#* }"; [[ "$us" == "$line" ]] && us=""
|
|
35
|
+
printf '%s\n' "== $e${us:+:$us}"
|
|
36
|
+
bash "$here/apply-ops.sh" "$e" ${us:+--only "$us"}
|
|
37
|
+
specs=()
|
|
38
|
+
while IFS= read -r s; do specs+=("$s"); done < <(jq -r --arg e "$e" --arg us "$us" '
|
|
39
|
+
(.[$e] // {}) | to_entries[] | .key as $k | select($us == "" or ($us | split(",") | index($k))) | "\($k)=\(.value.version)"' "$root/ops/versions.json")
|
|
40
|
+
(( ${#specs[@]} )) && bash "$here/smoke.sh" "$e" "${specs[@]}"
|
|
41
|
+
done
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Post-deploy smoke check: verifies what was DEPLOYED, not what was built.
|
|
3
|
+
# 1. the ops declaration for <env> pins each unit to the expected version
|
|
4
|
+
# 2. the apply log records an apply of that unit in this run, or /healthz already reported the declaration
|
|
5
|
+
# 3. /healthz of the Worker reports that version and the bundle hash of the declaration
|
|
6
|
+
# On failure it prints the rollback command (re-run production with the previous product tag);
|
|
7
|
+
# it never rolls back by itself (usage decision 2026-09-09).
|
|
8
|
+
#
|
|
9
|
+
# usage: smoke.sh <env> <unit>=<version> [<unit>=<version> ...]
|
|
10
|
+
# env: DEPLOY_LOG_DIR (defaults to tmp/deploy-log), ops/urls.json (see healthz.sh)
|
|
11
|
+
set -euo pipefail
|
|
12
|
+
env_name="${1:?usage: smoke.sh <env> <unit>=<version>...}"; shift
|
|
13
|
+
(( $# )) || { printf '%s\n' >&2 "smoke: 至少要一個 <unit>=<version>"; exit 2; }
|
|
14
|
+
[[ "$env_name" =~ ^[a-z][a-z0-9-]*$ ]] || { printf '%s\n' >&2 "env 需為小寫英數與連字號(環境名由 release-policy.json 的 targets 決定)"; exit 2; }
|
|
15
|
+
root="$(git rev-parse --show-toplevel)"
|
|
16
|
+
here="$(dirname "$0")"
|
|
17
|
+
log_dir="${DEPLOY_LOG_DIR:-$root/tmp/deploy-log}"
|
|
18
|
+
mkdir -p "$log_dir"
|
|
19
|
+
decl="$root/ops/versions.json"
|
|
20
|
+
fail=0
|
|
21
|
+
for spec in "$@"; do
|
|
22
|
+
unit="${spec%%=*}"; version="${spec#*=}"
|
|
23
|
+
entry="$(jq -c --arg e "$env_name" --arg u "$unit" '.[$e][$u] // empty' "$decl" 2>/dev/null || true)"
|
|
24
|
+
if [[ -z "$entry" ]]; then printf '%s\n' " ⊘ ${unit}:ops 沒有 $env_name 的宣告(非部署單元),略過"; continue; fi
|
|
25
|
+
pinned="$(jq -r .version <<<"$entry")"; sha="$(jq -r .sha256 <<<"$entry")"
|
|
26
|
+
if [[ "$pinned" != "$version" ]]; then
|
|
27
|
+
printf '%s\n' " ✗ ${unit}:ops 宣告釘的是 ${pinned:-(空)},預期版本 $version"; fail=1; continue
|
|
28
|
+
fi
|
|
29
|
+
probe="$(bash "$here/healthz.sh" "$unit-$env_name" "$env_name" 2>/dev/null || true)"
|
|
30
|
+
got_version="$(jq -r '.version // empty' <<<"${probe:-{\}}" 2>/dev/null || true)"
|
|
31
|
+
got_sha="$(jq -r '.bundleSha256 // empty' <<<"${probe:-{\}}" 2>/dev/null || true)"
|
|
32
|
+
if [[ "$got_version" != "$version" || "$got_sha" != "$sha" ]]; then
|
|
33
|
+
printf '%s\n' " ✗ ${unit}:/healthz 回報版號 ${got_version:-(無)}、雜湊 ${got_sha:-(無)},宣告是 ${version}、$sha"; fail=1; continue
|
|
34
|
+
fi
|
|
35
|
+
printf '%s\n' " ✓ $unit ${version}(sha256:${sha:0:12}…):宣告一致,/healthz 回報相同版號與 bundle 雜湊"
|
|
36
|
+
printf '%s\n' "$(date -u '+%Y-%m-%d %H:%M') | $env_name | $unit | $version | smoke | ok" >> "$log_dir/smoke.log"
|
|
37
|
+
done
|
|
38
|
+
if (( fail )); then
|
|
39
|
+
printf '\n'
|
|
40
|
+
if [[ "$env_name" == production ]]; then
|
|
41
|
+
# the tag being deployed is the newest, so the previous product tag is the second entry
|
|
42
|
+
prev="$(git tag --list 'v[0-9]*' --sort=-v:refname | sed -n '2p')"
|
|
43
|
+
printf '%s\n' "smoke 失敗。回滾:以前一個產品 tag 重跑 production(回滾會把 ops 宣告改寫回舊版本,再重新上線):"
|
|
44
|
+
printf '%s\n' " gh workflow run deploy-production.yml --ref ${prev:-<前一個產品 tag>} -f tag=${prev:-<前一個產品 tag>}"
|
|
45
|
+
else
|
|
46
|
+
printf '%s\n' "smoke 失敗。${env_name} 不需回滾:production 未受影響;修正後以 hotfix 合入發布分支,下一個 prerelease tag 會重新部署並重跑 smoke。"
|
|
47
|
+
fi
|
|
48
|
+
exit 1
|
|
49
|
+
fi
|