blueprint-shared-templates 0.7.0 → 0.8.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.
@@ -10,6 +10,7 @@
10
10
  # wrangler.toml holds no environment (the settings of each environment live in ops)
11
11
  # - anything else (package, sdk, mobile, desktop): nothing to deploy here. Packages are published by
12
12
  # publish.yml, and native apps are not part of the first version (decision native-apps-not-in-first-version)
13
+ # A unit whose publish.to in the policy file is image publishes a container image and is skipped here, whatever its kind.
13
14
  # The directory of the unit tells whether it is built here, because the policy kind alone does not: an InfraName unit is kind
14
15
  # backend too. A unit under apps/ or services/ is kind=service (the deploy role builds it). A unit under infra/ only publishes an
15
16
  # interface, and the cloud resources are created by the iac steps of ops (decision ops-declares-cloud-resources), so it is skipped.
@@ -42,6 +43,9 @@ if ! is_stage "$stage" || [[ "$stage" == ga ]]; then
42
43
  skip_with "版號 $version 的預發布階段「${stage}」不是 ${RELEASE_STAGES[*]:0:3} 之一"
43
44
  fi
44
45
  policy_unit_exists "$unit" || skip_with "$unit 不在政策檔($(basename "$(policy_file)"))的 units 裡,不是發布單元"
46
+ # a unit that publishes a container image (publish.to image) is not deployed by its tag: publish.yml pushes the image, and
47
+ # the deployment mounts the configuration and pulls it
48
+ [[ "$(policy_unit_publish_to "$unit")" != image ]] || skip_with "$unit 發布容器映像(政策檔的 publish.to 是 image),不經 tag 部署,映像由 publish.yml 推到 ghcr"
45
49
 
46
50
  case "$(policy_unit_kind "$unit")" in
47
51
  service|backend|web)
@@ -2,8 +2,9 @@
2
2
  # Put what ops/versions.json declares for an environment online. Per declared unit: download its bundle from the
3
3
  # GitHub Release that ops/versions.lock.json names, check the bundle hash against the lock, join the settings of the
4
4
  # environment (ops/workloads/<purpose>/<env>/<unit>/wrangler.toml) to the wrangler.toml of the bundle, `wrangler versions upload`,
5
- # then `wrangler versions deploy <id>@100`. A unit whose /healthz already reports the declared version, the bundle hash and the
6
- # hash of the joined settings is skipped, so a rerun changes nothing.
5
+ # then `wrangler versions deploy <id>@100` and `wrangler triggers deploy` (the routes, custom domains and workers_dev of the settings).
6
+ # A unit whose /healthz already reports the declared version, the bundle hash and the hash of the joined settings is skipped, so a rerun
7
+ # changes nothing.
7
8
  #
8
9
  # Each environment is a separate Worker (<unit>-<env>), and a version id belongs to one Worker, so an
9
10
  # environment never reuses the id of another: what carries over is the bundle, and the hash tells it is the same.
@@ -146,6 +147,9 @@ for step in "${steps[@]:-}"; do
146
147
  id="$(jq -r 'select(.type == "version-upload") | .version_id' "$out" 2>/dev/null | tail -1)"
147
148
  [[ -n "$id" ]] || { printf '%s\n' >&2 "✗ ${unit}:wrangler 的輸出檔沒有 version-upload 的 version_id"; exit 1; }
148
149
  (cd "$dir/bundle" && "${wrangler[@]}" versions deploy "$id@100" --env "$env_name" --yes --message "$unit $version")
150
+ # `wrangler versions deploy` moves traffic to the version only. It does not apply routes, custom domains or workers_dev of the
151
+ # settings, which `wrangler deploy` would; `wrangler triggers deploy` does, and it changes nothing when they are in place.
152
+ (cd "$dir/bundle" && "${wrangler[@]}" triggers deploy --env "$env_name")
149
153
  n=$(( n + 1 ))
150
154
  printf '%s\n' "$(date -u '+%Y-%m-%d %H:%M') | $env_name | $unit | $version | apply | $id | sha256:$sha | config:${cfg:0:12}" >> "$log_dir/$env_name.log"
151
155
  printf '%s\n' " [$n] $worker ← ${version}(版本 ${id},雜湊 ${sha:0:12}…,設定 ${cfg:0:12}…)"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blueprint-shared-templates",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "A public reference implementation and specification of the shared templates, pinnable and readable, not a source to fetch files from",
5
5
  "license": "MIT",
6
6
  "author": "Carlos Space",
@@ -4,8 +4,10 @@ name: publish
4
4
  # npm、PyPI、crates.io Trusted Publishing(OIDC),不讀任何 registry token
5
5
  # GitHub Packages 私有套件,用 GITHUB_TOKEN
6
6
  # Go、Swift 沒有 registry,mirror job 推到公開鏡像倉並打 tag
7
+ # 執行檔 政策檔 publish.to 是 binary 的單元,binary job 交叉編譯後發到公開發布倉的 GitHub release,GA 才更新 Homebrew formula
8
+ # 容器映像 政策檔 publish.to 是 image 的單元,image job 推到 ghcr,digest 附在來源倉單元 tag 的 Release
7
9
  # npm 以階段名作 dist-tag,GA 才是 latest;PyPI 用 PEP 440 版號,crates.io 用 SemVer,兩者的預發布版預設都不會被安裝。
8
- # 判斷集中在 scripts/release/publish-units.sh 與 scripts/release/mirror-units.sh。版號不屬於派送的階段時,
10
+ # 判斷集中在 scripts/release/publish-units.sh、mirror-units.sh、binary-units.sh 與 image-units.sh。版號不屬於派送的階段時,
9
11
  # plan job 在任何 registry 被寫入前就停下。
10
12
  #
11
13
  # 這是閉源倉庫以 Trusted Publishing 發公開套件的結構:build job 跑 self-hosted,打包 npm tgz、PyPI dist 並驗證
@@ -74,6 +76,8 @@ jobs:
74
76
  tp: ${{ steps.reg.outputs.tp }}
75
77
  mirror: ${{ steps.reg.outputs.mirror }}
76
78
  mirrors: ${{ steps.reg.outputs.mirrors }}
79
+ binary: ${{ steps.reg.outputs.binary }}
80
+ image: ${{ steps.reg.outputs.image }}
77
81
  steps:
78
82
  - uses: actions/checkout@v7
79
83
  with:
@@ -306,3 +310,108 @@ jobs:
306
310
  echo "::error::no GitHub App token, so nothing can be pushed to the mirror repositories"; exit 1
307
311
  fi
308
312
  bash scripts/release/mirror-units.sh ${flags[@]+"${flags[@]}"} "${paths[@]}"
