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,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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "blueprint-shared-templates",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Templates shared by several products, published on its own",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Carlos Space",
|
|
@@ -14,6 +14,23 @@
|
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
|
-
"LICENSE"
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
"LICENSE",
|
|
18
|
+
"transform.mjs",
|
|
19
|
+
"facts-skeleton.mjs",
|
|
20
|
+
"facts-describes.ts",
|
|
21
|
+
"base",
|
|
22
|
+
"deploy",
|
|
23
|
+
"hub",
|
|
24
|
+
"infra",
|
|
25
|
+
"ops",
|
|
26
|
+
"publish",
|
|
27
|
+
"release",
|
|
28
|
+
"verify"
|
|
29
|
+
],
|
|
30
|
+
"exports": {
|
|
31
|
+
"./transform.mjs": "./transform.mjs",
|
|
32
|
+
"./facts-skeleton.mjs": "./facts-skeleton.mjs",
|
|
33
|
+
"./facts-describes.ts": "./facts-describes.ts",
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
name: publish
|
|
2
|
+
|
|
3
|
+
# 發布入口:把一次發版的套件單元發布到各自的去處,alpha、beta、rc 與 GA 每個階段都發布。
|
|
4
|
+
# npm、PyPI、crates.io Trusted Publishing(OIDC),不讀任何 registry token
|
|
5
|
+
# GitHub Packages 私有套件,用 GITHUB_TOKEN
|
|
6
|
+
# Go、Swift 沒有 registry,mirror job 推到公開鏡像倉並打 tag
|
|
7
|
+
# npm 以階段名作 dist-tag,GA 才是 latest;PyPI 用 PEP 440 版號,crates.io 用 SemVer,兩者的預發布版預設都不會被安裝。
|
|
8
|
+
# 判斷集中在 scripts/release/publish-units.sh 與 scripts/release/mirror-units.sh。版號不屬於派送的階段時,
|
|
9
|
+
# plan job 在任何 registry 被寫入前就停下。
|
|
10
|
+
#
|
|
11
|
+
# 這是閉源倉庫以 Trusted Publishing 發公開套件的結構:build job 跑 self-hosted,打包 npm tgz、PyPI dist 並驗證
|
|
12
|
+
# crate,產物以 retention-days: 1 上傳;publish 是獨立發布 job,跑 ubuntu-latest,只下載產物並上傳,不再建置。這樣
|
|
13
|
+
# 建置不佔用託管 runner 時數,而 Trusted Publishing 只接受託管 runner 簽發的 OIDC token。產物只留 1 天,超過一天就
|
|
14
|
+
# 重新派送本檔。publish-github-packages 發到 GitHub Packages,不用 OIDC,單一 job 跑 self-hosted-linux。
|
|
15
|
+
#
|
|
16
|
+
# 由 release-please.yml 在發版後直接派送(gh workflow run publish.yml),也可以手動派送。不做成被 workflow_call
|
|
17
|
+
# 呼叫的 workflow:npm、PyPI、crates.io 的 Trusted Publisher 登記的都是本檔,被呼叫時 OIDC token 記的是呼叫端的
|
|
18
|
+
# workflow,比對不會通過。登記值是 owner、repo、workflow 檔名 publish.yml,environment 留空,本檔不宣告 environment。
|
|
19
|
+
# 本檔的 Trusted Publishing job 沒有任何 secrets 引用。
|
|
20
|
+
#
|
|
21
|
+
# 第一次發布:crates.io 的 Trusted Publishing 只能登記在已存在的 crate 上,第一版由使用者手動發布,在那之前
|
|
22
|
+
# publish job 略過 crates.io 並在摘要說明。npm 的套件也要先存在才能登記 Trusted Publisher。
|
|
23
|
+
#
|
|
24
|
+
# dry_run:每個發布步驟只印出要跑的指令,不寫任何 registry、也不推鏡像倉,用來演練派送。
|
|
25
|
+
on:
|
|
26
|
+
workflow_dispatch:
|
|
27
|
+
inputs:
|
|
28
|
+
paths:
|
|
29
|
+
description: 'JSON array of released unit paths, e.g. ["packages/product-sdk-js", "facts"]; a path that is not a unit to publish is left out'
|
|
30
|
+
required: true
|
|
31
|
+
type: string
|
|
32
|
+
stage:
|
|
33
|
+
description: "release stage of the train; every unit's version must belong to it"
|
|
34
|
+
required: true
|
|
35
|
+
type: choice
|
|
36
|
+
options: [alpha, beta, rc, ga]
|
|
37
|
+
dry_run:
|
|
38
|
+
description: "rehearse: print the commands each job would run and write nothing"
|
|
39
|
+
required: false
|
|
40
|
+
type: boolean
|
|
41
|
+
default: false
|
|
42
|
+
|
|
43
|
+
permissions:
|
|
44
|
+
contents: read
|
|
45
|
+
|
|
46
|
+
defaults:
|
|
47
|
+
run:
|
|
48
|
+
shell: bash
|
|
49
|
+
|
|
50
|
+
# GitHub keeps one pending run per group and a newer one replaces it. Every stage publishes, so a single group
|
|
51
|
+
# could drop a release queued behind another; the group is per stage, unit set and mode so only a repeat of the same
|
|
52
|
+
# request waits (publish-units.sh skips versions already on the registry).
|
|
53
|
+
concurrency:
|
|
54
|
+
group: publish-${{ inputs.stage }}-${{ inputs.paths }}-${{ inputs.dry_run }}
|
|
55
|
+
cancel-in-progress: false
|
|
56
|
+
|
|
57
|
+
env:
|
|
58
|
+
# a loop of a template script asks twice at most and the CI gate asks twice (decision self-hosted-job-no-wait)
|
|
59
|
+
POLL_TRIES: "2"
|
|
60
|
+
CI_GATE_TRIES: "2"
|
|
61
|
+
CI_GATE_QUEUE_TRIES: "0"
|
|
62
|
+
|
|
63
|
+
jobs:
|
|
64
|
+
# 哪些路徑要發布、各用到哪種去處,以及所有不寫入的檢查:單元檔的版號等於 manifest、版號屬於階段、單元內容等於它的 tag。
|
|
65
|
+
plan:
|
|
66
|
+
runs-on: self-hosted-linux
|
|
67
|
+
timeout-minutes: 10
|
|
68
|
+
outputs:
|
|
69
|
+
paths: ${{ steps.reg.outputs.paths }}
|
|
70
|
+
npm: ${{ steps.reg.outputs.npm }}
|
|
71
|
+
github-packages: ${{ steps.reg.outputs.github-packages }}
|
|
72
|
+
pypi: ${{ steps.reg.outputs.pypi }}
|
|
73
|
+
crates: ${{ steps.reg.outputs.crates }}
|
|
74
|
+
tp: ${{ steps.reg.outputs.tp }}
|
|
75
|
+
mirror: ${{ steps.reg.outputs.mirror }}
|
|
76
|
+
mirrors: ${{ steps.reg.outputs.mirrors }}
|
|
77
|
+
steps:
|
|
78
|
+
- uses: actions/checkout@v7
|
|
79
|
+
with:
|
|
80
|
+
persist-credentials: false
|
|
81
|
+
- id: reg
|
|
82
|
+
env:
|
|
83
|
+
PATHS: ${{ inputs.paths }}
|
|
84
|
+
run: |
|
|
85
|
+
mapfile -t all < <(jq -r '.[]' <<<"$PATHS")
|
|
86
|
+
(( ${#all[@]} )) || { echo "no unit in this release"; exit 0; }
|
|
87
|
+
# the verdict of every dispatched path, one line each: the target it is published to, or not-published
|
|
88
|
+
bash scripts/release/publish-units.sh --report "${all[@]}"
|
|
89
|
+
mapfile -t paths < <(bash scripts/release/publish-units.sh --filter "${all[@]}")
|
|
90
|
+
echo "paths=$(jq -cn '$ARGS.positional' --args ${paths[@]+"${paths[@]}"})" >> "$GITHUB_OUTPUT"
|
|
91
|
+
(( ${#paths[@]} )) || { echo "none of the released paths is a unit to publish"; exit 0; }
|
|
92
|
+
regs="$(bash scripts/release/publish-units.sh --registries "${paths[@]}")"
|
|
93
|
+
echo "$regs" | tee -a "$GITHUB_OUTPUT"
|
|
94
|
+
echo "tp=$(grep -Eq '^(npm|pypi|crates)=true$' <<<"$regs" && echo true || echo false)" >> "$GITHUB_OUTPUT"
|
|
95
|
+
echo "mirror=$(grep -Eq '^(go|swift)=true$' <<<"$regs" && echo true || echo false)" >> "$GITHUB_OUTPUT"
|
|
96
|
+
echo "mirrors=$(bash scripts/release/publish-units.sh --mirrors "${paths[@]}")" >> "$GITHUB_OUTPUT"
|
|
97
|
+
- name: Check every unit
|
|
98
|
+
if: steps.reg.outputs.paths != '' && steps.reg.outputs.paths != '[]'
|
|
99
|
+
env:
|
|
100
|
+
PATHS: ${{ steps.reg.outputs.paths }}
|
|
101
|
+
STAGE: ${{ inputs.stage }}
|
|
102
|
+
PUBLISH_CHECK_ONLY: "1"
|
|
103
|
+
run: |
|
|
104
|
+
mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
|
|
105
|
+
bash scripts/release/publish-units.sh "$STAGE" "${paths[@]}"
|
|
106
|
+
|
|
107
|
+
# 建置 job:打包與驗證都在這裡,沒有 id-token,也不碰任何 registry 的憑證。
|
|
108
|
+
build:
|
|
109
|
+
needs: plan
|
|
110
|
+
if: needs.plan.outputs.tp == 'true' && vars.PUBLISH_ENABLED == 'true'
|
|
111
|
+
runs-on: self-hosted-linux
|
|
112
|
+
timeout-minutes: 30
|
|
113
|
+
permissions:
|
|
114
|
+
contents: read
|
|
115
|
+
steps:
|
|
116
|
+
- uses: actions/checkout@v7
|
|
117
|
+
with:
|
|
118
|
+
persist-credentials: false
|
|
119
|
+
- uses: ./.github/actions/setup-workspace
|
|
120
|
+
if: needs.plan.outputs.npm == 'true'
|
|
121
|
+
- if: needs.plan.outputs.npm == 'true'
|
|
122
|
+
run: pnpm -r build
|
|
123
|
+
- uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # v10.2.0
|
|
124
|
+
if: needs.plan.outputs.pypi == 'true'
|
|
125
|
+
- uses: dtolnay/rust-toolchain@6bed0761d98439e5a578e2877258200ad565ba87 # stable
|
|
126
|
+
if: needs.plan.outputs.crates == 'true'
|
|
127
|
+
with:
|
|
128
|
+
toolchain: stable
|
|
129
|
+
- name: 打包與驗證
|
|
130
|
+
env:
|
|
131
|
+
PATHS: ${{ needs.plan.outputs.paths }}
|
|
132
|
+
STAGE: ${{ inputs.stage }}
|
|
133
|
+
PUBLISH_OUT: ${{ runner.temp }}/publish
|
|
134
|
+
PUBLISH_REGISTRIES: npm,pypi,crates
|
|
135
|
+
run: |
|
|
136
|
+
mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
|
|
137
|
+
bash scripts/release/publish-units.sh pack "$STAGE" "${paths[@]}"
|
|
138
|
+
# 私有倉庫的 artifact 計入組織的 Actions 儲存額度,只留 1 天。發布 job 在同一次 run 內馬上下載;
|
|
139
|
+
# 超過一天就重新派送本檔,不續用舊產物。
|
|
140
|
+
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
|
|
141
|
+
with:
|
|
142
|
+
name: publish-packages
|
|
143
|
+
path: ${{ runner.temp }}/publish
|
|
144
|
+
retention-days: 1
|
|
145
|
+
if-no-files-found: error
|
|
146
|
+
|
|
147
|
+
# 獨立發布 job:只下載 build job 的產物並上傳,不再建置。
|
|
148
|
+
publish:
|
|
149
|
+
needs: [plan, build]
|
|
150
|
+
if: needs.plan.outputs.tp == 'true' && vars.PUBLISH_ENABLED == 'true'
|
|
151
|
+
# 獨立發布 job:讓建置不佔用託管 runner 時數;Trusted Publishing 只接受 GitHub 託管 runner 簽發的 OIDC token
|
|
152
|
+
runs-on: ubuntu-latest
|
|
153
|
+
timeout-minutes: 20
|
|
154
|
+
permissions:
|
|
155
|
+
contents: read
|
|
156
|
+
id-token: write
|
|
157
|
+
steps:
|
|
158
|
+
# crates.io 沒有上傳現成 .crate 的指令,要從源碼 cargo publish,所以檢出源碼
|
|
159
|
+
- uses: actions/checkout@v7
|
|
160
|
+
with:
|
|
161
|
+
persist-credentials: false
|
|
162
|
+
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
|
|
163
|
+
with:
|
|
164
|
+
name: publish-packages
|
|
165
|
+
path: ${{ runner.temp }}/publish
|
|
166
|
+
# crates.io 的 Trusted Publishing 只能登記在已存在的 crate 上。crate 還不存在時第一版由使用者手動發布,
|
|
167
|
+
# 這一輪略過 crates.io,其他去處照常發布。
|
|
168
|
+
- id: crate
|
|
169
|
+
env:
|
|
170
|
+
PATHS: ${{ needs.plan.outputs.paths }}
|
|
171
|
+
NPM: ${{ needs.plan.outputs.npm }}
|
|
172
|
+
PYPI: ${{ needs.plan.outputs.pypi }}
|
|
173
|
+
CRATES: ${{ needs.plan.outputs.crates }}
|
|
174
|
+
run: |
|
|
175
|
+
regs=()
|
|
176
|
+
[[ "$NPM" != true ]] || regs+=(npm)
|
|
177
|
+
[[ "$PYPI" != true ]] || regs+=(pypi)
|
|
178
|
+
exists=false
|
|
179
|
+
if [[ "$CRATES" == true ]]; then
|
|
180
|
+
mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
|
|
181
|
+
if bash scripts/release/publish-units.sh --crate-exists "${paths[@]}"; then
|
|
182
|
+
exists=true; regs+=(crates)
|
|
183
|
+
else
|
|
184
|
+
rc=$?
|
|
185
|
+
[[ $rc == 1 ]] || { echo "::error::crates.io could not be asked whether the crate exists"; exit "$rc"; }
|
|
186
|
+
echo "::notice::the crate is not on crates.io yet, so nothing was published there; its first version is published by hand"
|
|
187
|
+
echo "- crates.io:crate 還不存在,Trusted Publishing 只能登記在已存在的 crate 上,第一版要手動發布,這一輪沒有發布。" >> "$GITHUB_STEP_SUMMARY"
|
|
188
|
+
fi
|
|
189
|
+
fi
|
|
190
|
+
echo "exists=$exists" >> "$GITHUB_OUTPUT"
|
|
191
|
+
echo "registries=$(IFS=,; echo "${regs[*]:-}")" >> "$GITHUB_OUTPUT"
|
|
192
|
+
# 只用 node 與 npm 上傳 tgz,不安裝相依套件。Trusted Publishing 要 npm 11.5.1 以上
|
|
193
|
+
- uses: ./.github/actions/setup-workspace
|
|
194
|
+
if: needs.plan.outputs.npm == 'true'
|
|
195
|
+
with:
|
|
196
|
+
registry-url: https://registry.npmjs.org
|
|
197
|
+
install: "false"
|
|
198
|
+
- if: needs.plan.outputs.npm == 'true'
|
|
199
|
+
run: npm install -g npm@^11.5.1
|
|
200
|
+
- uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # v10.2.0
|
|
201
|
+
if: needs.plan.outputs.pypi == 'true'
|
|
202
|
+
- uses: dtolnay/rust-toolchain@6bed0761d98439e5a578e2877258200ad565ba87 # stable
|
|
203
|
+
if: steps.crate.outputs.exists == 'true'
|
|
204
|
+
with:
|
|
205
|
+
toolchain: stable
|
|
206
|
+
# 短期憑證只在這個 job 內有效:crates.io 用 job 的 OIDC token 交換
|
|
207
|
+
- id: auth
|
|
208
|
+
if: steps.crate.outputs.exists == 'true' && !inputs.dry_run
|
|
209
|
+
uses: rust-lang/crates-io-auth-action@3268eb413133e2960fa4ae0e963ca76380c54cd9 # v1.0.6
|
|
210
|
+
# npm 與 PyPI 的上傳都用 OIDC token,這一步不設任何 npm 或 PyPI 的 token。
|
|
211
|
+
- name: 發布(Trusted Publishing)
|
|
212
|
+
if: steps.crate.outputs.registries != ''
|
|
213
|
+
env:
|
|
214
|
+
PATHS: ${{ needs.plan.outputs.paths }}
|
|
215
|
+
STAGE: ${{ inputs.stage }}
|
|
216
|
+
PUBLISH_OUT: ${{ runner.temp }}/publish
|
|
217
|
+
PUBLISH_REGISTRIES: ${{ steps.crate.outputs.registries }}
|
|
218
|
+
PUBLISH_DRY_RUN: ${{ inputs.dry_run && '1' || '0' }}
|
|
219
|
+
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
|
|
220
|
+
run: |
|
|
221
|
+
mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
|
|
222
|
+
bash scripts/release/publish-units.sh upload "$STAGE" "${paths[@]}"
|
|
223
|
+
|
|
224
|
+
# 私有套件發到 GitHub Packages,用 GITHUB_TOKEN 而不是 OIDC,所以單一 job 跑 self-hosted-linux,建置與上傳都在這裡。
|
|
225
|
+
# Actions 的權限只能設在 job,所以真上傳要的 packages: write 單獨放在這個 job,上面的 job 沒有。token 只在
|
|
226
|
+
# 上傳那一步以 NODE_AUTH_TOKEN 提供,不寫進任何檔案,也不設在 job 層。
|
|
227
|
+
publish-github-packages:
|
|
228
|
+
needs: plan
|
|
229
|
+
if: needs.plan.outputs.github-packages == 'true' && vars.PUBLISH_ENABLED == 'true'
|
|
230
|
+
runs-on: self-hosted-linux
|
|
231
|
+
timeout-minutes: 20
|
|
232
|
+
permissions:
|
|
233
|
+
contents: read
|
|
234
|
+
packages: write
|
|
235
|
+
steps:
|
|
236
|
+
- uses: actions/checkout@v7
|
|
237
|
+
with:
|
|
238
|
+
persist-credentials: false
|
|
239
|
+
- uses: ./.github/actions/setup-workspace
|
|
240
|
+
with:
|
|
241
|
+
registry-url: https://npm.pkg.github.com
|
|
242
|
+
- run: pnpm -r build
|
|
243
|
+
- name: 發布到 GitHub Packages
|
|
244
|
+
env:
|
|
245
|
+
PATHS: ${{ needs.plan.outputs.paths }}
|
|
246
|
+
STAGE: ${{ inputs.stage }}
|
|
247
|
+
PUBLISH_OUT: ${{ runner.temp }}/publish
|
|
248
|
+
PUBLISH_REGISTRIES: github-packages
|
|
249
|
+
PUBLISH_DRY_RUN: ${{ inputs.dry_run && '1' || '0' }}
|
|
250
|
+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
251
|
+
run: |
|
|
252
|
+
mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
|
|
253
|
+
bash scripts/release/publish-units.sh "$STAGE" "${paths[@]}"
|
|
254
|
+
|
|
255
|
+
# 把每個 Go 與 Swift 單元的目錄當成鏡像倉的根目錄推到它的 main,打上沒有路徑前綴的版號 tag
|
|
256
|
+
# (Go 是 v0.3.0-alpha.2,Swift 是 0.3.0-alpha.2)。鏡像倉只有這個 job 會寫,已發布的 tag 內容不同就拒絕。
|
|
257
|
+
# 它跑在本檔而不是 tag 推送時,因為用 GITHUB_TOKEN 建的 tag 不觸發事件。推送用 GitHub App 的權杖,
|
|
258
|
+
# 範圍只有這些鏡像倉的 contents: write。App 的名稱由 compose 依 fact 寫入。dry_run 不需要權杖。
|
|
259
|
+
mirror:
|
|
260
|
+
needs: plan
|
|
261
|
+
if: needs.plan.outputs.mirror == 'true' && vars.PUBLISH_ENABLED == 'true'
|
|
262
|
+
runs-on: self-hosted-linux
|
|
263
|
+
timeout-minutes: 10
|
|
264
|
+
permissions:
|
|
265
|
+
contents: read
|
|
266
|
+
# 一次一個寫入者,兩次發版不會搶同一個 main。
|
|
267
|
+
concurrency:
|
|
268
|
+
group: mirror
|
|
269
|
+
cancel-in-progress: false
|
|
270
|
+
steps:
|
|
271
|
+
# 要 tag:mirror-units.sh 讀每個單元 tag 的樹與 commit。
|
|
272
|
+
- uses: actions/checkout@v7
|
|
273
|
+
with:
|
|
274
|
+
fetch-depth: 0
|
|
275
|
+
fetch-tags: true
|
|
276
|
+
persist-credentials: false
|
|
277
|
+
- id: app-token
|
|
278
|
+
if: ${{ !inputs.dry_run }}
|
|
279
|
+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
|
|
280
|
+
with:
|
|
281
|
+
client-id: ${{ vars.@@clientIdVariable@@ }}
|
|
282
|
+
private-key: ${{ secrets.@@privateKeySecret@@ }}
|
|
283
|
+
owner: ${{ github.repository_owner }}
|
|
284
|
+
repositories: ${{ needs.plan.outputs.mirrors }}
|
|
285
|
+
permission-contents: write
|
|
286
|
+
- name: 推到鏡像倉
|
|
287
|
+
env:
|
|
288
|
+
PATHS: ${{ needs.plan.outputs.paths }}
|
|
289
|
+
MIRROR_PUSH_TOKEN: ${{ steps.app-token.outputs.token }}
|
|
290
|
+
DRY_RUN: ${{ inputs.dry_run && '1' || '0' }}
|
|
291
|
+
MIRROR_OWNER: ${{ github.repository_owner }}
|
|
292
|
+
run: |
|
|
293
|
+
mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
|
|
294
|
+
flags=()
|
|
295
|
+
if [[ "$DRY_RUN" == 1 ]]; then flags=(--dry-run)
|
|
296
|
+
elif [[ -z "$MIRROR_PUSH_TOKEN" ]]; then
|
|
297
|
+
echo "::error::no GitHub App token, so nothing can be pushed to the mirror repositories"; exit 1
|
|
298
|
+
fi
|
|
299
|
+
bash scripts/release/mirror-units.sh ${flags[@]+"${flags[@]}"} "${paths[@]}"
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Deletes the old versions of private packages on GitHub Packages. Each package keeps its latest GA version and every
|
|
2
|
+
// prerelease newer than that GA. Every older GA and every prerelease older than the latest GA is deleted. There is no
|
|
3
|
+
// exception list. The rule is the fact blueprint-shared.templates.private-package-cleanup.
|
|
4
|
+
//
|
|
5
|
+
// a package with no GA version nothing is deleted: there is no latest GA to count from
|
|
6
|
+
// a version that is not X.Y.Z or left alone and listed as skipped, because its order is unknown
|
|
7
|
+
// X.Y.Z-<alpha|beta|rc>.N
|
|
8
|
+
//
|
|
9
|
+
// usage: node scripts/release/cleanup-private-packages.mjs [--yes] [--owner <org>] [--owner-type orgs|users]
|
|
10
|
+
// [--versions-file <json>] <package>...
|
|
11
|
+
// without --yes dry run: prints every keep and delete line and sends nothing (the default)
|
|
12
|
+
// --yes deletes the versions listed as delete through `gh api -X DELETE`
|
|
13
|
+
// --owner the organization or user that owns the packages (default: the owner of GITHUB_REPOSITORY)
|
|
14
|
+
// --versions-file a JSON object { "<package>": [{ "id": 1, "name": "1.2.0" }, ...] } instead of asking GitHub (tests)
|
|
15
|
+
// <package> the package name, with or without its @scope/
|
|
16
|
+
// Output: one line per version, `keep <package> <version> (<reason>)`, `delete <package> <version> (<reason>)` or
|
|
17
|
+
// `skip <package> <version> (<reason>)`. Exit status: 0 done, 1 a deletion failed, 2 the arguments or GitHub are wrong.
|
|
18
|
+
import { execFileSync } from "node:child_process";
|
|
19
|
+
import { readFileSync } from "node:fs";
|
|
20
|
+
|
|
21
|
+
const STAGE_RANK = { alpha: 0, beta: 1, rc: 2 };
|
|
22
|
+
const SHAPE = /^(\d+)\.(\d+)\.(\d+)(?:-(alpha|beta|rc)\.(\d+))?$/;
|
|
23
|
+
|
|
24
|
+
// parse → { core: [x, y, z], stage: "ga" | "alpha" | "beta" | "rc", n } or null
|
|
25
|
+
export function parse(version) {
|
|
26
|
+
const m = SHAPE.exec(version);
|
|
27
|
+
if (!m) return null;
|
|
28
|
+
return { core: [Number(m[1]), Number(m[2]), Number(m[3])], stage: m[4] ?? "ga", n: m[5] === undefined ? 0 : Number(m[5]) };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// compare(a, b) < 0 when a is older: the core first, then a prerelease is older than the release it leads to, then
|
|
32
|
+
// alpha < beta < rc, then the number
|
|
33
|
+
export function compare(a, b) {
|
|
34
|
+
for (let i = 0; i < 3; i++) if (a.core[i] !== b.core[i]) return a.core[i] - b.core[i];
|
|
35
|
+
const rank = (v) => (v.stage === "ga" ? 3 : STAGE_RANK[v.stage]);
|
|
36
|
+
if (rank(a) !== rank(b)) return rank(a) - rank(b);
|
|
37
|
+
return a.n - b.n;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// plan(["1.0.0", ...]) → { keep: [{ version, reason }], delete: [...], skip: [...] }
|
|
41
|
+
export function plan(versions) {
|
|
42
|
+
const known = versions.map((version) => ({ version, v: parse(version) }));
|
|
43
|
+
const skip = known.filter((k) => k.v === null).map((k) => ({ version: k.version, reason: "not X.Y.Z or X.Y.Z-<alpha|beta|rc>.N, order unknown" }));
|
|
44
|
+
const ordered = known.filter((k) => k.v !== null).toSorted((a, b) => compare(a.v, b.v));
|
|
45
|
+
const gas = ordered.filter((k) => k.v.stage === "ga");
|
|
46
|
+
const latest = gas.at(-1);
|
|
47
|
+
const keep = [];
|
|
48
|
+
const del = [];
|
|
49
|
+
for (const k of ordered) {
|
|
50
|
+
if (latest === undefined) keep.push({ version: k.version, reason: "no GA version, nothing to count from" });
|
|
51
|
+
else if (k === latest) keep.push({ version: k.version, reason: "latest GA" });
|
|
52
|
+
else if (compare(k.v, latest.v) > 0) keep.push({ version: k.version, reason: "prerelease newer than the latest GA" });
|
|
53
|
+
else del.push({ version: k.version, reason: k.v.stage === "ga" ? "older GA" : "prerelease older than the latest GA" });
|
|
54
|
+
}
|
|
55
|
+
return { keep, delete: del, skip };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function ghVersions(ownerType, owner, name) {
|
|
59
|
+
const out = execFileSync("gh", ["api", "--paginate", `/${ownerType}/${owner}/packages/npm/${encodeURIComponent(name)}/versions`, "--jq", '.[] | [.id, .name] | @tsv'], { encoding: "utf8" });
|
|
60
|
+
return out.split("\n").filter(Boolean).map((l) => {
|
|
61
|
+
const [id, version] = l.split("\t");
|
|
62
|
+
return { id: Number(id), name: version };
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function main(argv) {
|
|
67
|
+
const opt = (k) => (argv.includes(`--${k}`) ? argv[argv.indexOf(`--${k}`) + 1] : undefined);
|
|
68
|
+
const valued = new Set(["--owner", "--owner-type", "--versions-file"]);
|
|
69
|
+
const packages = argv.filter((a, i) => !a.startsWith("--") && !valued.has(argv[i - 1]));
|
|
70
|
+
if (packages.length === 0) throw new Error("name at least one package");
|
|
71
|
+
const yes = argv.includes("--yes");
|
|
72
|
+
const ownerType = opt("owner-type") ?? "orgs";
|
|
73
|
+
const owner = opt("owner") ?? process.env.GITHUB_REPOSITORY?.split("/")[0];
|
|
74
|
+
const file = opt("versions-file");
|
|
75
|
+
if (file === undefined && !owner) throw new Error("give --owner, or run where GITHUB_REPOSITORY is set");
|
|
76
|
+
const fixture = file === undefined ? null : JSON.parse(readFileSync(file, "utf8"));
|
|
77
|
+
let failed = 0;
|
|
78
|
+
for (const raw of packages) {
|
|
79
|
+
const name = raw.replace(/^@[^/]+\//, "");
|
|
80
|
+
const rows = fixture === null ? ghVersions(ownerType, owner, name) : (fixture[name] ?? fixture[raw] ?? []);
|
|
81
|
+
const p = plan(rows.map((r) => r.name));
|
|
82
|
+
const idOf = new Map(rows.map((r) => [r.name, r.id]));
|
|
83
|
+
for (const k of p.keep) console.log(`keep ${name} ${k.version} (${k.reason})`);
|
|
84
|
+
for (const k of p.skip) console.log(`skip ${name} ${k.version} (${k.reason})`);
|
|
85
|
+
for (const d of p.delete) {
|
|
86
|
+
console.log(`delete ${name} ${d.version} (${d.reason})${yes ? "" : " [dry run]"}`);
|
|
87
|
+
if (!yes) continue;
|
|
88
|
+
try {
|
|
89
|
+
execFileSync("gh", ["api", "-X", "DELETE", `/${ownerType}/${owner}/packages/npm/${encodeURIComponent(name)}/versions/${idOf.get(d.version)}`], { stdio: "pipe" });
|
|
90
|
+
} catch (e) {
|
|
91
|
+
failed++;
|
|
92
|
+
console.error(`could not delete ${name} ${d.version}: ${e.stderr?.toString().trim() ?? e.message}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (!yes) console.log("dry run: nothing deleted, run again with --yes to delete");
|
|
97
|
+
process.exit(failed > 0 ? 1 : 0);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (process.argv[1] && import.meta.url === new URL(`file://${process.argv[1]}`).href) {
|
|
101
|
+
try {
|
|
102
|
+
main(process.argv.slice(2));
|
|
103
|
+
} catch (e) {
|
|
104
|
+
console.error(`cleanup-private-packages: ${e.message}`);
|
|
105
|
+
process.exit(2);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Mirror the Go and Swift units into their own repositories. Neither has a registry: the Go proxy and SwiftPM read
|
|
3
|
+
# the tags of a repository whose root is the package, so each release is pushed from <unit path>/ to the
|
|
4
|
+
# default branch of <MIRROR_OWNER>/<component> and tagged there with the bare version.
|
|
5
|
+
#
|
|
6
|
+
# Go v<version> (0.3.0-alpha.2 -> v0.3.0-alpha.2) the Go proxy needs the "v"
|
|
7
|
+
# Swift <version> (0.3.0-alpha.2 -> 0.3.0-alpha.2) SwiftPM reads SemVer tags without the "v"
|
|
8
|
+
#
|
|
9
|
+
# Each release is one mirror commit: the tree of <unit path> at the unit's release tag (<component>/v<version>, see
|
|
10
|
+
# unit_tag), on top of the mirror's main. Its author, committer and dates are those of the release tag's commit, so
|
|
11
|
+
# the same tag on the same main always gives the same commit. The mirror does not carry the source history: a
|
|
12
|
+
# release line is cut from main, the previous line came back to main as a squash, so the source commits of two lines
|
|
13
|
+
# are not ancestors of each other, while the Go proxy and SwiftPM only read the tags. The script never forces a push.
|
|
14
|
+
#
|
|
15
|
+
# main is always the commit of the highest version tagged in the mirror. A rerun is safe:
|
|
16
|
+
# - the mirror tag exists and its tree is the unit's tree at the release tag: nothing to do
|
|
17
|
+
# - the mirror tag exists with another tree: refuse, because a published version must not move
|
|
18
|
+
# - a higher version is already tagged (an older version dispatched again): only the tag is pushed, main stays
|
|
19
|
+
# - main is not the commit of the highest tag: refuse (someone pushed to the mirror by hand)
|
|
20
|
+
#
|
|
21
|
+
# usage: mirror-units.sh [--dry-run] <path>... units that are not Go or Swift are skipped
|
|
22
|
+
# --dry-run print what would be pushed (commit, tag, file list); nothing is written. Without a release tag
|
|
23
|
+
# the unit's tree is taken from HEAD, so the unit can be rehearsed on a branch.
|
|
24
|
+
# MIRROR_OWNER owner of the mirror repositories (default: the owner of GITHUB_REPOSITORY); the repository is the component name
|
|
25
|
+
# With neither GITHUB_REPOSITORY nor MIRROR_REMOTE set the script stops.
|
|
26
|
+
# MIRROR_REMOTE full remote URL or path, replaces owner/component (tests use a local bare repository)
|
|
27
|
+
# MIRROR_PUSH_TOKEN GitHub App token with contents: write on the mirror repositories; sent as an HTTP header, never in a URL
|
|
28
|
+
set -euo pipefail
|
|
29
|
+
source "$(dirname "$0")/../lib/common.sh"
|
|
30
|
+
cd "$REPO_ROOT"
|
|
31
|
+
|
|
32
|
+
unit_tag() { printf '%s/v%s\n' "$(unit_component "$1")" "$2"; } # the unit tag, <ComponentName>/v<version>
|
|
33
|
+
unit_mirror_kind() { # unit_mirror_kind <path> → go | swift, from the file the unit carries
|
|
34
|
+
if [[ -f "$1/go.mod" ]]; then printf '%s\n' go
|
|
35
|
+
elif [[ -f "$1/Package.swift" ]]; then printf '%s\n' swift
|
|
36
|
+
else return 1; fi
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
dry=0
|
|
40
|
+
if [[ "${1:-}" == --dry-run ]]; then dry=1; shift; fi
|
|
41
|
+
[[ $# -ge 1 ]] || die "usage: mirror-units.sh [--dry-run] <path>..."
|
|
42
|
+
|
|
43
|
+
# mirror_tag <registry> <version> → the tag in the mirror repository
|
|
44
|
+
mirror_tag() {
|
|
45
|
+
case "$1" in
|
|
46
|
+
go) printf 'v%s\n' "$2" ;;
|
|
47
|
+
swift) printf '%s\n' "$2" ;;
|
|
48
|
+
*) return 1 ;;
|
|
49
|
+
esac
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
# version_gt <a> <b> → success when version a is above b (X.Y.Z or X.Y.Z-<alpha|beta|rc>.N)
|
|
53
|
+
version_gt() {
|
|
54
|
+
local a="$1" b="$2" sa="${1%%-*}" sb="${2%%-*}"
|
|
55
|
+
[[ "$a" != "$b" ]] || return 1
|
|
56
|
+
if [[ "$sa" != "$sb" ]]; then
|
|
57
|
+
[[ "$(printf '%s\n%s\n' "$sa" "$sb" | sort -V | tail -1)" == "$sa" ]]
|
|
58
|
+
return
|
|
59
|
+
fi
|
|
60
|
+
[[ "$a" != "$sa" ]] || return 0 # a is the release, b a pre-release of it
|
|
61
|
+
[[ "$b" != "$sb" ]] || return 1
|
|
62
|
+
[[ "$(printf '%s\n%s\n' "${a#*-}" "${b#*-}" | sort -V | tail -1)" == "${a#*-}" ]]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
# git against the mirror, with the token as a header when there is one
|
|
66
|
+
mgit() {
|
|
67
|
+
if [[ -n "${MIRROR_PUSH_TOKEN:-}" ]]; then
|
|
68
|
+
git -c "http.extraheader=Authorization: Basic $(printf 'x-access-token:%s' "$MIRROR_PUSH_TOKEN" | base64 | tr -d '\n')" "$@"
|
|
69
|
+
else
|
|
70
|
+
git "$@"
|
|
71
|
+
fi
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
# mgit push, asking again after a transient error of GitHub (retry.mjs push: a push that forces or deletes is never asked again)
|
|
75
|
+
mgit_push() {
|
|
76
|
+
local -a cmd=(git)
|
|
77
|
+
[[ -z "${MIRROR_PUSH_TOKEN:-}" ]] || cmd+=(-c "http.extraheader=Authorization: Basic $(printf 'x-access-token:%s' "$MIRROR_PUSH_TOKEN" | base64 | tr -d '\n')")
|
|
78
|
+
node "$RETRY_MJS" push -- "${cmd[@]}" push "$@"
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
# the remote of a mirror: MIRROR_REMOTE as it is, else https://github.com/<owner>/<component>.git with the owner from
|
|
82
|
+
# MIRROR_OWNER or from GITHUB_REPOSITORY (owner/repo)
|
|
83
|
+
mirror_remote() { # mirror_remote <component>
|
|
84
|
+
local owner="${MIRROR_OWNER:-${GITHUB_REPOSITORY:-}}"
|
|
85
|
+
owner="${owner%%/*}"
|
|
86
|
+
if [[ -n "${MIRROR_REMOTE:-}" ]]; then printf '%s\n' "$MIRROR_REMOTE"
|
|
87
|
+
elif [[ -n "$owner" ]]; then printf '%s\n' "https://github.com/$owner/$1.git"
|
|
88
|
+
else die "$1: 不知道鏡像倉的 owner,請設 GITHUB_REPOSITORY(owner/repo)或 MIRROR_REMOTE"; fi
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
mirror_unit() { # mirror_unit <path>
|
|
92
|
+
local p="${1%/}" reg ver comp remote mtag ref tree sha tagged main top="" topsha="" s r an ae ad
|
|
93
|
+
reg="$(unit_mirror_kind "$p" 2>/dev/null || true)"
|
|
94
|
+
if [[ "$reg" != go && "$reg" != swift ]]; then info "$p: not a Go or Swift unit, skipped"; return 0; fi
|
|
95
|
+
jq -e --arg p "$p" '.packages | has($p)' "$CONFIG" >/dev/null || die "unexpected package path: $p (not a key of packages in $CONFIG)"
|
|
96
|
+
ver="$(manifest_version "$p")"
|
|
97
|
+
version_stage "$ver" >/dev/null || die "$p: version \"$ver\" is not X.Y.Z or X.Y.Z-<alpha|beta|rc>.N"
|
|
98
|
+
comp="$(unit_component "$p")"
|
|
99
|
+
remote="$(mirror_remote "$comp")"
|
|
100
|
+
mtag="$(mirror_tag "$reg" "$ver")"
|
|
101
|
+
|
|
102
|
+
ref="$(unit_tag "$p" "$ver")"
|
|
103
|
+
if ! git rev-parse -q --verify "refs/tags/$ref^{commit}" >/dev/null 2>&1; then
|
|
104
|
+
(( dry )) || die "release tag $ref does not exist, so $p $ver has not been released"
|
|
105
|
+
info "release tag $ref does not exist yet (dry run): taking the tree of HEAD"
|
|
106
|
+
ref=HEAD
|
|
107
|
+
fi
|
|
108
|
+
tree="$(git rev-parse -q --verify "$ref:$p" 2>/dev/null)" && [[ "$(git cat-file -t "$tree")" == tree ]] || die "$p is not part of $ref"
|
|
109
|
+
case "$reg" in
|
|
110
|
+
go) git cat-file -e "$tree:go.mod" 2>/dev/null || die "$p: the unit has no go.mod at $ref" ;;
|
|
111
|
+
swift) git cat-file -e "$tree:Package.swift" 2>/dev/null || die "$p: the unit has no Package.swift at $ref" ;;
|
|
112
|
+
esac
|
|
113
|
+
|
|
114
|
+
# what the mirror has now (a read; works without a token on a public repository)
|
|
115
|
+
tagged="$(mgit ls-remote --refs "$remote" "refs/tags/$mtag" | cut -f1)"
|
|
116
|
+
main="$(mgit ls-remote "$remote" refs/heads/main | cut -f1)"
|
|
117
|
+
local action=push
|
|
118
|
+
if [[ -n "$tagged" ]]; then
|
|
119
|
+
mgit fetch -q --no-tags "$remote" "refs/tags/$mtag"
|
|
120
|
+
[[ "$(git rev-parse "$tagged^{tree}")" == "$tree" ]] || die "$comp already has tag $mtag at ${tagged:0:12} with other content than $p at $ref; a published version must not move"
|
|
121
|
+
sha="$tagged"
|
|
122
|
+
action=none
|
|
123
|
+
else
|
|
124
|
+
while IFS=$'\t' read -r s r; do
|
|
125
|
+
r="${r#refs/tags/}"
|
|
126
|
+
[[ "$reg" != go ]] || r="${r#v}"
|
|
127
|
+
version_stage "$r" >/dev/null 2>&1 || continue
|
|
128
|
+
if [[ -z "$top" ]] || version_gt "$r" "$top"; then top="$r"; topsha="$s"; fi
|
|
129
|
+
done < <(mgit ls-remote --refs --tags "$remote")
|
|
130
|
+
if [[ -n "$main" ]]; then
|
|
131
|
+
[[ "$main" == "$topsha" ]] || die "$comp main (${main:0:12}) is not the commit of its highest tag${top:+ $(mirror_tag "$reg" "$top") (${topsha:0:12})}; someone pushed to the mirror by hand"
|
|
132
|
+
mgit fetch -q --no-tags "$remote" refs/heads/main
|
|
133
|
+
fi
|
|
134
|
+
IFS=$'\x1f' read -r an ae ad < <(git log -1 --format='%an%x1f%ae%x1f%aI' "$ref")
|
|
135
|
+
sha="$(GIT_AUTHOR_NAME="$an" GIT_AUTHOR_EMAIL="$ae" GIT_AUTHOR_DATE="$ad" \
|
|
136
|
+
GIT_COMMITTER_NAME="$an" GIT_COMMITTER_EMAIL="$ae" GIT_COMMITTER_DATE="$ad" \
|
|
137
|
+
git commit-tree --no-gpg-sign "$tree" ${main:+-p "$main"} -m "$comp $ver" -m "Source: $ref ($(git rev-parse "$ref^{commit}"))")"
|
|
138
|
+
if [[ -n "$top" ]] && version_gt "$top" "$ver"; then action=tag-only; fi
|
|
139
|
+
fi
|
|
140
|
+
|
|
141
|
+
local report
|
|
142
|
+
report="$({
|
|
143
|
+
echo "## $comp ($p $ver)"
|
|
144
|
+
echo "source $ref"
|
|
145
|
+
echo "mirror $remote"
|
|
146
|
+
echo "mirror commit $sha"
|
|
147
|
+
echo "mirror tag $mtag"
|
|
148
|
+
echo "remote main ${main:-(empty)}"
|
|
149
|
+
echo "action $action"
|
|
150
|
+
echo "history (newest first, up to 5):"
|
|
151
|
+
git log --format=' %h %s' -n 5 "$sha"
|
|
152
|
+
echo "files at the mirror root ($(git ls-tree -r --name-only "$sha" | wc -l | tr -d ' ')):"
|
|
153
|
+
git ls-tree -r --name-only "$sha" | sed 's/^/ /'
|
|
154
|
+
[[ "$reg" != go ]] || echo "go.mod: $(git show "$sha:go.mod" | sed -n '/^module /p')"
|
|
155
|
+
})"
|
|
156
|
+
printf '%s\n' "$report"
|
|
157
|
+
[[ -z "${GITHUB_STEP_SUMMARY:-}" ]] || printf '\n```\n%s\n```\n' "$report" >> "$GITHUB_STEP_SUMMARY"
|
|
158
|
+
|
|
159
|
+
if (( dry )); then echo "dry run: nothing pushed"; return 0; fi
|
|
160
|
+
case "$action" in
|
|
161
|
+
none) echo "$comp $mtag is already mirrored, nothing to push" ;;
|
|
162
|
+
tag-only) mgit_push "$remote" "$sha:refs/tags/$mtag" ;;
|
|
163
|
+
push) mgit_push --atomic "$remote" "$sha:refs/heads/main" "$sha:refs/tags/$mtag" ;;
|
|
164
|
+
esac
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
for p in "$@"; do mirror_unit "$p"; done
|