blueprint-shared-templates 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base/.github/ISSUE_TEMPLATE/bug.yml +2 -2
- package/base/.github/ISSUE_TEMPLATE/hotfix.yml +1 -1
- package/base/.github/actions/setup-workspace/action.yml +2 -2
- package/base/.github/workflows/auto-merge.yml +4 -0
- package/base/.github/workflows/ci.yml +17 -1
- package/base/scripts/ci/run-affected.sh +6 -0
- package/base/scripts/lib/common.sh +1 -1
- package/base/scripts/lib/retry.mjs +36 -6
- package/base/scripts/merge/decide.mjs +32 -17
- package/base/scripts/setup/github-repo-settings.sh +1 -1
- package/deploy/.github/workflows/deploy-prerelease.yml +6 -50
- package/deploy/.github/workflows/deploy-prod.yml +103 -0
- package/deploy/scripts/deploy/plan-prod.sh +57 -0
- package/deploy/scripts/deploy/promote.sh +23 -7
- package/deploy/scripts/deploy/publish-bundle.sh +10 -1
- package/deploy/scripts/deploy/route-tag.sh +10 -12
- package/deploy/scripts/deploy/run-prerelease.sh +4 -7
- package/deploy/scripts/deploy/{run-production.sh → run-prod.sh} +15 -17
- package/deploy/scripts/deploy/writeback-ops.sh +71 -40
- package/facts-skeleton.mjs +1 -1
- package/hub/scripts/release/dispatch-release-lines.sh +25 -2
- package/hub/scripts/release/snapshot-members.sh +1 -1
- package/ops/.github/workflows/ops-deploy.yml +92 -11
- package/ops/schemas/targets.schema.json +209 -0
- package/ops/scripts/ci/check-targets.mjs +291 -0
- package/ops/scripts/deploy/apply-iac.sh +89 -0
- package/ops/scripts/deploy/apply-ops.sh +101 -48
- package/ops/scripts/deploy/ops-run.sh +50 -12
- package/ops/scripts/deploy/smoke.sh +35 -16
- package/ops/scripts/lib/ops-steps.sh +75 -0
- package/org/scripts/ci/check-org-values.mjs +91 -0
- package/package.json +2 -2
- package/publish/.github/workflows/publish.yml +12 -3
- package/publish/scripts/release/cleanup-private-packages.mjs +1 -1
- package/publish/scripts/release/mirror-units.sh +45 -5
- package/publish/scripts/release/publish-units.sh +28 -4
- package/release/.github/workflows/advance-release.yml +107 -0
- package/release/.github/workflows/close-release.yml +133 -0
- package/release/.github/workflows/merge-back.yml +82 -0
- package/release/.github/workflows/merge-release-pr.yml +82 -0
- package/release/.github/workflows/open-release-line.yml +14 -5
- package/release/.github/workflows/release-line-finalize.yml +5 -3
- package/release/.github/workflows/release-please.yml +99 -15
- package/release/.github/workflows/sync-release-tools.yml +91 -0
- package/release/release-policy.json +1 -1
- package/release/schemas/release-policy.schema.json +13 -3
- package/release/scripts/ci/branch-exists.sh +27 -0
- package/release/scripts/ci/check-release-policy.sh +13 -2
- package/release/scripts/ci/pin-train.sh +5 -3
- package/release/scripts/ci/release-train.sh +22 -3
- package/release/scripts/lib/release-plan.sh +3 -1
- package/release/scripts/lib/release-pr.sh +275 -11
- package/release/scripts/lib/release-stages.sh +265 -36
- package/release/scripts/release/check-trigger-actor.sh +32 -0
- package/release/scripts/release/close-ready.mjs +72 -0
- package/release/scripts/release/close-ready.sh +70 -0
- package/release/scripts/release/cut-release.sh +35 -4
- package/release/scripts/release/ensure-stage-deploys.sh +3 -3
- package/release/scripts/release/finalize-release.sh +47 -15
- package/release/scripts/release/merge-back-ready.sh +27 -0
- package/release/scripts/release/merge-release-pr.sh +34 -107
- package/release/scripts/release/open-release-line.sh +28 -8
- package/release/scripts/release/pin-release-pr.sh +53 -0
- package/release/scripts/release/product-release-notes.sh +28 -0
- package/release/scripts/release/sync-release-tools.sh +137 -0
- package/release/scripts/release/{verify-production.sh → verify-prod.sh} +11 -10
- package/transform.mjs +3 -4
- package/verify/.github/workflows/{verify-production.yml → verify-prod.yml} +7 -7
- package/verify/scripts/release/verify-ops.sh +4 -4
- package/deploy/.github/workflows/deploy-production.yml +0 -173
- package/deploy/scripts/deploy/plan-production.sh +0 -43
- package/infra/.github/workflows/deploy-prerelease.yml +0 -157
- package/infra/.github/workflows/deploy-production.yml +0 -173
- package/infra/scripts/deploy/apply-infra.sh +0 -46
- package/infra/scripts/deploy/apply-prerelease.sh +0 -22
- package/infra/scripts/deploy/apply-production.sh +0 -33
- package/infra/scripts/deploy/route-tag.sh +0 -65
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
name: Bug(主線)
|
|
2
|
-
description: 尚未發布或
|
|
2
|
+
description: 尚未發布或 stg 發現的 bug(bugfix/<issue-N>-<kebab> → main)
|
|
3
3
|
labels: ["type:bug"]
|
|
4
4
|
body:
|
|
5
5
|
- type: textarea
|
|
@@ -12,4 +12,4 @@ body:
|
|
|
12
12
|
id: env
|
|
13
13
|
attributes:
|
|
14
14
|
label: 發現環境
|
|
15
|
-
options: [local, dev,
|
|
15
|
+
options: [local, dev, stg, prod]
|
|
@@ -46,8 +46,8 @@ runs:
|
|
|
46
46
|
- name: pnpm install
|
|
47
47
|
if: inputs.node == 'true' && inputs.install == 'true'
|
|
48
48
|
shell: bash
|
|
49
|
-
# pnpm 11 and later read only PNPM_CONFIG_STORE_DIR, and
|
|
50
|
-
# in this step, not in the job env, because pnpm self-update reads it too
|
|
49
|
+
# pnpm 11 and later read only PNPM_CONFIG_STORE_DIR, and the self-hosted runner sets only npm_config_store_dir.
|
|
50
|
+
# Export it in this step, not in the job env, because pnpm self-update reads it too.
|
|
51
51
|
# GitHub-hosted has no npm_config_store_dir, so nothing is exported there.
|
|
52
52
|
run: |
|
|
53
53
|
[ -n "${npm_config_store_dir:-}" ] && export PNPM_CONFIG_STORE_DIR="$npm_config_store_dir"
|
|
@@ -10,6 +10,10 @@ name: auto-merge
|
|
|
10
10
|
# A ci that failed still runs the decision, because a job of ci that is not a required check (native) can fail while the
|
|
11
11
|
# required checks pass. pr-message stays a trigger: pr-title is a required check in it, and when it finishes after ci there is no
|
|
12
12
|
# later event to merge on.
|
|
13
|
+
# The merge-back PR of a release line (release/vX.Y--to-main) is not merged here either, with the label or without: decide.mjs skips it.
|
|
14
|
+
# close-release.yml of the release role merges it after the same required checks, and in the same run makes the product tag and deletes
|
|
15
|
+
# the branches. It is a workflow of its own because this file runs on pull_request_target, where the lint rule pr-no-publish keeps
|
|
16
|
+
# finalize-release.sh out.
|
|
13
17
|
#
|
|
14
18
|
# pull_request_target runs this file and the scripts from the base branch, never from the pull request, so the
|
|
15
19
|
# App token is not handed to code of the pull request. A pull request from a fork is never merged.
|
|
@@ -26,8 +26,11 @@ defaults:
|
|
|
26
26
|
run:
|
|
27
27
|
shell: bash
|
|
28
28
|
|
|
29
|
+
# A pull_request run is grouped by its pull request number and a push or workflow_dispatch run by its ref. A pull_request event
|
|
30
|
+
# can arrive after its pull request was merged (release-please updates a merged Release PR); its merge ref is gone then and
|
|
31
|
+
# github.ref is the base branch, so a group keyed by github.ref would put it with the push run of that branch and cancel it.
|
|
29
32
|
concurrency:
|
|
30
|
-
group: ci-${{ github.ref }}
|
|
33
|
+
group: ci-${{ github.event.pull_request.number && format('pr-{0}', github.event.pull_request.number) || github.ref }}
|
|
31
34
|
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
|
32
35
|
|
|
33
36
|
jobs:
|
|
@@ -99,6 +102,13 @@ jobs:
|
|
|
99
102
|
- name: 政策檔符合 schema
|
|
100
103
|
run: bash scripts/ci/check-release-policy-schema.sh
|
|
101
104
|
# @end
|
|
105
|
+
# @if ops
|
|
106
|
+
# targets.json 的 schema 驗不了的三項(ref 的 kind、host 與 cluster 存在、帶環境後綴的上層裡實例環境相同)、叢集與租戶命名、
|
|
107
|
+
# 叢集目錄都宣告過。只有 ops 角色的倉有 ops/targets.json 和這支腳本,其他倉組裝時不放這一步
|
|
108
|
+
# 這個倉自己的 CI 只有本倉的 targets.json,指到別的倉的實例印「未核對」;跨倉的完整核對在放範本的倉的 CI 跑
|
|
109
|
+
- name: targets.json 與叢集命名
|
|
110
|
+
run: node scripts/ci/check-targets.mjs
|
|
111
|
+
# @end
|
|
102
112
|
# bash -n 抓不到:$var 後面直接接中文時,bash 把非 ASCII 位元組讀進變數名,set -u 下執行到才報未綁定。改寫成 ${var}
|
|
103
113
|
- name: Shell 變數名後不接非 ASCII
|
|
104
114
|
run: |
|
|
@@ -220,6 +230,12 @@ jobs:
|
|
|
220
230
|
- name: describes is written with describesOf
|
|
221
231
|
run: bash scripts/ci/check-facts-describes.sh
|
|
222
232
|
# @end
|
|
233
|
+
# @if org
|
|
234
|
+
# The facts were built by the step above. The organization values are checked field by field against the schema of the shared facts, and
|
|
235
|
+
# aggregatePackageName must be the name of the aggregate package. Only the repository that holds the organization values has the script
|
|
236
|
+
- name: organization values against blueprint-shared.org-values
|
|
237
|
+
run: node scripts/ci/check-org-values.mjs
|
|
238
|
+
# @end
|
|
223
239
|
|
|
224
240
|
# Native lane. Deciding whether there is native work must not itself need a macOS runner: only two
|
|
225
241
|
# macOS VMs can exist on the host, they are shared with other repositories, and the GA gate waits on
|
|
@@ -18,6 +18,12 @@ set -euo pipefail
|
|
|
18
18
|
ignore="${CHANGED_IGNORE:-**/*.md}"
|
|
19
19
|
declare -a args=(-r)
|
|
20
20
|
if [[ -z "${AFFECTED_SINCE:-}" ]]; then
|
|
21
|
+
# A workspace whose only package is the root (a repository that holds no pnpm package) has nothing to run;
|
|
22
|
+
# pnpm -r would fail it with ERR_PNPM_RECURSIVE_RUN_NO_SCRIPT. With any other package the full tree stays strict.
|
|
23
|
+
listing="$(pnpm ls -r --depth -1 --json)" || { printf '%s\n' "[affected] 無法列出 workspace 的套件" >&2; exit 1; }
|
|
24
|
+
if ! jq -e --argjson root "$(jq '.name' package.json)" 'any(.[]; .name != $root)' <<<"$listing" >/dev/null; then
|
|
25
|
+
printf '%s\n' "[affected] 全量:workspace 除了根目錄沒有套件,略過 $1"; exit 0
|
|
26
|
+
fi
|
|
21
27
|
printf '%s\n' "[affected] 全量"
|
|
22
28
|
else
|
|
23
29
|
# The workspace root is dropped in both modes: its build and test scripts are `pnpm -r <script>`,
|
|
@@ -96,7 +96,7 @@ external() {
|
|
|
96
96
|
|
|
97
97
|
# Same gate as external(), but asks again after a transient error through retry.mjs (decision transient-error-shared-retry), which
|
|
98
98
|
# decides by the kind. push: HTTP 500, 502, 503, 504 and a connect timeout, never a force or a delete. merge: those, and GitHub's
|
|
99
|
-
# "Base branch was modified", which happens routinely here when a
|
|
99
|
+
# "Base branch was modified", which happens routinely here when a stg deploy writes the ops declaration back to main while a feature
|
|
100
100
|
# PR is in flight. The merge and a plain push are safe to repeat, so retry rather than abort the run. 5 rounds, RETRY_SLEEP seconds apart (5).
|
|
101
101
|
RETRY_MJS="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/retry.mjs"
|
|
102
102
|
external_retry() { # external_retry <push|merge> <target> <cmd>...
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
// The one place that asks again after a transient error of GitHub (decision transient-error-shared-retry).
|
|
2
|
-
// A transient error is HTTP 500, 502, 503 or 504 (gh and git word it differently)
|
|
3
|
-
//
|
|
2
|
+
// A transient error is HTTP 500, 502, 503 or 504 (gh and git word it differently), a connect timeout (Linux, macOS and Go wordings), a
|
|
3
|
+
// connection that could not be made at all (gh's `error connecting to <host>`, git's `Could not resolve host`) and a break of the connection itself:
|
|
4
|
+
// a TLS handshake timeout, a connection reset by the peer, and a Go net/http read that ends in EOF (`Get "<url>": EOF`, `unexpected EOF`).
|
|
5
|
+
// Nothing else counts: another 5xx, a 4xx, a refused write, a dropped credential and a text that only has the letters EOF in it (a
|
|
6
|
+
// heredoc marker, EOFError) do not get better by asking again.
|
|
4
7
|
//
|
|
5
|
-
// transientOf(text) "HTTP 502" | "Connect Timeout" | null
|
|
8
|
+
// transientOf(text) "HTTP 502" | "Connect Timeout" | "Connect Failure" | "TLS Handshake Timeout" | "Connection Reset" | null
|
|
6
9
|
// retryMove(...) the pure rule: after a failure, retry, stop or check first. Every caller decides through it.
|
|
7
10
|
// retryTransient the async loop around retryMove; retryTransientSync is the same loop for a caller that cannot await
|
|
8
11
|
// CLI node retry.mjs <read|push|merge> -- <cmd> [args...]
|
|
9
12
|
//
|
|
10
13
|
// What each kind retries (a failure of any other text stops at once, with the error as it was):
|
|
11
|
-
// read a transient error,
|
|
14
|
+
// read a transient error, what the caller adds through `also` (local.mjs adds the HTTP 404 of a run that was just dispatched), and
|
|
15
|
+
// in the CLI an answer of a command with --json (and no --jq or --template) that exits 0 but is not whole JSON: gh has cut such
|
|
16
|
+
// an answer short now and then, and asking again gets the whole of it
|
|
12
17
|
// push a transient error, and never a force or a delete (--force, -f, --force-with-lease, --force-if-includes, --delete, -d, +refspec,
|
|
13
18
|
// :ref, --mirror, --prune): the retry of a write that rewrites or removes history is a decision, not a reflex
|
|
14
19
|
// merge a transient error and "Base branch was modified" (the base moved between the pull request being opened and merged)
|
|
@@ -30,11 +35,28 @@ export function transientOf(text) {
|
|
|
30
35
|
const codes = [...s.matchAll(/\bHTTP\/?\s*(\d{3})\b/gi), ...s.matchAll(/requested URL returned error:\s*(\d{3})\b/gi)].map((m) => m[1]);
|
|
31
36
|
const code = codes.find((c) => CODES.includes(c));
|
|
32
37
|
if (code) return `HTTP ${code}`;
|
|
33
|
-
|
|
38
|
+
// the connect timeout in its three wordings: Linux `connect: connection timed out`, macOS `connect: operation timed out`, and Go's
|
|
39
|
+
// `dial tcp <addr>: i/o timeout`; only the text after `connect:` or `dial tcp ...:` counts, not any text that says timed out
|
|
40
|
+
if (/Connect Timeout|connect: (?:connection|operation) timed out|Connection timed out|\bdial tcp [^\n]*:\s*i\/o timeout\b/i.test(s)) return "Connect Timeout";
|
|
41
|
+
// a connection that was not made: gh prints `error connecting to api.github.com` for a failed dial or lookup, git and curl print
|
|
42
|
+
// `Could not resolve host: github.com`
|
|
43
|
+
if (/\berror connecting to [\w-]+(?:\.[\w-]+)+|Could not resolve host:/i.test(s)) return "Connect Failure";
|
|
44
|
+
if (/TLS handshake timeout/i.test(s)) return "TLS Handshake Timeout";
|
|
45
|
+
// EOF only in the shape of a dropped connection: after the quoted URL of a Go request, after `read tcp ...:`, or as `unexpected EOF`
|
|
46
|
+
if (/connection reset by peer|"\s*:\s*(?:unexpected\s+)?EOF\b|\b(?:read|write) tcp [^\n]*:\s*EOF\b|\bunexpected EOF\b/i.test(s)) return "Connection Reset";
|
|
34
47
|
return null;
|
|
35
48
|
}
|
|
36
49
|
|
|
37
50
|
export const BASE_MODIFIED = "Base branch was modified";
|
|
51
|
+
export const INCOMPLETE_JSON = "Incomplete JSON";
|
|
52
|
+
|
|
53
|
+
// For a read whose command asks for JSON (--json) and prints it as it is (no --jq, -q, --template, -t): the parse error of what it printed,
|
|
54
|
+
// or null when it is whole JSON or the command does not ask for JSON
|
|
55
|
+
export function jsonProblem(argv = [], stdout = "") {
|
|
56
|
+
if (!argv.some((a) => a === "--json" || a.startsWith("--json="))) return null;
|
|
57
|
+
if (argv.some((a) => /^(?:--jq|-q|--template|-t)(?:=.*)?$/.test(a))) return null;
|
|
58
|
+
try { JSON.parse(stdout); return null; } catch (e) { return e.message; }
|
|
59
|
+
}
|
|
38
60
|
|
|
39
61
|
const LONG_REWRITE = /^--(?:force(?:-with-lease|-if-includes)?|delete|mirror|prune)(?:=.*)?$/;
|
|
40
62
|
const SHORT_REWRITE = /^-[A-Za-z]*[fd][A-Za-z]*$/;
|
|
@@ -61,6 +83,7 @@ export function retryMove({ kind, text, round, rounds = ROUNDS, argv = [], also
|
|
|
61
83
|
if (!KINDS.includes(kind)) throw new Error(`retry kind ${kind}: expected ${KINDS.join(", ")}`);
|
|
62
84
|
let reason = transientOf(text);
|
|
63
85
|
if (reason === null && kind === "merge" && new RegExp(BASE_MODIFIED, "i").test(String(text ?? ""))) reason = BASE_MODIFIED;
|
|
86
|
+
if (reason === null && kind === "read" && String(text ?? "").includes(INCOMPLETE_JSON)) reason = INCOMPLETE_JSON;
|
|
64
87
|
if (reason === null && kind === "read" && also) reason = also(String(text ?? "")) ?? null;
|
|
65
88
|
if (reason === null) return { move: "stop", reason: null, why: "not-transient" };
|
|
66
89
|
if (kind === "push" && rewritesRemote(argv)) return { move: "stop", reason, why: "force" };
|
|
@@ -141,7 +164,8 @@ class CommandFailed extends Error {
|
|
|
141
164
|
}
|
|
142
165
|
|
|
143
166
|
// Runs the command, prints what it printed as it was, and asks again after a failure the kind retries. The exit status is the status
|
|
144
|
-
// of the last run.
|
|
167
|
+
// of the last run. A read whose JSON is not whole is a failure with status 1, and what it printed on stdout is not passed on, so the
|
|
168
|
+
// caller never gets a cut answer in front of the whole one. `sleep` is how the CLI waits (seconds); the default really waits.
|
|
145
169
|
export async function main(args, { env = process.env, out = process.stdout, err = process.stderr, sleep = (s) => new Promise((r) => setTimeout(r, s * 1000)) } = {}) {
|
|
146
170
|
const [kind, sep, ...cmd] = args;
|
|
147
171
|
if (!CLI_KINDS.includes(kind) || sep !== "--" || cmd.length === 0) {
|
|
@@ -152,6 +176,12 @@ export async function main(args, { env = process.env, out = process.stdout, err
|
|
|
152
176
|
const attempt = async () => {
|
|
153
177
|
const r = spawnSync(cmd[0], cmd.slice(1), { encoding: "utf8", stdio: ["inherit", "pipe", "pipe"], maxBuffer: 256 * 1024 * 1024, env });
|
|
154
178
|
if (r.error) throw new CommandFailed({ status: 127, stderr: `${cmd[0]}: ${r.error.message}\n` });
|
|
179
|
+
const cut = kind === "read" && r.status === 0 ? jsonProblem(cmd, r.stdout ?? "") : null;
|
|
180
|
+
if (cut !== null) {
|
|
181
|
+
const stderr = `${r.stderr ?? ""}${INCOMPLETE_JSON}: ${cut}\n`;
|
|
182
|
+
err.write(stderr);
|
|
183
|
+
throw new CommandFailed({ status: 1, stderr });
|
|
184
|
+
}
|
|
155
185
|
out.write(r.stdout ?? "");
|
|
156
186
|
err.write(r.stderr ?? "");
|
|
157
187
|
if (r.status !== 0) throw new CommandFailed(r);
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
// checks from `gh pr checks --json name,bucket`; bucket is pass, fail, pending, skipping or cancel
|
|
11
11
|
// changedFiles the paths the pull request changes (only used for the checks in config.pathFiltered)
|
|
12
12
|
// config JSON { label, requiredChecks, pathFiltered, reviewPaths, syncPr }, written when the repository is composed
|
|
13
|
-
// from blueprint.merge.required-checks. pathFiltered maps a check name to the path globs of the workflow that
|
|
13
|
+
// from blueprint-shared.merge.required-checks. pathFiltered maps a check name to the path globs of the workflow that
|
|
14
14
|
// has a path filter: when none of the changed files matches, that workflow did not run and the check
|
|
15
|
-
// counts as skipped. reviewPaths are the CODEOWNERS style paths (blueprint.git-flow.review-paths) that need
|
|
15
|
+
// counts as skipped. reviewPaths are the CODEOWNERS style paths (blueprint-shared.git-flow.review-paths) that need
|
|
16
16
|
// a review; empty or missing means no pull request needs one here. syncPr is { author, branch }: the
|
|
17
17
|
// pull request that blueprint opens in a demo repository (blueprint.sync.pull-request), which needs no review.
|
|
18
18
|
// pr also needs author ({ login, is_bot }) and latestReviews ([{ state }]) from `gh pr view --json`.
|
|
@@ -25,6 +25,8 @@ import { fileURLToPath } from "node:url";
|
|
|
25
25
|
|
|
26
26
|
const RELEASE_PR_HEAD = "release-please--";
|
|
27
27
|
const RELEASE_LINE = /^release\/v(\d+\.\d+)$/;
|
|
28
|
+
// The merge-back PR of a release line, which the close workflow merges (release/vX.Y--to-main).
|
|
29
|
+
export const MERGE_BACK_HEAD = /^release\/v\d+\.\d+--to-main$/;
|
|
28
30
|
|
|
29
31
|
const skip = (reason, extra = {}) => ({ action: "skip", reason, ...extra });
|
|
30
32
|
|
|
@@ -51,18 +53,10 @@ function stateOf(entries) {
|
|
|
51
53
|
return "pass";
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (pr.isCrossRepository) return skip("the pull request comes from a fork and cannot get the App token", { pr: number });
|
|
59
|
-
const labels = (pr.labels ?? []).map((l) => (typeof l === "string" ? l : l.name));
|
|
60
|
-
if (!labels.includes(label)) return skip(`the pull request has no ${label} label`, { pr: number });
|
|
61
|
-
if (pr.isDraft) return skip("the pull request is a draft", { pr: number });
|
|
62
|
-
if (checkedSha !== pr.headRefOid) {
|
|
63
|
-
return skip(`a commit was pushed after the checks were read (checked ${checkedSha}, head now ${pr.headRefOid})`, { pr: number });
|
|
64
|
-
}
|
|
65
|
-
|
|
56
|
+
// The required checks of the config against the checks of one pull request: which failed, which are still running and which did
|
|
57
|
+
// not run. A check with no entry counts as not run unless its workflow has a path filter that none of the changed files matches.
|
|
58
|
+
// The auto-merge decision and the close workflow (scripts/release/close-ready.mjs) read the checks through this one function.
|
|
59
|
+
export function requiredCheckProblems(checks, changedFiles, { requiredChecks, pathFiltered = {} }) {
|
|
66
60
|
const failed = [];
|
|
67
61
|
const pending = [];
|
|
68
62
|
const missing = [];
|
|
@@ -78,6 +72,25 @@ export function decide({ pr, checkedSha, checks = [], changedFiles = [] }, confi
|
|
|
78
72
|
if (state === "fail") failed.push(name);
|
|
79
73
|
else if (state === "pending") pending.push(name);
|
|
80
74
|
}
|
|
75
|
+
return { failed, pending, missing };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function decide({ pr, checkedSha, checks = [], changedFiles = [] }, config) {
|
|
79
|
+
const { label } = config;
|
|
80
|
+
const number = pr.number;
|
|
81
|
+
if (pr.state !== "OPEN") return skip(`the pull request is ${pr.state}, not OPEN`, { pr: number });
|
|
82
|
+
if (pr.isCrossRepository) return skip("the pull request comes from a fork and cannot get the App token", { pr: number });
|
|
83
|
+
// The merge-back PR is merged by the close workflow, after the same required checks and a head check, together with the tag and the
|
|
84
|
+
// deletion of the branches. This workflow never merges it, a label or not: a merge here would leave the tag and the branches.
|
|
85
|
+
if (MERGE_BACK_HEAD.test(pr.headRefName)) return skip("a merge-back PR is merged by the close workflow (close-release.yml), not by this workflow", { pr: number });
|
|
86
|
+
const labels = (pr.labels ?? []).map((l) => (typeof l === "string" ? l : l.name));
|
|
87
|
+
if (!labels.includes(label)) return skip(`the pull request has no ${label} label`, { pr: number });
|
|
88
|
+
if (pr.isDraft) return skip("the pull request is a draft", { pr: number });
|
|
89
|
+
if (checkedSha !== pr.headRefOid) {
|
|
90
|
+
return skip(`a commit was pushed after the checks were read (checked ${checkedSha}, head now ${pr.headRefOid})`, { pr: number });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const { failed, pending, missing } = requiredCheckProblems(checks, changedFiles, config);
|
|
81
94
|
if (failed.length > 0) return skip(`required check failed: ${failed.join(", ")}`, { pr: number, failed });
|
|
82
95
|
if (pending.length > 0) return skip(`required check still running: ${pending.join(", ")}`, { pr: number });
|
|
83
96
|
if (missing.length > 0) return skip(`required check has not run: ${missing.join(", ")}`, { pr: number });
|
|
@@ -91,9 +104,11 @@ export function decide({ pr, checkedSha, checks = [], changedFiles = [] }, confi
|
|
|
91
104
|
if (!states.includes("APPROVED")) return skip("the pull request changes a review path and has no approving review yet", { pr: number });
|
|
92
105
|
}
|
|
93
106
|
|
|
94
|
-
// A Release PR is not merged by this workflow. It waits for release-please to settle and its
|
|
95
|
-
//
|
|
96
|
-
// in its
|
|
107
|
+
// A Release PR is not merged by this workflow. It waits for release-please to settle, and its versions are checked first, so it is
|
|
108
|
+
// merged with merge-release-pr.sh, by a person on a machine or through the workflow merge-release-pr.yml (git-flow-template
|
|
109
|
+
// docs/versioning.md:28), and the real run of the demos does it in its merge step (decision real-run-local-orchestration). The
|
|
110
|
+
// facts and docs are pinned earlier, by the release-please job once it has written the Release PR (decision
|
|
111
|
+
// release-pin-and-merge-back-by-events); merge-release-pr.sh pins whatever that job left.
|
|
97
112
|
if (release) {
|
|
98
113
|
if (!RELEASE_LINE.test(pr.baseRefName)) return skip(`a Release PR must target release/vX.Y, this one targets ${pr.baseRefName}`, { pr: number });
|
|
99
114
|
return skip("a Release PR is merged with merge-release-pr.sh on a machine, not by this workflow", { pr: number });
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
# description differ.
|
|
21
21
|
#
|
|
22
22
|
# The values of squash, delete-branch, days and the label come from github-repo-settings.json next to this script,
|
|
23
|
-
# a copy of blueprint.merge.method and blueprint.retention.repository-days (tests/repo-settings.test.mjs
|
|
23
|
+
# a copy of blueprint-shared.merge.method and blueprint-shared.retention.repository-days (tests/repo-settings.test.mjs
|
|
24
24
|
# fails when they drift).
|
|
25
25
|
#
|
|
26
26
|
# Reads the current values first and sends only the requests that change something. A failing repository
|
|
@@ -1,26 +1,14 @@
|
|
|
1
1
|
name: deploy-prerelease
|
|
2
2
|
|
|
3
|
-
# 這份檔在 deploy 與 infra 兩個角色的目錄各有一份,內容相同。倉有哪個角色,就留下那個角色的 job(compose 的角色區塊 # @if),
|
|
4
|
-
# 所以只有 deploy 角色的倉沒有 apply job,只有 infra 角色的倉沒有 bundle job,兩個角色都有的倉(monorepo)兩個都有。
|
|
5
3
|
# 預發布階段逐單元處理:每個 prerelease tag({unit}/vX.Y.Z-<階段>.N)依階段處理到對應環境,
|
|
6
4
|
# 階段與去處的對應寫在政策檔 release-policy.json 的 targets 與各單元的 targets
|
|
7
|
-
# (GA 由 deploy-
|
|
8
|
-
# 單元沒有該階段的去處時略過,原因寫進 job summary
|
|
9
|
-
#
|
|
10
|
-
# infra/ 下的單元是 infra,由 apply job 處理。同一個 tag 只會跑其中一個 job。
|
|
11
|
-
# @if deploy
|
|
5
|
+
# (GA 由 deploy-prod.yml 處理,單元的正式 tag 不處理)。
|
|
6
|
+
# 單元沒有該階段的去處時略過,原因寫進 job summary。判斷集中在 scripts/deploy/route-tag.sh。
|
|
7
|
+
# route 依單元所在的目錄決定 kind:apps/、services/ 下的單元是 service,由 bundle job 處理。
|
|
12
8
|
# bundle job 只建置:建置 bundle、算雜湊、掛在這個 tag 的 GitHub Release,再把版號、bundle 位置與雜湊寫進 ops 宣告。
|
|
13
9
|
# monorepo 寫自己的 ops/ 並派送 ops-deploy.yml,poly 寫 poly-ops(repository variable OPS_REPO)並由它上線。
|
|
14
|
-
# 這個 job 不呼叫 wrangler,也不讀 Cloudflare
|
|
15
|
-
#
|
|
16
|
-
# @if infra
|
|
17
|
-
# apply job 不建置 bundle,也不寫 ops 宣告,而是用 wrangler 在環境裡建立單元宣告的 R2 bucket(scripts/deploy/apply-infra.sh)。
|
|
18
|
-
# 只有這個 job 與 deploy-production.yml 的 infra job 讀 Cloudflare 的憑證。
|
|
19
|
-
# 只合併到預設分支不套用,套用跟著單元自己的 tag(decision infra-apply-on-tag)。
|
|
20
|
-
# 同一個倉的 infra 與 api 同時打預發布 tag 時,兩個 run 的順序不保證。ops-deploy 若先於 bucket 建立而失敗,用 workflow_dispatch 重跑
|
|
21
|
-
# (decision monorepo-release-align)。
|
|
22
|
-
# @end
|
|
23
|
-
# 產品層的統一上線閘門在 deploy-production.yml,不在這裡。
|
|
10
|
+
# 這個 job 不呼叫 wrangler,也不讀 Cloudflare 的憑證。雲端資源由 ops 的 iac 步驟建立,不在這裡。
|
|
11
|
+
# 產品層的統一上線閘門在 deploy-prod.yml,不在這裡。
|
|
24
12
|
|
|
25
13
|
on:
|
|
26
14
|
push:
|
|
@@ -28,7 +16,7 @@ on:
|
|
|
28
16
|
workflow_dispatch:
|
|
29
17
|
inputs:
|
|
30
18
|
tag:
|
|
31
|
-
description: "unit tag, e.g.
|
|
19
|
+
description: "unit tag, e.g. monorepo-web/v1.4.0-beta.2"
|
|
32
20
|
required: true
|
|
33
21
|
type: string
|
|
34
22
|
|
|
@@ -70,7 +58,6 @@ jobs:
|
|
|
70
58
|
sed 's/^/- /' route.txt
|
|
71
59
|
} >> "$GITHUB_STEP_SUMMARY"
|
|
72
60
|
|
|
73
|
-
# @if deploy
|
|
74
61
|
bundle:
|
|
75
62
|
needs: route
|
|
76
63
|
if: needs.route.outputs.skip == 'false' && needs.route.outputs.kind == 'service'
|
|
@@ -124,34 +111,3 @@ jobs:
|
|
|
124
111
|
name: deploy-${{ needs.route.outputs.env }}-${{ needs.route.outputs.unit }}-${{ needs.route.outputs.version }}
|
|
125
112
|
path: ${{ runner.temp }}/deploy-log
|
|
126
113
|
retention-days: 7
|
|
127
|
-
# @end
|
|
128
|
-
# @if infra
|
|
129
|
-
apply:
|
|
130
|
-
needs: route
|
|
131
|
-
if: needs.route.outputs.skip == 'false' && needs.route.outputs.kind == 'infra' && vars.DEPLOY_ENABLED == 'true'
|
|
132
|
-
runs-on: self-hosted-linux
|
|
133
|
-
timeout-minutes: 15
|
|
134
|
-
concurrency:
|
|
135
|
-
group: apply-${{ needs.route.outputs.env }}-${{ needs.route.outputs.unit }}
|
|
136
|
-
steps:
|
|
137
|
-
- uses: actions/checkout@v7
|
|
138
|
-
with:
|
|
139
|
-
ref: ${{ inputs.tag || github.ref_name }}
|
|
140
|
-
- uses: ./.github/actions/setup-workspace
|
|
141
|
-
with:
|
|
142
|
-
restore-from-default: "scripts"
|
|
143
|
-
- name: 套用單元宣告的 R2 bucket
|
|
144
|
-
run: bash scripts/deploy/apply-prerelease.sh "$TAG"
|
|
145
|
-
env:
|
|
146
|
-
TAG: ${{ inputs.tag || github.ref_name }}
|
|
147
|
-
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
148
|
-
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
|
|
149
|
-
DEPLOY_LOG_DIR: ${{ runner.temp }}/deploy-log
|
|
150
|
-
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
|
|
151
|
-
if: always()
|
|
152
|
-
with:
|
|
153
|
-
name: apply-${{ needs.route.outputs.env }}-${{ needs.route.outputs.unit }}-${{ needs.route.outputs.version }}
|
|
154
|
-
path: ${{ runner.temp }}/deploy-log
|
|
155
|
-
if-no-files-found: ignore
|
|
156
|
-
retention-days: 7
|
|
157
|
-
# @end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
name: deploy-prod
|
|
2
|
+
|
|
3
|
+
# 產品 tag 是唯一的上線閘門,逐單元的正式 tag 不觸發,因此本版所有單元有單一的上線時刻。
|
|
4
|
+
# release job 一次把本版全部入版單元用 GA 版號重建 bundle,掛在產品 tag 的 GitHub Release,再一起寫進 prod 的 ops 宣告。
|
|
5
|
+
# 入版單元由 manifest 與 ops 的 prod 宣告現況的差異推導,不需另行維護清單。排隊中的 tag 被後來的 tag 取消時,被跳過的 tag 改到的單元,會在下一個 tag 一起上線。
|
|
6
|
+
# 不沿用 rc 的 bundle:版號寫在 bundle 裡,rc 的 bundle 寫 rc 的版號,GA 要重建(decision ga-rebuild-bundle)。
|
|
7
|
+
# 這個 job 不呼叫 wrangler,也不讀 Cloudflare 的憑證。雲端資源由 ops 的 iac 步驟建立,不在這裡。
|
|
8
|
+
|
|
9
|
+
on:
|
|
10
|
+
push:
|
|
11
|
+
tags: ["v[0-9]*"]
|
|
12
|
+
workflow_dispatch:
|
|
13
|
+
inputs:
|
|
14
|
+
tag:
|
|
15
|
+
description: "product tag, e.g. v1.3.0"
|
|
16
|
+
required: true
|
|
17
|
+
type: string
|
|
18
|
+
|
|
19
|
+
permissions:
|
|
20
|
+
contents: read
|
|
21
|
+
|
|
22
|
+
defaults:
|
|
23
|
+
run:
|
|
24
|
+
shell: bash
|
|
25
|
+
|
|
26
|
+
concurrency:
|
|
27
|
+
group: deploy-prod
|
|
28
|
+
cancel-in-progress: false
|
|
29
|
+
|
|
30
|
+
env:
|
|
31
|
+
# a loop of a template script asks twice at most and the CI gate asks twice (decision self-hosted-job-no-wait)
|
|
32
|
+
POLL_TRIES: "2"
|
|
33
|
+
CI_GATE_TRIES: "2"
|
|
34
|
+
CI_GATE_QUEUE_TRIES: "0"
|
|
35
|
+
|
|
36
|
+
jobs:
|
|
37
|
+
release:
|
|
38
|
+
# prod 只能由產品 tag 部署。免費方案的 private repo 用不了 environment 的 deployment branches
|
|
39
|
+
# and tags,所以由這裡核對:觸發的 ref 必須是 tag,且要部署的 tag 是 v<數字>開頭的產品 tag。
|
|
40
|
+
# 這個檢查寫在 workflow 裡,改 workflow 就能繞過;GitHub 強制的白名單需要 public repo 或 Pro/Team。
|
|
41
|
+
# scripts/deploy/run-prod.sh 會再核對一次 tag 的格式。
|
|
42
|
+
if: >-
|
|
43
|
+
startsWith(github.ref, 'refs/tags/v')
|
|
44
|
+
&& (
|
|
45
|
+
startsWith(inputs.tag || github.ref_name, 'v0') ||
|
|
46
|
+
startsWith(inputs.tag || github.ref_name, 'v1') ||
|
|
47
|
+
startsWith(inputs.tag || github.ref_name, 'v2') ||
|
|
48
|
+
startsWith(inputs.tag || github.ref_name, 'v3') ||
|
|
49
|
+
startsWith(inputs.tag || github.ref_name, 'v4') ||
|
|
50
|
+
startsWith(inputs.tag || github.ref_name, 'v5') ||
|
|
51
|
+
startsWith(inputs.tag || github.ref_name, 'v6') ||
|
|
52
|
+
startsWith(inputs.tag || github.ref_name, 'v7') ||
|
|
53
|
+
startsWith(inputs.tag || github.ref_name, 'v8') ||
|
|
54
|
+
startsWith(inputs.tag || github.ref_name, 'v9')
|
|
55
|
+
)
|
|
56
|
+
runs-on: self-hosted-linux
|
|
57
|
+
timeout-minutes: 20
|
|
58
|
+
# 掛 Release 與回寫同倉的 ops 宣告都要寫入權限;read 會讓 push 失敗
|
|
59
|
+
permissions:
|
|
60
|
+
contents: write
|
|
61
|
+
actions: write
|
|
62
|
+
steps:
|
|
63
|
+
- uses: actions/checkout@v7
|
|
64
|
+
with:
|
|
65
|
+
ref: ${{ inputs.tag || github.ref_name }}
|
|
66
|
+
fetch-depth: 0
|
|
67
|
+
- uses: ./.github/actions/setup-workspace
|
|
68
|
+
with:
|
|
69
|
+
restore-from-default: "scripts"
|
|
70
|
+
- id: ops-name
|
|
71
|
+
if: vars.OPS_REPO != ''
|
|
72
|
+
env:
|
|
73
|
+
OPS_REPO: ${{ vars.OPS_REPO }}
|
|
74
|
+
run: echo "name=${OPS_REPO#*/}" >> "$GITHUB_OUTPUT"
|
|
75
|
+
- id: app-token
|
|
76
|
+
if: vars.OPS_REPO != ''
|
|
77
|
+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
|
78
|
+
with:
|
|
79
|
+
client-id: ${{ vars.@@clientIdVariable@@ }}
|
|
80
|
+
private-key: ${{ secrets.@@privateKeySecret@@ }}
|
|
81
|
+
owner: ${{ github.repository_owner }}
|
|
82
|
+
repositories: ${{ steps.ops-name.outputs.name }}
|
|
83
|
+
- name: 用 GA 版號重建入版單元,掛 Release,寫入 prod 宣告
|
|
84
|
+
run: |
|
|
85
|
+
if [[ -n "$OPS_TOKEN" ]]; then
|
|
86
|
+
export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraheader"
|
|
87
|
+
GIT_CONFIG_VALUE_0="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$OPS_TOKEN" | base64 | tr -d '\n')"
|
|
88
|
+
export GIT_CONFIG_VALUE_0
|
|
89
|
+
fi
|
|
90
|
+
bash scripts/deploy/run-prod.sh "$TAG"
|
|
91
|
+
env:
|
|
92
|
+
TAG: ${{ inputs.tag || github.ref_name }}
|
|
93
|
+
GH_TOKEN: ${{ github.token }}
|
|
94
|
+
OPS_TOKEN: ${{ steps.app-token.outputs.token }}
|
|
95
|
+
OPS_REPO: ${{ vars.OPS_REPO }}
|
|
96
|
+
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
|
|
97
|
+
DEPLOY_LOG_DIR: ${{ runner.temp }}/deploy-log
|
|
98
|
+
BUNDLE_DIR: ${{ runner.temp }}/bundles
|
|
99
|
+
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
|
|
100
|
+
with:
|
|
101
|
+
name: deploy-prod-${{ inputs.tag || github.ref_name }}
|
|
102
|
+
path: ${{ runner.temp }}/deploy-log
|
|
103
|
+
retention-days: 7
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Which units a product tag puts online: the units whose version in .release-please-manifest.json differs from what the prod
|
|
3
|
+
# declaration of ops holds now, and that deploy to the GA environment (a web, service or backend unit under apps/ or services/ that the policy file
|
|
4
|
+
# gives a target for the ga stage; a unit under infra/ only publishes an interface and is not built here). The units are derived from the manifest and the declaration, so nobody keeps a list.
|
|
5
|
+
# The tag is compared with the declaration, not with the previous tag, because GitHub keeps one pending run per concurrency group and cancels the
|
|
6
|
+
# older one: the units of a skipped tag are still different from the declaration, so the next tag puts them online. A rerun of an
|
|
7
|
+
# older tag lists every unit that differs from it, so the rollback is complete. A unit the declaration does not hold is listed.
|
|
8
|
+
# Prints key=value lines (usable as $GITHUB_OUTPUT): product (X.Y.Z), previous (the previous product tag or empty, for the log only),
|
|
9
|
+
# services (a JSON array of {unit, version, env}).
|
|
10
|
+
#
|
|
11
|
+
# usage: plan-prod.sh <product tag> [--ops-root <dir>] the tag must be checked out
|
|
12
|
+
# --ops-root the checkout whose ops/versions.json is the declaration (a poly repository passes its clone of poly-ops). Without it ops/versions.json
|
|
13
|
+
# is read from origin/$DEFAULT_BRANCH (default main) after a fetch: the tag checkout holds the ops/ of release time, not the current one
|
|
14
|
+
set -euo pipefail
|
|
15
|
+
source "$(dirname "$0")/../lib/common.sh"
|
|
16
|
+
cd "$REPO_ROOT"
|
|
17
|
+
ref="${1:?usage: plan-prod.sh <product tag> [--ops-root <dir>]}"
|
|
18
|
+
shift
|
|
19
|
+
ops_root=""
|
|
20
|
+
while [[ $# -gt 0 ]]; do
|
|
21
|
+
case "$1" in --ops-root) ops_root="$2"; shift 2 ;; *) die "未知的參數:$1" ;; esac
|
|
22
|
+
done
|
|
23
|
+
[[ "$ref" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "$ref 不是產品 tag(vX.Y.Z)"
|
|
24
|
+
product="${ref#v}"
|
|
25
|
+
# 產品 tag 之間沒有祖先關係(發布分支以 squash 合回),以版本序取前一版,只寫進輸出供紀錄
|
|
26
|
+
prev="$(git tag --list 'v[0-9]*' --sort=-v:refname | awk -v c="$ref" 'f{print;exit} $0==c{f=1}')"
|
|
27
|
+
if [[ -n "$ops_root" ]]; then
|
|
28
|
+
declared="$(cat "$ops_root/ops/versions.json")" || die "找不到 $ops_root/ops/versions.json"
|
|
29
|
+
else
|
|
30
|
+
branch="${DEFAULT_BRANCH:-main}"
|
|
31
|
+
git fetch --quiet --no-tags origin "$branch" || die "無法從 origin 抓取 ${branch},讀不到 prod 的 ops 宣告"
|
|
32
|
+
declared="$(git show "origin/$branch:ops/versions.json")" || die "origin/$branch 沒有 ops/versions.json,讀不到 prod 的 ops 宣告"
|
|
33
|
+
fi
|
|
34
|
+
paths=$(jq -r 'keys[]' "$MANIFEST")
|
|
35
|
+
services="[]"
|
|
36
|
+
for path in $paths; do
|
|
37
|
+
# unit 名稱一律取設定的 component,設定沒有時才退回資料夾名稱
|
|
38
|
+
unit="$(unit_component "$path")"
|
|
39
|
+
ver="$(manifest_version "$path")"
|
|
40
|
+
case "$(policy_unit_kind "$unit")" in
|
|
41
|
+
service|backend|web) ;;
|
|
42
|
+
*) info "略過非部署單元:$path" >&2; continue ;;
|
|
43
|
+
esac
|
|
44
|
+
case "${path%%/*}" in
|
|
45
|
+
apps|services) ;;
|
|
46
|
+
*) info "略過不在 apps/、services/ 之下的單元:$path" >&2; continue ;;
|
|
47
|
+
esac
|
|
48
|
+
env="$(stage_target "$unit" ga || true)"
|
|
49
|
+
if [[ -z "$env" ]]; then
|
|
50
|
+
info "略過沒有 GA 階段去處的單元:$path" >&2; continue
|
|
51
|
+
fi
|
|
52
|
+
# the version the declaration holds for this unit in this environment (<purpose>/<env>, not hosts/...); empty when it holds none
|
|
53
|
+
held="$(jq -r --arg e "$env" --arg u "$unit" '[to_entries[] | select((.key | startswith("hosts/") | not) and (.key | endswith("/" + $e))) | (.value // {})[$u] // empty | if type == "object" then .version else . end] | first // ""' <<<"$declared")"
|
|
54
|
+
if [[ "$held" == "$ver" ]]; then info "略過 ops 已宣告 $ver 的單元:$unit" >&2; continue; fi
|
|
55
|
+
services="$(jq -c --arg u "$unit" --arg v "$ver" --arg e "$env" '. + [{unit: $u, version: $v, env: $e}]' <<<"$services")"
|
|
56
|
+
done
|
|
57
|
+
printf '%s=%s\n' product "$product" previous "$prev" services "$services"
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
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.
|
|
4
|
-
# { "<env>":
|
|
5
|
-
#
|
|
3
|
+
# the declaration, not the Worker. Two files are written, keyed by the instance path <purpose>/<env> (e.g. edge/prod):
|
|
4
|
+
# ops/versions.json { "<purpose>/<env>": { "<unit>": "<version>" } } (blueprint-shared.infra-layout.versions)
|
|
5
|
+
# ops/versions.lock.json { "<purpose>/<env>": { "<unit>": { "sha256": "<bundle hash>",
|
|
6
|
+
# "bundle": { "repo": "owner/name", "tag": "...", "asset": "..." } } } } (blueprint-shared.infra-layout.versions-lock)
|
|
7
|
+
# The instance path is found in the checkout of ops: the one directory workloads/*/<env>/<unit>/ gives the purpose. No such directory,
|
|
8
|
+
# or more than one, stops the script before anything is written, so ops never holds a declaration that nothing can apply.
|
|
9
|
+
# A unit's entries are replaced as a whole, so a rerun leaves the same files.
|
|
6
10
|
#
|
|
7
11
|
# usage: promote.sh <env> <unit> <version> <sha256> --repo <owner/name> --tag <tag> --asset <file> [--ops-root <dir>]
|
|
8
12
|
# --ops-root the checkout that holds ops/ (default the repository root; a poly repository passes its clone of poly-ops)
|
|
@@ -28,8 +32,20 @@ done
|
|
|
28
32
|
root="${ops_root:-$(git rev-parse --show-toplevel)}"
|
|
29
33
|
f="$root/ops/versions.json"
|
|
30
34
|
[[ -f "$f" ]] || { printf '%s\n' >&2 "找不到 ${f}"; exit 1; }
|
|
31
|
-
|
|
35
|
+
shopt -s nullglob
|
|
36
|
+
matches=("$root"/ops/workloads/*/"$env_name"/"$unit"/)
|
|
37
|
+
if (( ${#matches[@]} == 0 )); then
|
|
38
|
+
printf '%s\n' >&2 "✗ $unit 在 ops 沒有 $env_name 環境的目錄(找不到 ops/workloads/*/$env_name/$unit/),不寫宣告"; exit 1
|
|
39
|
+
fi
|
|
40
|
+
if (( ${#matches[@]} > 1 )); then
|
|
41
|
+
printf '%s\n' >&2 "✗ $unit 在 ops 的 $env_name 環境有多個目錄,分不出實例路徑:"; printf ' %s\n' >&2 "${matches[@]#"$root"/}"; exit 1
|
|
42
|
+
fi
|
|
43
|
+
purpose="$(basename "$(dirname "$(dirname "${matches[0]%/}")")")"
|
|
44
|
+
instance="$purpose/$env_name"
|
|
45
|
+
lock="$root/ops/versions.lock.json"
|
|
46
|
+
[[ -f "$lock" ]] || printf '%s\n' '{}' > "$lock"
|
|
32
47
|
tmp="$(mktemp)"
|
|
33
|
-
jq --arg
|
|
34
|
-
|
|
35
|
-
|
|
48
|
+
jq --arg i "$instance" --arg u "$unit" --arg v "$version" '.[$i] = ((.[$i] // {}) + {($u): $v})' "$f" > "$tmp" && mv "$tmp" "$f"
|
|
49
|
+
jq --arg i "$instance" --arg u "$unit" --arg s "$sha" --arg r "$repo" --arg t "$tag" --arg a "$asset" \
|
|
50
|
+
'.[$i] = ((.[$i] // {}) + {($u): {sha256: $s, bundle: {repo: $r, tag: $t, asset: $a}}})' "$lock" > "$tmp" && mv "$tmp" "$lock"
|
|
51
|
+
printf '%s\n' "[promote] $instance/$unit → $version sha256:${sha:0:12}…"
|
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
# (a product tag has none until now; release-please creates the Release of a unit tag). The Release is where
|
|
4
4
|
# the ops declaration says to fetch the bundle from.
|
|
5
5
|
#
|
|
6
|
+
# release-line-finalize creates the Release of the same product tag at the same time (it sets the notes to the
|
|
7
|
+
# Snapshot). When this script's create loses that race, the Release exists and the upload goes on; a create that
|
|
8
|
+
# fails and leaves no Release (a missing permission, a tag that is not on the remote) fails the script.
|
|
9
|
+
#
|
|
6
10
|
# usage: publish-bundle.sh <tag> <bundle.tgz> (reads the hash from the .sha256 file next to the bundle)
|
|
7
11
|
# env: GH_TOKEN a token that can write Releases of this repository
|
|
8
12
|
# Prints the repository (owner/name) on stdout as repo=<owner/name>.
|
|
@@ -14,7 +18,12 @@ sum="${bundle%.tgz}.sha256"
|
|
|
14
18
|
[[ -f "$sum" ]] || { printf '%s\n' >&2 "找不到雜湊檔:$sum"; exit 1; }
|
|
15
19
|
repo="${GITHUB_REPOSITORY:-$(gh repo view --json nameWithOwner --jq .nameWithOwner)}"
|
|
16
20
|
if ! gh release view "$tag" --repo "$repo" >/dev/null 2>&1; then
|
|
17
|
-
|
|
21
|
+
rc=0
|
|
22
|
+
gh release create "$tag" --repo "$repo" --verify-tag --title "$tag" --notes "bundles of $tag" || rc=$?
|
|
23
|
+
if (( rc != 0 )); then
|
|
24
|
+
gh release view "$tag" --repo "$repo" >/dev/null 2>&1 || exit "$rc"
|
|
25
|
+
printf '%s\n' >&2 "[publish-bundle] $tag 的 Release 已由另一個 workflow 建立,繼續上傳"
|
|
26
|
+
fi
|
|
18
27
|
fi
|
|
19
28
|
gh release upload "$tag" "$bundle" "$sum" --repo "$repo" --clobber
|
|
20
29
|
printf '%s\n' >&2 "[publish-bundle] $tag ← $(basename "$bundle")"
|