313
+
314
+ # 政策檔 publish.to 是 binary 的單元:依政策檔的平台交叉編譯,連同 checksum 發到公開發布倉的 GitHub release,GA 才更新
315
+ # Homebrew formula(binary-units.sh)。跑預設 label 的 self-hosted runner,用 setup-go 裝 go.mod 指定的版本。
316
+ # 寫發布倉用 GitHub App 的權杖,範圍只有發布倉的 contents: write。發布倉名稱是組織值 binaryReleaseRepository,只寫倉庫名,
317
+ # owner 和這個倉相同;組織沒填這個值時,第一步就停下並寫原因,不讓空的 repositories 把權杖開到整個組織。
318
+ # 一次一個寫入者,兩次發版不會搶同一個 formula。dry_run 不需要權杖。
319
+ binary:
320
+ needs: plan
321
+ if: needs.plan.outputs.binary == 'true'
322
+ runs-on: self-hosted-linux
323
+ timeout-minutes: 30
324
+ permissions:
325
+ contents: read
326
+ concurrency:
327
+ group: binary
328
+ cancel-in-progress: false
329
+ steps:
330
+ # 要 tag:binary-units.sh 讀單元 tag 的 commit。
331
+ - uses: actions/checkout@v7
332
+ with:
333
+ fetch-depth: 0
334
+ fetch-tags: true
335
+ persist-credentials: false
336
+ - id: release-repo
337
+ env:
338
+ BINARY_RELEASE_REPOSITORY: "@@binaryReleaseRepository@@"
339
+ PATHS: ${{ needs.plan.outputs.paths }}
340
+ run: |
341
+ if [[ -z "$BINARY_RELEASE_REPOSITORY" ]]; then
342
+ echo "::error::the organization gives no binaryReleaseRepository, so there is no repository to release the executables to"
343
+ exit 1
344
+ fi
345
+ mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
346
+ file=""
347
+ for p in "${paths[@]}"; do
348
+ if [[ "$(bash scripts/release/publish-units.sh --report "$p")" == "unit $p -> binary" ]]; then file="$p/go.mod"; break; fi
349
+ done
350
+ echo "go-mod=$file" >> "$GITHUB_OUTPUT"
351
+ - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
352
+ with:
353
+ go-version-file: ${{ steps.release-repo.outputs.go-mod }}
354
+ cache: false
355
+ - id: app-token
356
+ if: ${{ !inputs.dry_run }}
357
+ uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
358
+ with:
359
+ client-id: ${{ vars.@@clientIdVariable@@ }}
360
+ private-key: ${{ secrets.@@privateKeySecret@@ }}
361
+ owner: ${{ github.repository_owner }}
362
+ repositories: "@@binaryReleaseRepository@@"
363
+ permission-contents: write
364
+ - name: 發布執行檔
365
+ env:
366
+ PATHS: ${{ needs.plan.outputs.paths }}
367
+ BINARY_RELEASE_TOKEN: ${{ steps.app-token.outputs.token }}
368
+ BINARY_RELEASE_REPOSITORY: "@@binaryReleaseRepository@@"
369
+ BINARY_RELEASE_OWNER: ${{ github.repository_owner }}
370
+ BINARY_OUT: ${{ runner.temp }}/binary
371
+ DRY_RUN: ${{ inputs.dry_run && '1' || '0' }}
372
+ run: |
373
+ mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
374
+ flags=()
375
+ if [[ "$DRY_RUN" == 1 ]]; then flags=(--dry-run)
376
+ elif [[ -z "$BINARY_RELEASE_TOKEN" ]]; then
377
+ echo "::error::no GitHub App token, so nothing can be released to the release repository"; exit 1
378
+ fi
379
+ bash scripts/release/binary-units.sh ${flags[@]+"${flags[@]}"} "${paths[@]}"
380
+
381
+ # 政策檔 publish.to 是 image 的單元:用單元目錄的 Dockerfile 依政策檔平台建置,推到 ghcr.io/<owner>/<單元名>:<版號>,
382
+ # 把 digest 附在來源倉單元 tag 的 Release 並印在摘要(image-units.sh)。要 docker,所以跑組織值 selfHostedRunnerLabels.docker
383
+ # 的 runner;組織沒填這個值時,image-units.sh 在沒有 docker 的 runner 上停下並寫原因。
384
+ # 推映像和附 digest 都用這個 job 的 GITHUB_TOKEN:GitHub App 沒有 packages 權限,Release 在這個倉,contents: write 就夠。
385
+ # DOCKER_CONFIG 放在 job 的暫存目錄,image-units.sh 結束時刪掉。套件第一次推送是私有,要公開時由人改一次,改了收不回。
386
+ image:
387
+ needs: plan
388
+ if: needs.plan.outputs.image == 'true'
389
+ runs-on: "@@dockerRunnerLabel@@"
390
+ timeout-minutes: 60
391
+ permissions:
392
+ contents: write
393
+ packages: write
394
+ concurrency:
395
+ group: image
396
+ cancel-in-progress: false
397
+ steps:
398
+ # 要 tag:image-units.sh 讀單元 tag 的 commit。
399
+ - uses: actions/checkout@v7
400
+ with:
401
+ fetch-depth: 0
402
+ fetch-tags: true
403
+ persist-credentials: false
404
+ - name: 推映像
405
+ env:
406
+ PATHS: ${{ needs.plan.outputs.paths }}
407
+ GH_TOKEN: ${{ github.token }}
408
+ IMAGE_TOKEN: ${{ github.token }}
409
+ IMAGE_ACTOR: ${{ github.actor }}
410
+ IMAGE_OWNER: ${{ github.repository_owner }}
411
+ IMAGE_OUT: ${{ runner.temp }}/image
412
+ DRY_RUN: ${{ inputs.dry_run && '1' || '0' }}
413
+ run: |
414
+ mapfile -t paths < <(jq -r '.[]' <<<"$PATHS")
415
+ flags=()
416
+ if [[ "$DRY_RUN" == 1 ]]; then flags=(--dry-run); fi
417
+ bash scripts/release/image-units.sh ${flags[@]+"${flags[@]}"} "${paths[@]}"
@@ -0,0 +1,272 @@
1
+ #!/usr/bin/env bash
2
+ # Release the executables of the units whose publish.to in release-policy.json is binary (blueprint-shared.publishing.binary-release).
3
+ # Each unit is a Go program: it is cross-compiled for every platform the policy lists, packed, and released with its checksum file
4
+ # to the public release repository, and at a GA version the Homebrew formula in that repository is updated.
5
+ #
6
+ # build CGO_ENABLED=0 go build -trimpath -buildvcs=false for each <os>/<arch> of publish.platforms, in the unit directory.
7
+ # -buildvcs=false keeps the commit and its time out of the executable, so one source gives the same bytes at any commit
8
+ # archive <command>-<version>-<os>-<arch>.tar.gz, one file in it: the executable <command>. The archive is the same bytes for the
9
+ # same executable (fixed mtime, owner and gzip header), so a rerun can tell a version that changed from one that did not
10
+ # checksum <command>-<version>.sha256, sha256sum format: "<sha256> <archive name>" for every archive
11
+ # version the unit has a version.txt and it must equal the version of .release-please-manifest.json. After the build the
12
+ # executable of the runner platform runs with --version and must print that version
13
+ # release tag <ComponentName>/v<version> in the release repository (the same string as the unit tag of the source
14
+ # repository), with the archives and the checksum file. A prerelease version is a GitHub prerelease and the formula
15
+ # is not touched. A GA version updates Formula/<command>.rb on main with a fast-forward push, never forced
16
+ # formula in on_macos and on_linux, each on_arm and on_intel block holds a url and the sha256 of the archive; a comment lists
17
+ # the platforms. Only darwin and linux on amd64 and arm64 can be in a formula; any other platform is released but left out
18
+ #
19
+ # A rerun is safe:
20
+ # - the release exists and its checksum file is the one just built: nothing is released again
21
+ # - the release exists with another checksum file: refuse, because a published version must not move
22
+ # - the release exists without a checksum file (an upload that did not finish): refuse, a person decides
23
+ # - a GA formula that already says this version is left alone, and one that says a higher version is not changed
24
+ #
25
+ # usage: binary-units.sh [--dry-run] <path>... units whose publish.to is not binary are skipped
26
+ # --dry-run print the release (tag, archives, formula) and write nothing: nothing is built, released or pushed
27
+ # BINARY_RELEASE_REPOSITORY the name of the release repository without the owner (binaryReleaseRepository of the organization values)
28
+ # BINARY_RELEASE_OWNER owner of that repository (default: the owner of GITHUB_REPOSITORY)
29
+ # BINARY_RELEASE_REMOTE full remote URL or path of the release repository for git (tests use a local bare repository)
30
+ # BINARY_RELEASE_TOKEN GitHub App token with contents: write on the release repository only; sent as an HTTP header that git reads
31
+ # from its environment (GIT_CONFIG_*), never in a URL or in an argument (default for gh: GH_TOKEN)
32
+ # BINARY_OUT where the archives are built (default tmp/binary)
33
+ set -euo pipefail
34
+ source "$(dirname "$0")/../lib/common.sh"
35
+ cd "$REPO_ROOT"
36
+
37
+ dry=0
38
+ if [[ "${1:-}" == --dry-run ]]; then dry=1; shift; fi
39
+ [[ $# -ge 1 ]] || die "usage: binary-units.sh [--dry-run] <path>..."
40
+
41
+ unit_tag() { printf '%s/v%s\n' "$(unit_component "$1")" "$2"; } # the unit tag, <ComponentName>/v<version>
42
+
43
+ # version_gt <a> <b> → success when version a is above b (X.Y.Z or X.Y.Z-<alpha|beta|rc>.N)
44
+ version_gt() {
45
+ local a="$1" b="$2" sa="${1%%-*}" sb="${2%%-*}"
46
+ [[ "$a" != "$b" ]] || return 1
47
+ if [[ "$sa" != "$sb" ]]; then
48
+ [[ "$(printf '%s\n%s\n' "$sa" "$sb" | sort -V | tail -1)" == "$sa" ]]
49
+ return
50
+ fi
51
+ [[ "$a" != "$sa" ]] || return 0 # a is the release, b a pre-release of it
52
+ [[ "$b" != "$sb" ]] || return 1
53
+ [[ "$(printf '%s\n%s\n' "${a#*-}" "${b#*-}" | sort -V | tail -1)" == "${a#*-}" ]]
54
+ }
55
+
56
+ release_token() { printf '%s\n' "${BINARY_RELEASE_TOKEN:-${GH_TOKEN:-}}"; }
57
+
58
+ # gh against the release repository, with its token
59
+ ghr() { GH_TOKEN="$(release_token)" gh "$@"; }
60
+ ghr_read() { GH_TOKEN="$(release_token)" read_retry gh "$@"; }
61
+
62
+ # git against the release repository. The token goes to git as an HTTP header set in the environment of that one call (GIT_CONFIG_*),
63
+ # so it is in no argument and no process list shows it
64
+ with_git_auth() { # with_git_auth <command>...
65
+ local t n="${GIT_CONFIG_COUNT:-0}"; t="$(release_token)"
66
+ if [[ -z "$t" ]]; then "$@"; return; fi
67
+ env "GIT_CONFIG_COUNT=$((n + 1))" "GIT_CONFIG_KEY_${n}=http.https://github.com/.extraheader" \
68
+ "GIT_CONFIG_VALUE_${n}=Authorization: Basic $(printf 'x-access-token:%s' "$t" | base64 | tr -d '\n')" "$@"
69
+ }
70
+ rgit() { with_git_auth git "$@"; }
71
+ rgit_push() { # a push that is asked again after a transient error of GitHub, never forced
72
+ with_git_auth node "$RETRY_MJS" push -- git push "$@"
73
+ }
74
+
75
+ release_repo() { # → owner/name of the release repository
76
+ local owner="${BINARY_RELEASE_OWNER:-${GITHUB_REPOSITORY:-}}"
77
+ owner="${owner%%/*}"
78
+ [[ -n "${BINARY_RELEASE_REPOSITORY:-}" ]] || die "組織值 binaryReleaseRepository 沒有填,不知道執行檔要發到哪個倉;有 binary 單元的倉要填它"
79
+ [[ "$BINARY_RELEASE_REPOSITORY" =~ ^[A-Za-z0-9._-]+$ ]] || die "binaryReleaseRepository「${BINARY_RELEASE_REPOSITORY}」只能是倉庫名,不含 owner"
80
+ [[ -n "$owner" ]] || die "不知道發布倉的 owner,請設 GITHUB_REPOSITORY(owner/repo)或 BINARY_RELEASE_OWNER"
81
+ printf '%s/%s\n' "$owner" "$BINARY_RELEASE_REPOSITORY"
82
+ }
83
+
84
+ release_remote() { # → the remote git uses for the release repository
85
+ if [[ -n "${BINARY_RELEASE_REMOTE:-}" ]]; then printf '%s\n' "$BINARY_RELEASE_REMOTE"
86
+ else printf 'https://github.com/%s.git\n' "$(release_repo)"; fi
87
+ }
88
+
89
+ sha256_of() { # sha256_of <file> → the hex digest
90
+ if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi
91
+ }
92
+
93
+ # pack_archive <executable> <command> <archive>: a tar.gz of the one file, the same bytes for the same executable
94
+ pack_archive() {
95
+ python3 - "$@" <<'PY'
96
+ import gzip, io, sys, tarfile
97
+ src, name, dest = sys.argv[1:4]
98
+ data = open(src, "rb").read()
99
+ with open(dest, "wb") as raw:
100
+ with gzip.GzipFile(filename="", mode="wb", fileobj=raw, mtime=0, compresslevel=9) as gz:
101
+ with tarfile.open(fileobj=gz, mode="w", format=tarfile.GNU_FORMAT) as tar:
102
+ info = tarfile.TarInfo(name)
103
+ info.size, info.mode, info.mtime, info.uid, info.gid = len(data), 0o755, 0, 0, 0
104
+ tar.addfile(info, io.BytesIO(data))
105
+ PY
106
+ }
107
+
108
+ # the Homebrew blocks of a platform: <os>/<arch> → "on_macos on_arm", or nothing when a formula cannot hold it
109
+ formula_slot() {
110
+ local os arch
111
+ case "${1%%/*}" in darwin) os=on_macos ;; linux) os=on_linux ;; *) return 1 ;; esac
112
+ case "${1#*/}" in arm64) arch=on_arm ;; amd64) arch=on_intel ;; *) return 1 ;; esac
113
+ printf '%s %s\n' "$os" "$arch"
114
+ }
115
+
116
+ # formula_text <command> <description> <version> <tag> <repo> <checksum file> <platform>...
117
+ formula_text() {
118
+ local cmd="$1" desc="$2" ver="$3" tag="$4" repo="$5" sums="$6" cls="" part pl slot os arch name sha listed=()
119
+ shift 6
120
+ for part in $(printf '%s' "$cmd" | tr '._-' ' '); do cls+="$(tr '[:lower:]' '[:upper:]' <<<"${part:0:1}")${part:1}"; done
121
+ for pl in "$@"; do formula_slot "$pl" >/dev/null && listed+=("$pl"); done
122
+ (( ${#listed[@]} )) || die "${cmd}:平台 $* 裡沒有 formula 放得下的(darwin 或 linux,amd64 或 arm64)"
123
+ printf '# Supported platforms: %s\n' "$(IFS=,; printf '%s' "${listed[*]}" | sed 's/,/, /g')"
124
+ printf 'class %s < Formula\n desc "%s"\n homepage "https://github.com/%s"\n version "%s"\n' "$cls" "${desc//\"/\\\"}" "$repo" "$ver"
125
+ for os in on_macos on_linux; do
126
+ local opened=0
127
+ for arch in on_arm on_intel; do
128
+ for pl in "${listed[@]}"; do
129
+ slot="$(formula_slot "$pl")"
130
+ [[ "$slot" == "$os $arch" ]] || continue
131
+ (( opened )) || { printf '\n %s do\n' "$os"; opened=1; }
132
+ name="${cmd}-${ver}-${pl%%/*}-${pl#*/}.tar.gz"
133
+ sha="$(awk -v n="$name" '$2 == n { print $1 }' "$sums")"
134
+ [[ -n "$sha" ]] || die "$sums 沒有 $name 的一行"
135
+ printf ' %s do\n url "https://github.com/%s/releases/download/%s/%s"\n sha256 "%s"\n end\n' "$arch" "$repo" "$tag" "$name" "$sha"
136
+ done
137
+ done
138
+ (( ! opened )) || printf ' end\n'
139
+ done
140
+ printf '\n def install\n bin.install "%s"\n end\n\n test do\n assert_match version.to_s, shell_output("#{bin}/%s --version")\n end\nend\n' "$cmd" "$cmd"
141
+ }
142
+
143
+ summary() { [[ -z "${GITHUB_STEP_SUMMARY:-}" ]] || printf '%s\n' "- $*" >> "$GITHUB_STEP_SUMMARY"; }
144
+
145
+ # update_formula <command> <description> <version> <tag> <repo> <checksum file> <source tag> <platform>...
146
+ update_formula() {
147
+ local cmd="$1" desc="$2" ver="$3" tag="$4" repo="$5" sums="$6" ref="$7" remote work text have cur ad
148
+ shift 7
149
+ remote="$(release_remote)"
150
+ work="$(mktemp -d "${TMPDIR:-/tmp}/formula.XXXXXX")"
151
+ text="$(formula_text "$cmd" "$desc" "$ver" "$tag" "$repo" "$sums" "$@")"
152
+ [[ -n "$(rgit ls-remote "$remote" refs/heads/main | cut -f1)" ]] || { rm -rf "$work"; die "發布倉 ${repo} 沒有 main,formula 沒有地方推;先建好有 main 的發布倉"; }
153
+ rgit clone -q --no-tags --branch main "$remote" "$work"
154
+ if [[ -f "$work/Formula/$cmd.rb" ]]; then
155
+ cur="$(sed -n 's/^ version "\(.*\)"$/\1/p' "$work/Formula/$cmd.rb" | head -1)"
156
+ if [[ -n "$cur" ]] && version_gt "$cur" "$ver"; then
157
+ info "formula $cmd 已是更高的版本 ${cur},不改成 $ver"; rm -rf "$work"; return 0
158
+ fi
159
+ have="$(cat "$work/Formula/$cmd.rb")"
160
+ if [[ "$have" == "$text" ]]; then info "formula $cmd 已是 ${ver},沒有要推的"; rm -rf "$work"; return 0; fi
161
+ fi
162
+ mkdir -p "$work/Formula"
163
+ printf '%s\n' "$text" > "$work/Formula/$cmd.rb"
164
+ # the commit is made by the bot, not by the author of the source repository: that one may be private and its address would be public here
165
+ ad="$(git log -1 --format='%aI' "$ref")"
166
+ local bot_name="github-actions[bot]" bot_email="41898283+github-actions[bot]@users.noreply.github.com"
167
+ GIT_AUTHOR_NAME="$bot_name" GIT_AUTHOR_EMAIL="$bot_email" GIT_AUTHOR_DATE="$ad" GIT_COMMITTER_NAME="$bot_name" GIT_COMMITTER_EMAIL="$bot_email" GIT_COMMITTER_DATE="$ad" \
168
+ git -C "$work" add "Formula/$cmd.rb"
169
+ GIT_AUTHOR_NAME="$bot_name" GIT_AUTHOR_EMAIL="$bot_email" GIT_AUTHOR_DATE="$ad" GIT_COMMITTER_NAME="$bot_name" GIT_COMMITTER_EMAIL="$bot_email" GIT_COMMITTER_DATE="$ad" \
170
+ git -C "$work" commit -q --no-gpg-sign -m "$cmd $ver" -m "Source: $ref"
171
+ # a fast-forward only: a push that is not one is refused by the remote, and the script stops with its error
172
+ ( cd "$work" && rgit_push origin HEAD:refs/heads/main )
173
+ rm -rf "$work"
174
+ log "formula $cmd → $ver 已推到 ${repo} 的 main"
175
+ summary "formula \`$cmd\` → \`$ver\`(${repo})"
176
+ }
177
+
178
+ binary_unit() { # binary_unit <path>
179
+ local p="${1%/}" comp ver stage cmd desc tag repo ref work out sums host plat os arch bin archive pre=0 title sum_have
180
+ local -a platforms=() archives=()
181
+ comp="$(unit_component "$p")"
182
+ if [[ "$(policy_unit_publish_to "$comp")" != binary ]]; then info "$p: publish.to is not binary, skipped"; return 0; fi
183
+ [[ -f "$p/go.mod" ]] || die "${p}:publish.to 是 binary,但單元沒有 go.mod;這一版的執行檔發布只支援 Go"
184
+ jq -e --arg p "$p" '.packages | has($p)' "$CONFIG" >/dev/null || die "unexpected package path: $p (not a key of packages in $CONFIG)"
185
+ ver="$(manifest_version "$p")"
186
+ [[ -n "$ver" ]] || die "$p 不在 $MANIFEST"
187
+ stage="$(version_stage "$ver")" || die "$p: version \"$ver\" is not X.Y.Z or X.Y.Z-<alpha|beta|rc>.N"
188
+ [[ "$stage" == ga ]] || pre=1
189
+ cmd="$(policy_unit_publish_field "$comp" command)"
190
+ desc="$(policy_unit_publish_field "$comp" description)"
191
+ mapfile -t platforms < <(policy_unit_publish_field "$comp" platforms)
192
+ [[ -n "$cmd" && -n "$desc" ]] || die "${p}:政策檔的 units.${comp}.publish 要寫 command 與 description"
193
+ (( ${#platforms[@]} )) || die "${p}:政策檔的 units.${comp}.publish 要寫 platforms"
194
+ for plat in "${platforms[@]}"; do [[ "$plat" =~ ^[a-z0-9]+/[a-z0-9]+$ ]] || die "${p}:平台「${plat}」不是 <os>/<arch>"; done
195
+ [[ -f "$p/version.txt" ]] || die "${p}:執行檔的單元要有 version.txt,放它的版號"
196
+ [[ "$(tr -d '[:space:]' < "$p/version.txt")" == "$ver" ]] \
197
+ || die "${p}:manifest 是 ${ver},但 $p/version.txt 是 $(tr -d '[:space:]' < "$p/version.txt");release-please 沒有更新到這個檔"
198
+ tag="$(unit_tag "$p" "$ver")"
199
+ repo="$(release_repo)"
200
+
201
+ if (( dry )); then
202
+ echo "## $comp ($p $ver)"
203
+ echo "release repo $repo"
204
+ echo "release tag $tag$( (( pre )) && echo ' (prerelease)')"
205
+ for plat in "${platforms[@]}"; do echo "archive ${cmd}-${ver}-${plat%%/*}-${plat#*/}.tar.gz"; done
206
+ echo "checksum ${cmd}-${ver}.sha256"
207
+ if (( pre )); then echo "formula not changed (prerelease)"; else echo "formula Formula/${cmd}.rb"; fi
208
+ echo "dry run: nothing built, released or pushed"
209
+ return 0
210
+ fi
211
+
212
+ ref="$tag"
213
+ git rev-parse -q --verify "refs/tags/$ref^{commit}" >/dev/null 2>&1 || die "release tag $ref does not exist, so $p $ver has not been released"
214
+ command -v go >/dev/null 2>&1 || die "找不到 go:binary job 要先用 actions/setup-go 裝 go.mod 指定的版本"
215
+ work="${BINARY_OUT:-$REPO_ROOT/tmp/binary}/${comp}-${ver}"
216
+ out="$work/out"
217
+ rm -rf "$work"; mkdir -p "$out"
218
+ host="$(go env GOHOSTOS)/$(go env GOHOSTARCH)"
219
+
220
+ for plat in "${platforms[@]}"; do
221
+ os="${plat%%/*}" arch="${plat#*/}"
222
+ mkdir -p "$work/bin/$os-$arch"
223
+ log "build:$cmd $ver for $plat"
224
+ ( cd "$p" && CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" go build -trimpath -buildvcs=false -o "$work/bin/$os-$arch/$cmd" . )
225
+ archive="${cmd}-${ver}-${os}-${arch}.tar.gz"
226
+ pack_archive "$work/bin/$os-$arch/$cmd" "$cmd" "$out/$archive"
227
+ archives+=("$archive")
228
+ done
229
+ sums="$out/${cmd}-${ver}.sha256"
230
+ : > "$sums"
231
+ for archive in "${archives[@]}"; do printf '%s %s\n' "$(sha256_of "$out/$archive")" "$archive" >> "$sums"; done
232
+
233
+ # the executable of the runner platform must say it is this version
234
+ if [[ ! -x "$work/bin/${host%%/*}-${host#*/}/$cmd" ]]; then
235
+ mkdir -p "$work/bin/host"
236
+ ( cd "$p" && CGO_ENABLED=0 GOOS="${host%%/*}" GOARCH="${host#*/}" go build -trimpath -buildvcs=false -o "$work/bin/host/$cmd" . )
237
+ bin="$work/bin/host/$cmd"
238
+ else
239
+ bin="$work/bin/${host%%/*}-${host#*/}/$cmd"
240
+ fi
241
+ sum_have="$("$bin" --version 2>&1)" || die "$cmd --version 在 ${host} 失敗:$sum_have"
242
+ [[ "$sum_have" == *"$ver"* ]] || die "$cmd --version 印的是「${sum_have}」,沒有版號 ${ver}:版號要從 version.txt 進到執行檔裡"
243
+
244
+ # what the release repository has now
245
+ local view rc=0 existing
246
+ view="$(ghr_read release view "$tag" --repo "$repo" 2>&1)" || rc=$?
247
+ if (( rc == 0 )); then
248
+ existing="$work/existing"; mkdir -p "$existing"
249
+ ghr_read release download "$tag" --repo "$repo" --pattern "${cmd}-${ver}.sha256" --dir "$existing" >/dev/null 2>&1 || true
250
+ [[ -s "$existing/${cmd}-${ver}.sha256" ]] || die "$repo 已有 release ${tag},但沒有 ${cmd}-${ver}.sha256:上傳沒有做完,請使用者決定怎麼補,不要覆蓋已發布的檔"
251
+ cmp -s "$existing/${cmd}-${ver}.sha256" "$sums" \
252
+ || die "$repo 的 release $tag 已有 ${cmd}-${ver}.sha256,但內容和這次建置的不同;已發布的版本不能動,請使用者決定,改發下一個版號"
253
+ echo "$cmd $tag is already released in $repo, nothing to release"
254
+ else
255
+ [[ "$view" == *"release not found"* ]] || die "查 $repo 的 release $tag 失敗:$view"
256
+ title="$comp $ver"
257
+ local -a flags=()
258
+ (( ! pre )) || flags+=(--prerelease)
259
+ log "release:$tag → ${repo}(${#archives[@]} 個壓縮檔與 checksum)"
260
+ ghr release create "$tag" --repo "$repo" --title "$title" --notes "Source: $ref" --target main ${flags[@]+"${flags[@]}"} \
261
+ "${archives[@]/#/$out/}" "$sums"
262
+ summary "執行檔 \`$cmd $ver\` → \`$repo\` release \`$tag\`$( (( pre )) && echo '(prerelease)')"
263
+ fi
264
+
265
+ if (( pre )); then
266
+ info "預發布版本,不更新 formula"
267
+ else
268
+ update_formula "$cmd" "$desc" "$ver" "$tag" "$repo" "$sums" "$ref" "${platforms[@]}"
269
+ fi
270
+ }
271
+
272
+ for p in "$@"; do binary_unit "$p"; done
@@ -0,0 +1,171 @@
1
+ #!/usr/bin/env bash
2
+ # Push the container images of the units whose publish.to in release-policy.json is image (blueprint-shared.publishing.container-image)
3
+ # and attach each digest to the Release of the unit tag in this repository.
4
+ #
5
+ # image ghcr.io/<owner>/<ComponentName>:<version>, one tag per version and never latest. A prerelease version is pushed too,
6
+ # with its full version as the tag (1.2.0-rc.1)
7
+ # build docker buildx build with the Dockerfile in the unit directory (the build context), for every <os>/<arch> of
8
+ # publish.platforms, in one command. The Dockerfile compiles on the build platform (FROM --platform=$BUILDPLATFORM)
9
+ # and does not run under emulation
10
+ # digest <ComponentName>-<version>.digest, one line "sha256:...", an asset of the Release of the unit tag
11
+ # (<ComponentName>/v<version>) in this repository. The digest is also written to the job summary
12
+ # login docker login ghcr.io with the token of the job (GITHUB_TOKEN, packages: write). DOCKER_CONFIG is a new directory
13
+ # in the temporary directory, and it is deleted when the script ends, whatever the exit status. Its config.json lists
14
+ # the cli-plugins directory of the original DOCKER_CONFIG (cliPluginsExtraDirs), so the buildx plugin is still found
15
+ # builder docker buildx create --driver docker-container, named publish-<pid>, used for the build and removed when the script
16
+ # ends, whatever the exit status. A runner needs docker and the buildx plugin (selfHostedRunnerLabels.docker)
17
+ # config the image carries no configuration of the organization: it is mounted at deployment
18
+ #
19
+ # A rerun is safe:
20
+ # - the image exists: nothing is built or pushed, and its digest is attached to the Release when the Release has none
21
+ # - the image exists and the digest on the Release is another one: refuse, because a published version must not move
22
+ # - only an answer of the registry that says the image is not there (not found, MANIFEST_UNKNOWN) counts as "no image"; any other
23
+ # error of the inspect stops the run and is printed
24
+ # - the image does not exist but the Release already has a digest: refuse, a person decides
25
+ #
26
+ # A package is private after its first push. A person makes it public once in GitHub, and that cannot be undone.
27
+ #
28
+ # usage: image-units.sh [--dry-run] <path>... units whose publish.to is not image are skipped
29
+ # --dry-run print the image and the digest file and run nothing: no login, no build, no push
30
+ # IMAGE_OWNER owner of the image (default: the owner of GITHUB_REPOSITORY, in lower case, as ghcr.io requires)
31
+ # IMAGE_REGISTRY the registry host (default ghcr.io)
32
+ # IMAGE_TOKEN the token docker logs in with (default: GITHUB_TOKEN, then GH_TOKEN)
33
+ # IMAGE_ACTOR the user name of the login (default: GITHUB_ACTOR, then x-access-token)
34
+ # GH_TOKEN the token gh reads and writes the Release of this repository with (contents: write)
35
+ # GITHUB_REPOSITORY owner/repo of this repository, the one that has the Release
36
+ # IMAGE_OUT where the digest file and the build metadata are written (default tmp/image)
37
+ set -euo pipefail
38
+ source "$(dirname "$0")/../lib/common.sh"
39
+ cd "$REPO_ROOT"
40
+
41
+ dry=0
42
+ if [[ "${1:-}" == --dry-run ]]; then dry=1; shift; fi
43
+ [[ $# -ge 1 ]] || die "usage: image-units.sh [--dry-run] <path>..."
44
+
45
+ unit_tag() { printf '%s/v%s\n' "$(unit_component "$1")" "$2"; } # the unit tag, <ComponentName>/v<version>
46
+
47
+ summary() { [[ -z "${GITHUB_STEP_SUMMARY:-}" ]] || printf '%s\n' "- $*" >> "$GITHUB_STEP_SUMMARY"; }
48
+
49
+ registry() { printf '%s\n' "${IMAGE_REGISTRY:-ghcr.io}"; }
50
+
51
+ image_owner() {
52
+ local owner="${IMAGE_OWNER:-${GITHUB_REPOSITORY:-}}"
53
+ owner="${owner%%/*}"
54
+ [[ -n "$owner" ]] || die "不知道映像的 owner,請設 GITHUB_REPOSITORY(owner/repo)或 IMAGE_OWNER"
55
+ printf '%s\n' "$owner" | tr '[:upper:]' '[:lower:]'
56
+ }
57
+
58
+ # the DOCKER_CONFIG of this run: a directory of its own that holds the credential of the login, deleted when the script ends
59
+ docker_cfg=""
60
+ builder=""
61
+ cleanup() {
62
+ local rc=$?
63
+ # the builder is removed with the DOCKER_CONFIG it was made in; a failure here does not change the exit status
64
+ [[ -z "$builder" ]] || docker buildx rm "$builder" >/dev/null 2>&1 || true
65
+ [[ -z "$docker_cfg" ]] || rm -rf "$docker_cfg"
66
+ return "$rc"
67
+ }
68
+ trap 'cleanup; report_skipped_external' EXIT
69
+
70
+ docker_login() {
71
+ [[ -z "$docker_cfg" ]] || return 0 # one login for the whole run
72
+ local token="${IMAGE_TOKEN:-${GITHUB_TOKEN:-${GH_TOKEN:-}}}"
73
+ [[ -n "$token" ]] || die "沒有可以登入 $(registry) 的 token:job 的 GITHUB_TOKEN 要帶 packages: write,並設成 IMAGE_TOKEN 或 GITHUB_TOKEN"
74
+ local plugins="${DOCKER_CONFIG:-$HOME/.docker}/cli-plugins"
75
+ docker_cfg="$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/docker-config.XXXXXX")"
76
+ # the new directory has no plugins of its own, so it points at the ones of the runner
77
+ if [[ -d "$plugins" ]]; then jq -n --arg d "$plugins" '{cliPluginsExtraDirs: [$d]}' > "$docker_cfg/config.json"; fi
78
+ export DOCKER_CONFIG="$docker_cfg"
79
+ printf '%s' "$token" | docker login "$(registry)" -u "${IMAGE_ACTOR:-${GITHUB_ACTOR:-x-access-token}}" --password-stdin >/dev/null
80
+ }
81
+
82
+ # the digest of an image that is in the registry, or nothing when it is not there
83
+ # Only an answer that says the image is not there is "nothing"; any other error (network, auth, daemon) stops the run
84
+ image_digest() { # image_digest <ref>
85
+ local out rc=0
86
+ out="$(docker buildx imagetools inspect "$1" --format '{{.Manifest.Digest}}' 2>&1)" || rc=$?
87
+ if (( rc == 0 )); then printf '%s\n' "$out"; return 0; fi
88
+ shopt -s nocasematch
89
+ # A package that was never pushed answers NAME_UNKNOWN ("name unknown"), a missing tag answers MANIFEST_UNKNOWN or "not found"
90
+ if [[ "$out" == *"not found"* || "$out" == *MANIFEST_UNKNOWN* || "$out" == *NAME_UNKNOWN* || "$out" == *"name unknown"* ]]; then shopt -u nocasematch; return 0; fi
91
+ shopt -u nocasematch
92
+ die "查 $1 在 $(registry) 的狀態失敗,不當成映像不存在:$out"
93
+ }
94
+
95
+ image_unit() { # image_unit <path>
96
+ local p="${1%/}" comp ver ref tag repo out file="" platforms_csv have attached view rc=0 existing digest
97
+ local -a platforms=()
98
+ comp="$(unit_component "$p")"
99
+ if [[ "$(policy_unit_publish_to "$comp")" != image ]]; then info "$p: publish.to is not image, skipped"; return 0; fi
100
+ [[ -f "$p/Dockerfile" ]] || die "${p}:publish.to 是 image,但單元目錄沒有 Dockerfile"
101
+ jq -e --arg p "$p" '.packages | has($p)' "$CONFIG" >/dev/null || die "unexpected package path: $p (not a key of packages in $CONFIG)"
102
+ ver="$(manifest_version "$p")"
103
+ [[ -n "$ver" ]] || die "$p 不在 $MANIFEST"
104
+ version_stage "$ver" >/dev/null || die "$p: version \"$ver\" is not X.Y.Z or X.Y.Z-<alpha|beta|rc>.N"
105
+ mapfile -t platforms < <(policy_unit_publish_field "$comp" platforms)
106
+ (( ${#platforms[@]} )) || die "${p}:政策檔的 units.${comp}.publish 要寫 platforms"
107
+ for have in "${platforms[@]}"; do [[ "$have" =~ ^[a-z0-9]+/[a-z0-9]+$ ]] || die "${p}:平台「${have}」不是 <os>/<arch>"; done
108
+ platforms_csv="$(IFS=,; printf '%s' "${platforms[*]}")"
109
+ ref="$(registry)/$(image_owner)/${comp}:${ver}"
110
+ tag="$(unit_tag "$p" "$ver")"
111
+ file="${comp}-${ver}.digest"
112
+ repo="${GITHUB_REPOSITORY:-}"
113
+
114
+ if (( dry )); then
115
+ echo "## $comp ($p $ver)"
116
+ echo "image $ref"
117
+ echo "platforms $platforms_csv"
118
+ echo "digest file $file (asset of the Release $tag${repo:+ in $repo})"
119
+ echo "dry run: nothing built or pushed"
120
+ return 0
121
+ fi
122
+
123
+ [[ -n "$repo" ]] || die "不知道要把 digest 附到哪個倉的 Release,請設 GITHUB_REPOSITORY(owner/repo)"
124
+ git rev-parse -q --verify "refs/tags/$tag^{commit}" >/dev/null 2>&1 || die "release tag $tag does not exist, so $p $ver has not been released"
125
+ command -v docker >/dev/null 2>&1 || die "找不到 docker:image job 要跑在組織值 selfHostedRunnerLabels.docker 指定的 runner 上;組織沒有給這個值,就沒有能建映像的 runner"
126
+ docker version >/dev/null 2>&1 || die "docker 連不上 daemon:image job 要跑在有 docker 的 runner 上(組織值 selfHostedRunnerLabels.docker)"
127
+ docker buildx version >/dev/null 2>&1 || die "docker 沒有 buildx 外掛:image job 要跑在裝了 docker buildx 的 runner 上(組織值 selfHostedRunnerLabels.docker)"
128
+ out="${IMAGE_OUT:-$REPO_ROOT/tmp/image}/${comp}-${ver}"
129
+ rm -rf "$out"; mkdir -p "$out"
130
+
131
+ # the Release of the unit tag must be there: release-please creates it with the tag
132
+ view="$(read_retry gh release view "$tag" --repo "$repo" 2>&1)" || rc=$?
133
+ if (( rc != 0 )); then
134
+ [[ "$view" == *"release not found"* ]] && die "$repo 沒有 $tag 的 Release,digest 沒有地方附;release-please 發版時會建它"
135
+ die "查 $repo 的 Release $tag 失敗:$view"
136
+ fi
137
+ attached=""
138
+ existing="$out/existing"; mkdir -p "$existing"
139
+ read_retry gh release download "$tag" --repo "$repo" --pattern "$file" --dir "$existing" >/dev/null 2>&1 || true
140
+ [[ ! -s "$existing/$file" ]] || attached="$(tr -d '[:space:]' < "$existing/$file")"
141
+
142
+ docker_login
143
+ digest="$(image_digest "$ref")"
144
+ if [[ -n "$digest" ]]; then
145
+ if [[ -n "$attached" && "$attached" != "$digest" ]]; then
146
+ die "$ref 已在 $(registry),digest 是 ${digest},但 Release $tag 附的 $file 是 ${attached};已發布的版本不能動,請使用者決定,改發下一個版號"
147
+ fi
148
+ echo "$ref is already pushed ($digest), not built again"
149
+ else
150
+ [[ -z "$attached" ]] || die "$ref 不在 $(registry),但 Release $tag 已附 ${file}(${attached});請使用者決定怎麼處理,不要推出另一個 digest"
151
+ log "build and push:${ref}(${platforms_csv})"
152
+ local -a labels=(--label "org.opencontainers.image.version=$ver")
153
+ labels+=(--label "org.opencontainers.image.source=https://github.com/$repo")
154
+ if [[ -z "$builder" ]]; then
155
+ docker buildx create --name "publish-$$" --driver docker-container --use >/dev/null
156
+ builder="publish-$$"
157
+ fi
158
+ docker buildx build --platform "$platforms_csv" --file "$p/Dockerfile" --tag "$ref" "${labels[@]}" --metadata-file "$out/metadata.json" --push "$p"
159
+ digest="$(jq -r '."containerimage.digest" // empty' "$out/metadata.json")"
160
+ [[ "$digest" =~ ^sha256:[0-9a-f]{64}$ ]] || die "建置沒有回報 $ref 的 digest(${digest:-空})"
161
+ fi
162
+
163
+ printf '%s\n' "$digest" > "$out/$file"
164
+ if [[ -z "$attached" ]]; then
165
+ gh release upload "$tag" "$out/$file" --repo "$repo"
166
+ log "digest $digest 已附到 $repo 的 Release $tag"
167
+ fi
168
+ summary "映像 \`$ref\`,digest \`$digest\`(附在 Release \`$tag\`)"
169
+ }
170
+
171
+ for p in "$@"; do image_unit "$p"; done
@@ -2,7 +2,9 @@
2
2
  # Publish the units a release created, at every stage of a train. publish.yml calls it with the unit paths of the
3
3
  # release and the stage; release-please.yml dispatches publish.yml after each release.
4
4
  #
5
- # The target of a unit follows from the file its directory carries:
5
+ # The target of a unit comes from publish.to of the unit in release-policy.json when it has one: binary (binary-units.sh
6
+ # releases executables), image (image-units.sh pushes a container image) or none (not published, whatever files it has).
7
+ # A unit without publish.to follows from the file its directory carries:
6
8
  # package.json npm publishConfig.registry unset or https://registry.npmjs.org. Public package,
7
9
  # Trusted Publishing (OIDC, npm >= 11.5.1), no token is read. dist-tag is the stage
8
10
  # (alpha | beta | rc), latest at ga: latest must only ever point at a GA. pnpm packs
@@ -20,6 +22,10 @@
20
22
  # published by hand (--crate-exists).
21
23
  # go.mod go nothing is uploaded: mirror-units.sh pushes the unit to its mirror repository and
22
24
  # Package.swift swift tags it there (vX.Y.Z for Go, X.Y.Z for Swift). This script only checks the unit.
25
+ # (publish.to) binary a Go unit (go.mod) built for the platforms of the policy and released to the public release
26
+ # repository, with its formula at a GA version. binary-units.sh does it, this script only
27
+ # checks the unit: version.txt equals the manifest and the unit tag exists.
28
+ # (publish.to) image a unit with a Dockerfile, pushed to ghcr by image-units.sh. This script only checks it.
23
29
  # Every unit's version must belong to <stage> (version_stage in scripts/lib/release-stages.sh), so a dispatch with the
24
30
  # wrong stage stops instead of publishing a prerelease under latest. A version that is already on the registry is
25
31
  # skipped, so a re-run is safe. The unit's own file must carry the version of .release-please-manifest.json, and a
@@ -32,17 +38,20 @@
32
38
  #
33
39
  # usage: publish-units.sh [pack|upload] <alpha|beta|rc|ga> <path>...
34
40
  # publish-units.sh --registries <path>... print npm=true / github-packages=true / pypi=true / crates=true /
35
- # go=true / swift=true for the targets the paths need ($GITHUB_OUTPUT format)
41
+ # go=true / swift=true / binary=true / image=true for the targets the paths need
42
+ # ($GITHUB_OUTPUT format)
36
43
  # publish-units.sh --filter <path>... print the paths that are units to publish, one per line
37
44
  # publish-units.sh --report <path>... print one line per path, once each: "unit <path> -> <target>" for a unit to
38
- # publish, "unit <path> -> not-published" for any other path (the plan job log)
45
+ # publish, "unit <path> -> not-published" for any other path (the plan job log). A
46
+ # unit whose target cannot be told (for example publish.to binary without go.mod)
47
+ # prints the reason on stderr and the exit status is 1 after the last path.
39
48
  # publish-units.sh --mirrors <path>... print the mirror repository names of the Go and Swift units, comma separated
40
49
  # publish-units.sh --crate-exists <path>... exit 0 when every crates.io unit among the paths already has its crate
41
50
  # on crates.io, 1 when one has not, 2 when crates.io cannot be asked
42
51
  # PUBLISH_DRY_RUN 1 prints one line per unit (target, name@version, registry, dist-tag) and the command that would
43
52
  # run, and runs nothing: nothing is packed, built or uploaded. A version the registry already has
44
53
  # is printed as skipped.
45
- # PUBLISH_REGISTRIES comma list of targets (npm,github-packages,pypi,crates,go,swift): act only on the units of these
54
+ # PUBLISH_REGISTRIES comma list of targets (npm,github-packages,pypi,crates,go,swift,binary,image): act only on the units of these
46
55
  # targets. The checks still run for every unit. publish.yml has one job per kind of target.
47
56
  # PUBLISH_CHECK_ONLY 1 stops after the checks (file version, stage, same content as the tag)
48
57
  # GH_TOKEN a token that can read origin (publish.yml gives github.token). Only the git commands that read the
@@ -77,9 +86,30 @@ npm_dist_tag() { if [[ "$1" == ga ]]; then printf '%s\n' latest; else printf '%s
77
86
 
78
87
  summary() { [[ -z "${GITHUB_STEP_SUMMARY:-}" ]] || printf '%s\n' "- $*" >> "$GITHUB_STEP_SUMMARY"; }
79
88
 
80
- # unit_target <path> → npm | github-packages | pypi | crates | go | swift, from the file the unit carries
89
+ # unit_publish_to <path> → binary | image | none, the publish.to of the unit in release-policy.json; nothing when it has none
90
+ unit_publish_to() {
91
+ local to
92
+ to="$(policy_unit_publish_to "$(unit_component "$1")")"
93
+ case "$to" in
94
+ "" | binary | image | none) printf '%s\n' "$to" ;;
95
+ *) die "$1:政策檔的 publish.to 是 ${to},只能是 binary、image 或 none" ;;
96
+ esac
97
+ }
98
+
99
+ # unit_target <path> → npm | github-packages | pypi | crates | go | swift | binary | image. publish.to of the unit
100
+ # decides when it has one (none has no target, so it fails); otherwise the file the unit carries does.
81
101
  unit_target() {
82
- local reg
102
+ local reg to
103
+ to="$(unit_publish_to "$1")" || return 1
104
+ case "$to" in
105
+ binary)
106
+ [[ -f "$1/go.mod" ]] || { echo "$1:publish.to 是 binary,但單元沒有 go.mod;這一版的執行檔發布只支援 Go" >&2; return 1; }
107
+ printf '%s\n' binary; return 0 ;;
108
+ image)
109
+ [[ -f "$1/Dockerfile" ]] || { echo "$1:publish.to 是 image,但單元目錄沒有 Dockerfile" >&2; return 1; }
110
+ printf '%s\n' image; return 0 ;;
111
+ none) return 1 ;;
112
+ esac
83
113
  if [[ -f "$1/package.json" ]]; then
84
114
  reg="$(jq -r '.publishConfig.registry // empty' "$1/package.json")"
85
115
  case "${reg%/}" in
@@ -94,13 +124,17 @@ unit_target() {
94
124
  else return 1; fi
95
125
  }
96
126
 
97
- target_of() { # target_of <path> → the target, or stop
98
- unit_target "$1" || die "$1 沒有 package.json(publishConfig.registry 只能是 npmjs 或 GitHub Packages)、pyproject.toml、Cargo.toml、go.mod 或 Package.swift,不知道要發布到哪裡"
127
+ target_of() { # target_of <path> → the target, or stop. A unit that names its target in the policy has already said why it fails.
128
+ unit_target "$1" && return 0
129
+ [[ -z "$(unit_publish_to "$1")" ]] && die "$1 沒有 package.json(publishConfig.registry 只能是 npmjs 或 GitHub Packages)、pyproject.toml、Cargo.toml、go.mod 或 Package.swift,不知道要發布到哪裡"
130
+ return 1
99
131
  }
100
132
 
101
- # a unit is published when it carries one of those files, and a package.json only when it has a publishConfig: a
102
- # unit that is not published (a shared library of the repository) has none
133
+ # a unit is published when its publish.to is binary or image; with none it is not, whatever files it has. Without publish.to it
134
+ # is published when it carries one of those files, and a package.json only when it has a publishConfig: a unit that is not
135
+ # published (a shared library of the repository) has none
103
136
  publishable() { # publishable <path>
137
+ case "$(unit_publish_to "$1")" in binary | image) return 0 ;; none) return 1 ;; esac
104
138
  if [[ -f "$1/package.json" ]]; then jq -e 'has("publishConfig")' "$1/package.json" >/dev/null
105
139
  else [[ -f "$1/pyproject.toml" || -f "$1/Cargo.toml" || -f "$1/go.mod" || -f "$1/Package.swift" ]]; fi
106
140
  }
@@ -111,17 +145,21 @@ unit_name() { # unit_name <path> <target>
111
145
  pypi) toml_get "$1/pyproject.toml" project name ;;
112
146
  crates) toml_get "$1/Cargo.toml" package name ;;
113
147
  go) sed -n 's/^module //p' "$1/go.mod" | head -1 ;;
148
+ binary) policy_unit_publish_field "$(unit_component "$1")" command ;;
149
+ image) unit_component "$1" ;;
114
150
  swift) sed -n 's/^ *name: *"\([^"]*\)".*/\1/p' "$1/Package.swift" | head -1 ;;
115
151
  esac
116
152
  }
117
153
 
118
- # the version the unit's own file carries; empty for a Go unit (no file) and for a Swift unit without version.txt
154
+ # the version the unit's own file carries; empty for a Go unit (no file) and for a Swift unit without version.txt. A binary unit
155
+ # carries it in version.txt, and a missing file reads as an empty version.
119
156
  file_version() { # file_version <path> <target> → "<file>\t<version>"
120
157
  case "$2" in
121
158
  npm | github-packages) printf '%s\t%s\n' "$1/package.json" "$(jq -r .version "$1/package.json")" ;;
122
159
  pypi) printf '%s\t%s\n' "$1/pyproject.toml" "$(toml_get "$1/pyproject.toml" project version)" ;;
