返回 CodeWhale
sync-cnb.yml
根目录 / .github / workflows / sync-cnb.yml
1 name: Sync to CNB
2
3 # Mirror commits and release tags to cnb.cool/codewhale.net/codewhale
4 # so users behind GitHub-blocking networks can fetch the source and tagged
5 # releases from the Tencent-hosted mirror.
6 #
7 # Main pushes mirror to CNB unless the repository variable CNB_MIRROR is
8 # "disabled"; release tags arrive through release.yml or, for a release
9 # published outside it, the release event; a half-hourly schedule re-mirrors
10 # main and the latest release tag; manual dispatch is the fallback.
11 #
12 # Why the rewrite (v0.8.31):
13 # The previous implementation used the opaque tencentcom/git-sync Docker
14 # action, which discovered every local ref via fetch-depth: 0 and tried
15 # to push them all — including dependabot/* branches that GitHub had
16 # locally. Those follow-on pushes ran without the configured credential
17 # helper in scope and failed with
18 # `fatal: could not read Username for 'https://cnb.cool'`
19 # No concurrency block meant the main-push and tag-push workflow runs
20 # that auto-tag.yml fires within seconds of each other raced. About
21 # half of recent runs failed for those two reasons combined.
22
23 on:
24 push:
25 branches: [main]
26 release:
27 types: [published]
28 schedule:
29 - cron: "17,47 * * * *"
30 workflow_dispatch: {}
31 workflow_call:
32 inputs:
33 release_tag:
34 required: true
35 type: string
36 source_sha:
37 required: true
38 type: string
39 secrets:
40 CNB_GIT_TOKEN:
41 required: true
42
43 # Serialize runs per ref. One global group was wrong: with
44 # cancel-in-progress: false GitHub still keeps only ONE pending run per group
45 # and cancels it when a newer one queues, so a release tag sync waiting behind
46 # a main push was dropped by the next branch push. Keyed by ref, each tag gets
47 # its own group (never superseded), and a pending branch sync is only ever
48 # replaced by a newer push of the same branch, whose force-push supersedes it.
49 # The main and tag pushes from auto-tag.yml write different CNB refs, so
50 # running them concurrently cannot conflict.
51 concurrency:
52 group: cnb-sync-${{ github.ref }}
53 cancel-in-progress: false
54
55 permissions:
56 contents: read
57
58 jobs:
59 sync:
60 if: >-
61 github.repository_owner == 'codewhale-hq' &&
62 !(github.event_name == 'release' && github.event.release.prerelease) &&
63 (!contains(fromJSON('["push","schedule","release"]'), github.event_name) || vars.CNB_MIRROR != 'disabled')
64 runs-on: ubuntu-24.04
65 timeout-minutes: 15
66 steps:
67 - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
68 with:
69 fetch-depth: 0
70 ref: ${{ inputs.source_sha || github.sha }}
71
72 - name: Push triggering ref to CNB
73 env:
74 CNB_TOKEN: ${{ secrets.CNB_GIT_TOKEN }}
75 GH_TOKEN: ${{ github.token }}
76 RELEASE_TAG: ${{ inputs.release_tag }}
77 SOURCE_SHA: ${{ inputs.source_sha || github.sha }}
78 shell: bash
79 run: |
80 set -euo pipefail
81
82 if [ -z "${CNB_TOKEN:-}" ]; then
83 echo "::error::CNB_GIT_TOKEN secret is not set; cannot push to CNB." >&2
84 exit 1
85 fi
86
87 # URL-encode any '%' in the token so basic-auth doesn't break on
88 # special characters. CNB tokens are typically alphanumeric so
89 # this is belt-and-suspenders.
90 ENCODED_TOKEN="$(printf '%s' "${CNB_TOKEN}" | python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.stdin.read(), safe=""))')"
91 REMOTE_URL="https://cnb:${ENCODED_TOKEN}@cnb.cool/codewhale.net/codewhale.git"
92 # Use a masked alias so the token never appears in log lines.
93 git remote add cnb "${REMOTE_URL}"
94
95 if [[ -n "${RELEASE_TAG}" || "${GITHUB_REF}" == refs/tags/* ]]; then
96 TAG="${RELEASE_TAG:-${GITHUB_REF#refs/tags/}}"
97 # The tag starts CNB's public release pipeline. Require the
98 # canonical release even for a manual recovery dispatch.
99 if ! [[ "${TAG}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] ||
100 [[ "$(git rev-parse 'HEAD^{commit}')" != "${SOURCE_SHA}" ]]; then
101 echo "::error::CNB release source does not match the frozen tag input." >&2
102 exit 1
103 fi
104 ./scripts/release/verify-remote-tag.sh \
105 "https://github.com/${GITHUB_REPOSITORY}.git" "${TAG}" "${SOURCE_SHA}"
106 node scripts/release/verify-release-inventory.js --manifest "${GITHUB_REPOSITORY}" "${TAG}"
107 scripts/release/mirror-push.sh cnb "tag ${TAG}" "refs/tags/${TAG}:refs/tags/${TAG}"
108 elif [[ "${GITHUB_REF}" == refs/heads/main ]]; then
109 # Plain --force. The CNB mirror is one-way by design —
110 # nothing else pushes to it, so there's no contributor work
111 # to protect against. `--force-with-lease` would be safer
112 # in a multi-writer scenario, but in our setup the lease
113 # check requires `refs/remotes/cnb/main` to exist in the
114 # runner's local clone, which it never does (we add `cnb`
115 # as a fresh remote in this step and don't fetch first).
116 # That made the lease check spuriously fail with
117 # `! [rejected] HEAD -> main (stale info)` even when CNB
118 # was actually behind GitHub.
119 scripts/release/mirror-push.sh cnb "main" HEAD:refs/heads/main --force
120 else
121 # First-party fix/rebrand/release branches are first-class CNB
122 # sources for heavy Linux CI, release preflight, and
123 # Lighthouse/Feishu bootstrap.
124 # Mirror the triggering branch exactly so the CNB clone path stays
125 # useful before the branch has merged to main or become a release
126 # tag.
127 BRANCH="${GITHUB_REF#refs/heads/}"
128 scripts/release/mirror-push.sh cnb "branch ${BRANCH}" "HEAD:refs/heads/${BRANCH}" --force
129 fi
130
131 - name: Mirror the latest release to CNB
132 if: github.event_name == 'schedule'
133 env:
134 GH_TOKEN: ${{ github.token }}
135 shell: bash
136 run: |
137 set -euo pipefail
138 TAG="$(gh api "repos/${GITHUB_REPOSITORY}/releases/latest" --jq .tag_name)"
139 if [[ -z "$(git ls-remote --tags cnb "refs/tags/${TAG}")" ]]; then
140 node scripts/release/verify-release-inventory.js --manifest "${GITHUB_REPOSITORY}" "${TAG}"
141 scripts/release/mirror-push.sh cnb "tag ${TAG}" "refs/tags/${TAG}:refs/tags/${TAG}"
142 echo "::notice::Pushed ${TAG} to CNB; its CNB release build starts now."
143 exit 0
144 fi
145 ./scripts/release/verify-remote-tag.sh cnb "${TAG}" "$(git rev-parse "refs/tags/${TAG}^{commit}")"
146 if ! curl -fsSL --retry 3 -o /dev/null \
147 "https://cnb.cool/codewhale.net/codewhale/-/releases/download/${TAG}/codewhale-artifacts-sha256.txt"; then
148 echo "::warning::CNB release ${TAG} has no codewhale-artifacts-sha256.txt yet; recover its CNB tag build if it is not still running (docs/CNB_MIRROR.md)." >&2
149 if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
150 printf 'Warning: CNB release %s tag is mirrored, but codewhale-artifacts-sha256.txt is not reachable yet (docs/CNB_MIRROR.md).\n' "${TAG}" >> "${GITHUB_STEP_SUMMARY}"
151 fi
152 fi
153
153 lines YAML