123
160
  crates) printf '%s\t%s\n' "$1/Cargo.toml" "$(toml_get "$1/Cargo.toml" package version)" ;;
124
161
  swift) [[ ! -f "$1/version.txt" ]] || printf '%s\t%s\n' "$1/version.txt" "$(tr -d '[:space:]' < "$1/version.txt")" ;;
162
+ binary) printf '%s\t%s\n' "$1/version.txt" "$([[ ! -f "$1/version.txt" ]] || tr -d '[:space:]' < "$1/version.txt")" ;;
125
163
  esac
126
164
  }
127
165
 
@@ -181,6 +219,14 @@ mirror_name() { # mirror_name <path>
181
219
  else die "$1: 不知道鏡像倉的 owner,請設 GITHUB_REPOSITORY(owner/repo)或 MIRROR_REMOTE"; fi
182
220
  }
183
221
 
222
+ # the owner of the repository this run is in (GITHUB_REPOSITORY is owner/repo)
223
+ github_owner() {
224
+ local owner="${GITHUB_REPOSITORY:-}"
225
+ owner="${owner%%/*}"
226
+ [[ -n "$owner" ]] || die "不知道 owner,請設 GITHUB_REPOSITORY(owner/repo)"
227
+ printf '%s\n' "$owner"
228
+ }
229
+
184
230
  # the registry a line names: the host, or the mirror repository (mirror_name) for Go and Swift
185
231
  registry_name() { # registry_name <target> <path>
186
232
  case "$1" in
@@ -189,6 +235,8 @@ registry_name() { # registry_name <target> <path>
189
235
  pypi) echo pypi.org ;;
190
236
  crates) echo crates.io ;;
191
237
  go | swift) mirror_name "$2" ;;
238
+ binary) echo github-release ;;
239
+ image) echo "ghcr.io/$(github_owner)/$(unit_component "$2")" ;;
192
240
  esac
193
241
  }
194
242
 
@@ -270,6 +318,10 @@ plan_unit() { # plan_unit <path> <version> <stage> <target>
270
318
  plan_line dry-run "$t" "$name" "$ver" "$reg" "" "cargo publish --locked --no-verify --manifest-path $p/Cargo.toml" ;;
271
319
  go | swift)
272
320
  plan_line dry-run "$t" "$name" "$ver" "$reg" "mirror tag $([[ "$t" == go ]] && echo "v$ver" || echo "$ver")" "bash scripts/release/mirror-units.sh $p" ;;
321
+ binary)
322
+ plan_line dry-run "$t" "$name" "$ver" "$reg" "release tag $(unit_tag "$p" "$ver")" "bash scripts/release/binary-units.sh $p" ;;
323
+ image)
324
+ plan_line dry-run "$t" "$name" "$ver" "$reg" "image tag $ver" "bash scripts/release/image-units.sh $p" ;;
273
325
  esac
274
326
  }
275
327
 
@@ -400,17 +452,23 @@ publish_crates() { # publish_crates <path> <version>
400
452
  summary "crates.io \`$name $ver\`"
401
453
  }
402
454
 
403
- # --- Go and Swift: nothing to upload ------------------------------------------------------------------------------
404
- # release-please creates the unit tag when the Release PR merges, and mirror-units.sh pushes the unit to its mirror
405
- # repository. A real run requires the unit tag to exist.
455
+ # --- Go, Swift, binary and image: nothing to upload here --------------------------------------------------------------
456
+ # release-please creates the unit tag when the Release PR merges. mirror-units.sh pushes a Go or Swift unit to its mirror
457
+ # repository, binary-units.sh releases the executables of a binary unit, and image-units.sh pushes the image of an image unit.
458
+ # A real run requires the unit tag to exist.
406
459
  publish_tag() { # publish_tag <path> <version> <target>
407
- local p="$1" ver="$2" t
460
+ local p="$1" ver="$2" t by
408
461
  t="$(unit_tag "$p" "$ver")"
409
462
  if ! git rev-parse -q --verify "refs/tags/$t" >/dev/null 2>&1; then
410
463
  origin_git fetch -q --depth=1 origin "refs/tags/$t:refs/tags/$t" 2>/dev/null || true
411
464
  git rev-parse -q --verify "refs/tags/$t" >/dev/null 2>&1 || die "tag $t 不存在,所以 $p $ver 還沒有發版"
412
465
  fi
413
- log "${3}:$(unit_name "$p" "$3") ${ver},tag $t 即發布紀錄,由 mirror-units.sh 推到鏡像倉,這裡不上傳"
466
+ case "$3" in
467
+ binary) by="binary-units.sh 發到發布倉" ;;
468
+ image) by="image-units.sh 推到 ghcr" ;;
469
+ *) by="mirror-units.sh 推到鏡像倉" ;;
470
+ esac
471
+ log "${3}:$(unit_name "$p" "$3") ${ver},tag $t 即發布紀錄,由 ${by},這裡不上傳"
414
472
  summary "$3 \`$(unit_name "$p" "$3") $ver\`,tag \`$t\`(不上傳)"
415
473
  }
416
474
 
@@ -419,7 +477,11 @@ case "$1" in
419
477
  --registries)
420
478
  shift
421
479
  regs=()
422
- for p in "$@"; do r="$(target_of "${p%/}")"; regs+=("$r=true"); done
480
+ for p in "$@"; do
481
+ p="${p%/}"
482
+ [[ "$(unit_publish_to "$p")" != none ]] || continue # a unit that is not published needs no target
483
+ r="$(target_of "$p")"; regs+=("$r=true")
484
+ done
423
485
  printf '%s\n' "${regs[@]}" | sort -u
424
486
  exit 0 ;;
425
487
  --filter)
@@ -429,13 +491,16 @@ case "$1" in
429
491
  --report)
430
492
  shift
431
493
  declare -A reported=()
494
+ rc=0
432
495
  for p in "$@"; do
433
496
  p="${p%/}"
434
497
  [[ -z "${reported[$p]:-}" ]] || continue
435
498
  reported[$p]=1
436
- if publishable "$p"; then printf 'unit %s -> %s\n' "$p" "$(target_of "$p")"; else printf 'unit %s -> not-published\n' "$p"; fi
499
+ if publishable "$p"; then
500
+ if t="$(target_of "$p")"; then printf 'unit %s -> %s\n' "$p" "$t"; else rc=1; fi
501
+ else printf 'unit %s -> not-published\n' "$p"; fi
437
502
  done
438
- exit 0 ;;
503
+ exit "$rc" ;;
439
504
  --mirrors)
440
505
  shift
441
506
  names=()
@@ -471,6 +536,10 @@ fi
471
536
  for phase in "${phases[@]}"; do
472
537
  for p in "$@"; do
473
538
  p="${p%/}"
539
+ if [[ "$(unit_publish_to "$p")" == none ]]; then
540
+ [[ "$phase" == pack ]] && info "${p}:政策檔的 publish.to 是 none,不發布" >&2
541
+ continue
542
+ fi
474
543
  ver="$(manifest_version "$p")"
475
544
  [[ -n "$ver" ]] || die "$p 不在 $MANIFEST"
476
545
  vs="$(version_stage "$ver")" || die "$p 的版號 $ver 不是 X.Y.Z 或 X.Y.Z-<alpha|beta|rc>.N,無法決定發布階段"
@@ -497,7 +566,7 @@ for phase in "${phases[@]}"; do
497
566
  printf '%s %s\n' "$p" "$ver" >> "$PUBLISH_OUT/packed.txt"
498
567
  else
499
568
  # an upload job that got a different artifact than the build job made stops instead of publishing it
500
- case "$target" in go | swift) ;; *)
569
+ case "$target" in go | swift | binary | image) ;; *)
501
570
  grep -qxF "$p $ver" "$PUBLISH_OUT/packed.txt" 2>/dev/null \
502
571
  || die "$p $ver 不在 $PUBLISH_OUT/packed.txt:建置階段沒有打包這個單元,或下載的 artifact 不是這次建置的" ;;
503
572
  esac
@@ -505,7 +574,7 @@ for phase in "${phases[@]}"; do
505
574
  npm | github-packages) publish_npm "$p" "$ver" "$stage" "$target" ;;
506
575
  pypi) publish_pypi "$p" "$ver" ;;
507
576
  crates) publish_crates "$p" "$ver" ;;
508
- go | swift) publish_tag "$p" "$ver" "$target" ;;
577
+ go | swift | binary | image) publish_tag "$p" "$ver" "$target" ;;
509
578
  esac
510
579
  fi
511
580
  done
@@ -146,7 +146,8 @@ jobs:
146
146
  done
147
147
 
148
148
  # @if publish
149
- # packages/*、libs/*、facts 與 docs 匯總套件 → 每個階段都發布(publish.yml)。哪些路徑真的是要發布的單元,
149
+ # packages/*、libs/*、facts 與 docs 匯總套件,以及政策檔 publish.to 是 binary 或 image 的單元(不論它在哪個目錄,
150
+ # 例如 services/)→ 每個階段都發布(publish.yml)。哪些路徑真的是要發布的單元,
150
151
  # 由 publish.yml 的 plan job 判斷(libs/* 有的不發布)。alpha、beta、rc 掛在階段名的 npm dist-tag,GA 才進
151
152
  # latest,所以使用者要明確指定才會裝到預發布版。同一條發布線的單元可以停在不同階段(docs/versioning.md),
152
153
  # 所以 dispatch-publish.sh 依每個單元自己的版號算階段,每個階段各派送一次 publish.yml,publish.yml 再逐單元核對
@@ -105,6 +105,45 @@
105
105
  "propertyNames": { "$ref": "#/definitions/stage" },
106
106
  "additionalProperties": { "type": "string", "minLength": 1 }
107
107
  },
108
+ "publish": {
109
+ "description": "Where the unit is published. Left out, the target follows the files of the unit.",
110
+ "type": "object",
111
+ "required": ["to"],
112
+ "additionalProperties": false,
113
+ "properties": {
114
+ "to": {
115
+ "description": "binary: a compiled executable released to the public release repository. image: a container image pushed to ghcr. none: the unit is not published.",
116
+ "enum": ["binary", "image", "none"]
117
+ },
118
+ "command": {
119
+ "description": "The name of the executable and of its formula. Required for binary.",
120
+ "type": "string",
121
+ "pattern": "^[a-z0-9][a-z0-9._-]*$"
122
+ },
123
+ "description": {
124
+ "description": "One line that says what the executable does, written into the formula. Required for binary.",
125
+ "type": "string",
126
+ "minLength": 1
127
+ },
128
+ "platforms": {
129
+ "description": "The platforms to build for, as <os>/<arch>, for example linux/amd64. Required for binary and image.",
130
+ "type": "array",
131
+ "minItems": 1,
132
+ "uniqueItems": true,
133
+ "items": { "type": "string", "pattern": "^[a-z0-9]+/[a-z0-9]+$" }
134
+ }
135
+ },
136
+ "allOf": [
137
+ {
138
+ "if": { "required": ["to"], "properties": { "to": { "const": "binary" } } },
139
+ "then": { "required": ["command", "description", "platforms"] }
140
+ },
141
+ {
142
+ "if": { "required": ["to"], "properties": { "to": { "const": "image" } } },
143
+ "then": { "required": ["platforms"] }
144
+ }
145
+ ]
146
+ },
108
147
  "unit": {
109
148
  "type": "object",
110
149
  "required": ["kind", "stages"],
@@ -139,6 +178,7 @@
139
178
  "description": "Destination of each stage for this unit, overriding the repository targets. Store channels for mobile and desktop.",
140
179
  "$ref": "#/definitions/targets"
141
180
  },
181
+ "publish": { "$ref": "#/definitions/publish" },
142
182
  "dependsOn": {
143
183
  "description": "Same-repository units this unit depends on that its package manifest cannot express, e.g. a binding across languages.",
144
184
  "type": "array",
@@ -154,6 +194,16 @@
154
194
  {
155
195
  "if": { "properties": { "kind": { "enum": ["package", "sdk"] } } },
156
196
  "then": { "not": { "required": ["targets"] } }
197
+ },
198
+ {
199
+ "description": "A unit that publishes executables is a package.",
200
+ "if": { "required": ["publish"], "properties": { "publish": { "required": ["to"], "properties": { "to": { "const": "binary" } } } } },
201
+ "then": { "properties": { "kind": { "const": "package" } } }
202
+ },
203
+ {
204
+ "description": "A unit that publishes a container image is a service or a backend.",
205
+ "if": { "required": ["publish"], "properties": { "publish": { "required": ["to"], "properties": { "to": { "const": "image" } } } } },
206
+ "then": { "properties": { "kind": { "enum": ["service", "backend"] } } }
157
207
  }
158
208
  ]
159
209
  }
@@ -14,12 +14,16 @@
14
14
  # config of its own to compare with, so this check lives here, on the release side. On a release branch
15
15
  # whose config was cut down to the train, a component the config no longer has is skipped.
16
16
  # - a unit that deploys (service, backend, web) finds a target for each stage it walks, in its own
17
- # targets or the repository targets
17
+ # targets or the repository targets. A unit whose publish.to is image is left out: it publishes a container
18
+ # image and is not deployed by its tag, so it needs no targets
18
19
  # - a unit that deploys walks at least one pre-release stage when its own targets or the repository
19
20
  # targets have one. Only after deploying does it show how it works with the other services, and a
20
21
  # GA-only unit goes to prod as soon as the train enters its first stage
21
22
  # - a unit that ships to store channels (mobile, desktop) names a channel for each stage it walks
22
23
  # - every dependsOn names a unit the policy lists
24
+ # - a unit whose publish.to is binary has the kind package, names command, description and platforms, and has a go.mod in
25
+ # its directory (this version supports Go only); a unit whose publish.to is image has the kind service or backend and
26
+ # names platforms
23
27
  # A <product>-hub has no units and no release-please-config.json, so there is nothing to compare and it passes.
24
28
  # A repository of the organization layer (layer: organization) belongs to no product, so product.members is not read.
25
29
  # Without units it passes the same way; with units it is checked as a single repository, like any other.
@@ -93,9 +97,10 @@ fi
93
97
  while IFS= read -r line; do [[ -n "$line" ]] && problems+=("$line"); done < <(jq -r '
94
98
  . as $p
95
99
  | (.units // {}) | to_entries[] | .key as $u | .value as $v
100
+ | (($v.kind | IN("service", "backend", "web")) and (($v.publish.to // "") != "image")) as $deploys
96
101
  | (
97
102
  ($v.stages // [])[] as $s
98
- | if ($v.kind | IN("service", "backend", "web")) then
103
+ | if $deploys then
99
104
  select((($v.targets // {})[$s] // ($p.targets // {})[$s]) == null)
100
105
  | "\($u)(\($v.kind))的階段 \($s) 找不到去處:units.\($u).targets 與 targets 都沒有 \($s)"
101
106
  elif ($v.kind | IN("mobile", "desktop")) then
@@ -104,7 +109,7 @@ while IFS= read -r line; do [[ -n "$line" ]] && problems+=("$line"); done < <(jq
104
109
  else empty end
105
110
  ),
106
111
  (
107
- select($v.kind | IN("service", "backend", "web"))
112
+ select($deploys)
108
113
  | ([($v.targets // {}), ($p.targets // {})] | map(keys[]) | map(select(IN("alpha", "beta", "rc"))) | unique) as $pre
109
114
  | select(($pre | length) > 0 and ([($v.stages // [])[] | select(. != "ga")] | length) == 0)
110
115
  | "\($u)(\($v.kind))只發 GA,但 targets 有預發布階段 \($pre | join("、")):部署型單元要至少走一個預發布階段"
@@ -113,9 +118,29 @@ while IFS= read -r line; do [[ -n "$line" ]] && problems+=("$line"); done < <(jq
113
118
  ($v.dependsOn // [])[] as $d
114
119
  | select(($p.units | has($d)) | not)
115
120
  | "\($u) 的 dependsOn 指到不存在的單元:\($d)"
121
+ ),
122
+ (
123
+ select(($v.publish.to // "") == "binary")
124
+ | (select($v.kind != "package") | "\($u) 的 publish.to 是 binary,kind 要是 package(現在是 \($v.kind))"),
125
+ (([ "command", "description", "platforms" ] - ($v.publish | keys)) | select(length > 0) | "\($u) 的 publish.to 是 binary,publish 要寫 \(join("、"))")
126
+ ),
127
+ (
128
+ select(($v.publish.to // "") == "image")
129
+ | (select($v.kind | IN("service", "backend") | not) | "\($u) 的 publish.to 是 image,kind 要是 service 或 backend(現在是 \($v.kind))"),
130
+ (select(($v.publish | has("platforms")) | not) | "\($u) 的 publish.to 是 image,publish 要寫 platforms")
116
131
  )
117
132
  ' "$policy")
118
133
 
134
+ # 3. a unit that publishes executables is a Go unit: this version has no other way to build one
135
+ if [[ -f "$config" ]]; then
136
+ while IFS= read -r u; do
137
+ [[ -n "$u" ]] || continue
138
+ path="$(jq -r --arg c "$u" '.packages | to_entries[] | select((.value.component // (.key | split("/")[1])) == $c) | .key' "$config" | head -n 1)"
139
+ [[ -n "$path" ]] || continue
140
+ [[ -f "$REPO_ROOT/$path/go.mod" ]] || problems+=("${u} 的 publish.to 是 binary,但 ${path}/ 沒有 go.mod:這一版的執行檔發布只支援 Go")
141
+ done < <(jq -r '(.units // {}) | to_entries[] | select(.value.publish.to == "binary") | .key' "$policy")
142
+ fi
143
+
119
144
  if (( ${#problems[@]} )); then
120
145
  printf '%s\n' "✗ 政策檔與倉庫不一致(${policy}):" >&2
121
146
  for p in "${problems[@]}"; do printf '%s\n' " - $p" >&2; done
@@ -64,6 +64,22 @@ stages_override_policy() {
64
64
 
65
65
  policy_unit_kind() { policy_require; jq -r --arg u "$1" '.units[$u].kind // empty' "$(policy_file)"; }
66
66
 
67
+ # policy_unit_publish_to <unit> → binary | image | none, the publish.to the policy gives the unit; prints nothing when the unit
68
+ # has none, when the policy does not list it, and when there is no policy file at all (a repository that only publishes
69
+ # has the policy of its release role, but a script that only reads where to publish must not stop on a missing file).
70
+ # A unit without it follows the files of its directory (publish-units.sh, blueprint-shared.publishing.target-selection).
71
+ policy_unit_publish_to() {
72
+ [[ -f "$(policy_file)" ]] || return 0
73
+ jq -r --arg u "$1" '.units[$u].publish.to // empty' "$(policy_file)"
74
+ }
75
+
76
+ # policy_unit_publish_field <unit> <command|description|platforms> → that field of publish in the policy, an array one element per line;
77
+ # prints nothing when the unit has no such field.
78
+ policy_unit_publish_field() {
79
+ [[ -f "$(policy_file)" ]] || return 0
80
+ jq -r --arg u "$1" --arg f "$2" '.units[$u].publish[$f] // empty | if type == "array" then .[] else . end' "$(policy_file)"
81
+ }
82
+
67
83
  # policy_unit_independent <unit> → succeeds when the policy marks the unit "independent": true, which means the
68
84
  # unit releases on its own and does not follow the product line's cadence. A unit the policy does not list, or
69
85
  # one without the field, is not independent.
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env bash
2
2
  # Dispatch publish.yml for the packages a release created, one dispatch per stage of the versions.
3
- # release-please.yml calls it after each release. The units of one release line may stand at different
3
+ # release-please.yml calls it after each release. It dispatches packages/*, libs/*, the facts and docs aggregate packages, and the
4
+ # units whose publish.to in release-policy.json is binary or image, wherever their directory is. The units of one release line may stand at different
4
5
  # stages (docs/versioning.md): a unit that only walks ga is at its final version while the line is in
5
6
  # alpha. publish-units.sh stops when a version does not belong to the stage it is given, so the units are
6
7
  # grouped by the stage of their own version in .release-please-manifest.json and each group is dispatched
@@ -16,7 +17,16 @@ cd "$REPO_ROOT"
16
17
  paths="${PATHS:?PATHS is required (JSON array of released paths)}"
17
18
  ref="${GITHUB_REF_NAME:?GITHUB_REF_NAME is required}"
18
19
  pkgs="$(jq -r '.[]' <<<"$paths" | while IFS= read -r p; do
19
- case "$p" in packages/*|libs/*) printf '%s\n' "$p" ;; *) ! is_aggregate_path "$p" || printf '%s\n' "$p" ;; esac
20
+ case "$p" in
21
+ packages/*|libs/*) printf '%s\n' "$p" ;;
22
+ *)
23
+ # a unit whose publish.to is binary or image is dispatched wherever its directory is, services/ for example; a unit without
24
+ # publish.to or with none is not, so a service that is only deployed is left out
25
+ case "$(policy_unit_publish_to "$(unit_component "$p")")" in
26
+ binary|image) printf '%s\n' "$p" ;;
27
+ *) ! is_aggregate_path "$p" || printf '%s\n' "$p" ;;
28
+ esac ;;
29
+ esac
20
30
  done | jq -Rsc 'split("\n") | map(select(length > 0))')"
21
31
  if [[ "$pkgs" == "[]" ]]; then echo "no packages in this release"; exit 0; fi
22
32
  pairs=""