From cec72605e37ce91e795b23b0ad49055c59da394e Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 10 Apr 2026 22:11:34 +0800 Subject: [PATCH] fix(ci): harden ota publish workflow --- .github/scripts/trigger-ota-sync.mjs | 63 ++++++++++++++------ .github/scripts/trigger-ota-sync.test.ts | 64 +++++++++++++++++++++ .github/workflows/publish-ota.yml | 15 ++++- .github/workflows/tag.yml | 73 +++++++++++++++++++++--- 4 files changed, 189 insertions(+), 26 deletions(-) diff --git a/.github/scripts/trigger-ota-sync.mjs b/.github/scripts/trigger-ota-sync.mjs index e7cbae8cd..70aa6687a 100644 --- a/.github/scripts/trigger-ota-sync.mjs +++ b/.github/scripts/trigger-ota-sync.mjs @@ -7,13 +7,21 @@ import { pathToFileURL } from "node:url" * baseUrl: string * token: string * headerName: string + * timeoutMs?: number * }} TriggerOtaSyncOptions */ +const DEFAULT_TIMEOUT_MS = 10_000 + /** * @param {TriggerOtaSyncOptions} options */ -export async function triggerOtaSync({ baseUrl, token, headerName }) { +export async function triggerOtaSync({ + baseUrl, + token, + headerName, + timeoutMs = DEFAULT_TIMEOUT_MS, +}) { if (!baseUrl) { throw new TypeError("OTA base URL is required") } @@ -26,24 +34,45 @@ export async function triggerOtaSync({ baseUrl, token, headerName }) { throw new TypeError("OTA sync header name is required") } - const normalizedBaseUrl = baseUrl.replace(/\/+$/, "") - const response = await fetch(`${normalizedBaseUrl}/internal/sync`, { - method: "POST", - headers: { - [headerName]: token, - }, - }) - - if (!response.ok) { - const responseText = await response.text() - const detail = responseText ? `: ${responseText}` : "" - - throw new Error( - `Failed to trigger OTA sync (${response.status} ${response.statusText})${detail}`, - ) + if (!Number.isInteger(timeoutMs) || timeoutMs <= 0) { + throw new TypeError("OTA sync timeout must be a positive integer") } - return response + const normalizedBaseUrl = baseUrl.replace(/\/+$/, "") + const controller = new AbortController() + const timeoutError = new Error(`Timed out while triggering OTA sync after ${timeoutMs}ms`) + const timeoutId = setTimeout(() => { + controller.abort(timeoutError) + }, timeoutMs) + + try { + const response = await fetch(`${normalizedBaseUrl}/internal/sync`, { + method: "POST", + headers: { + [headerName]: token, + }, + signal: controller.signal, + }) + + if (!response.ok) { + const responseText = await response.text() + const detail = responseText ? `: ${responseText}` : "" + + throw new Error( + `Failed to trigger OTA sync (${response.status} ${response.statusText})${detail}`, + ) + } + + return response + } catch (error) { + if (controller.signal.aborted && controller.signal.reason === timeoutError) { + throw timeoutError + } + + throw error + } finally { + clearTimeout(timeoutId) + } } async function main() { diff --git a/.github/scripts/trigger-ota-sync.test.ts b/.github/scripts/trigger-ota-sync.test.ts index daf4e820c..21fedbd7d 100644 --- a/.github/scripts/trigger-ota-sync.test.ts +++ b/.github/scripts/trigger-ota-sync.test.ts @@ -69,4 +69,68 @@ describe("triggerOtaSync", () => { }, ]) }) + + it("throws when the sync endpoint responds with a non-OK status", async () => { + const headerName = "x-ota-sync-token" + const token = "sync-token-value" + + const server = createServer((_request, response) => { + response.writeHead(503, { "content-type": "text/plain" }) + response.end("service unavailable") + }) + + activeServers.add(server) + + await new Promise((resolve, reject) => { + server.once("error", reject) + server.listen(0, "127.0.0.1", resolve) + }) + + const address = server.address() + if (!address || typeof address === "string") { + throw new TypeError("Expected the test server to bind to an ephemeral TCP port") + } + + const { triggerOtaSync } = await import("./trigger-ota-sync.mjs") + + await expect( + triggerOtaSync({ + baseUrl: `http://127.0.0.1:${address.port}`, + token, + headerName, + }), + ).rejects.toThrow("Failed to trigger OTA sync (503 Service Unavailable): service unavailable") + }) + + it("aborts the request when the timeout is exceeded", async () => { + const headerName = "x-ota-sync-token" + const token = "sync-token-value" + + const server = createServer((_request, _response) => { + // Intentionally never respond so the client must rely on the timeout. + }) + + activeServers.add(server) + + await new Promise((resolve, reject) => { + server.once("error", reject) + server.listen(0, "127.0.0.1", resolve) + }) + + const address = server.address() + if (!address || typeof address === "string") { + throw new TypeError("Expected the test server to bind to an ephemeral TCP port") + } + + const { triggerOtaSync } = await import("./trigger-ota-sync.mjs") + + await expect( + triggerOtaSync({ + baseUrl: `http://127.0.0.1:${address.port}`, + token, + headerName, + timeoutMs: 25, + }), + ).rejects.toThrow("Timed out while triggering OTA sync after 25ms") + }) }) diff --git a/.github/workflows/publish-ota.yml b/.github/workflows/publish-ota.yml index e8287d498..939595a9b 100644 --- a/.github/workflows/publish-ota.yml +++ b/.github/workflows/publish-ota.yml @@ -40,7 +40,6 @@ jobs: OTA_RUNTIME_VERSION: ${{ inputs.runtime_version }} OTA_CHANNEL: ${{ inputs.channel }} OTA_GIT_TAG: mobile/v${{ inputs.release_version }} - OTA_GIT_COMMIT: ${{ github.sha }} steps: - name: Checkout repository @@ -48,6 +47,18 @@ jobs: with: fetch-depth: 0 + - name: Resolve target release tag + run: | + git fetch --tags --force + + if ! git rev-parse --verify "refs/tags/${OTA_GIT_TAG}" >/dev/null 2>&1; then + echo "::error::Required release tag ${OTA_GIT_TAG} does not exist. Create it before publishing OTA assets." + exit 1 + fi + + git checkout --detach "refs/tags/${OTA_GIT_TAG}" + echo "Checked out ${OTA_GIT_TAG} at $(git rev-list -n 1 "refs/tags/${OTA_GIT_TAG}")" + - name: Setup pnpm uses: pnpm/action-setup@v5 @@ -83,7 +94,7 @@ jobs: gh release upload "$tag_name" "${ota_files[@]}" --clobber else gh release create "$tag_name" "${ota_files[@]}" \ - --target "$GITHUB_SHA" \ + --verify-tag \ --title "$release_title" \ --notes "OTA release assets for ${OTA_RELEASE_KIND} releases on the ${OTA_CHANNEL} channel." fi diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml index d16a93297..bafc0b5d4 100644 --- a/.github/workflows/tag.yml +++ b/.github/workflows/tag.yml @@ -80,17 +80,76 @@ jobs: COMMIT_MESSAGE: ${{ github.event.head_commit.message }} RELEASE_VERSION: ${{ needs.create_tag.outputs.version }} run: | - release_kind="$(printf '%s\n' "$COMMIT_MESSAGE" | sed -nE 's/^OTA_RELEASE_KIND=(ota|store)$/\1/p' | tail -n 1)" - runtime_version="$(printf '%s\n' "$COMMIT_MESSAGE" | sed -nE 's/^OTA_RUNTIME_VERSION=(.+)$/\1/p' | tail -n 1)" - channel="$(printf '%s\n' "$COMMIT_MESSAGE" | sed -nE 's/^OTA_CHANNEL=(production|preview)$/\1/p' | tail -n 1)" + ota_trailers="$(printf '%s\n' "$COMMIT_MESSAGE" | sed -nE '/^OTA_/p')" normalized_release_version="${RELEASE_VERSION#v}" - if [ -n "$release_kind" ] && [ -n "$runtime_version" ] && [ -n "$channel" ]; then - echo "enabled=true" >> "$GITHUB_OUTPUT" - else + if [ -z "$ota_trailers" ]; then echo "enabled=false" >> "$GITHUB_OUTPUT" + echo "release_kind=" >> "$GITHUB_OUTPUT" + echo "runtime_version=" >> "$GITHUB_OUTPUT" + echo "channel=" >> "$GITHUB_OUTPUT" + echo "release_version=${normalized_release_version}" >> "$GITHUB_OUTPUT" + exit 0 fi + invalid_trailers="$(printf '%s\n' "$ota_trailers" | sed -nE '/^OTA_(RELEASE_KIND|RUNTIME_VERSION|CHANNEL)=/!p')" + if [ -n "$invalid_trailers" ]; then + echo "::error::Unsupported OTA trailer lines detected:" + printf '%s\n' "$invalid_trailers" + exit 1 + fi + + release_kind_values="$(printf '%s\n' "$COMMIT_MESSAGE" | sed -nE 's/^OTA_RELEASE_KIND=(.*)$/\1/p')" + runtime_version_values="$(printf '%s\n' "$COMMIT_MESSAGE" | sed -nE 's/^OTA_RUNTIME_VERSION=(.*)$/\1/p')" + channel_values="$(printf '%s\n' "$COMMIT_MESSAGE" | sed -nE 's/^OTA_CHANNEL=(.*)$/\1/p')" + + count_values() { + local values="$1" + + if [ -z "$values" ]; then + echo 0 + return + fi + + printf '%s\n' "$values" | sed '/^$/d' | wc -l | tr -d ' ' + } + + release_kind_count="$(count_values "$release_kind_values")" + runtime_version_count="$(count_values "$runtime_version_values")" + channel_count="$(count_values "$channel_values")" + + if [ "$release_kind_count" -ne 1 ] || [ "$runtime_version_count" -ne 1 ] || [ "$channel_count" -ne 1 ]; then + echo "::error::OTA trailers are incomplete or duplicated. Expected exactly one OTA_RELEASE_KIND, OTA_RUNTIME_VERSION, and OTA_CHANNEL trailer when any OTA_ trailer is present." + printf '%s\n' "$ota_trailers" + exit 1 + fi + + release_kind="$(printf '%s\n' "$release_kind_values" | tail -n 1)" + runtime_version="$(printf '%s\n' "$runtime_version_values" | tail -n 1)" + channel="$(printf '%s\n' "$channel_values" | tail -n 1)" + + case "$release_kind" in + ota|store) ;; + *) + echo "::error::OTA_RELEASE_KIND must be either ota or store." + exit 1 + ;; + esac + + if [ -z "$runtime_version" ]; then + echo "::error::OTA_RUNTIME_VERSION must not be empty." + exit 1 + fi + + case "$channel" in + production|preview) ;; + *) + echo "::error::OTA_CHANNEL must be either production or preview." + exit 1 + ;; + esac + + echo "enabled=true" >> "$GITHUB_OUTPUT" echo "release_kind=${release_kind}" >> "$GITHUB_OUTPUT" echo "runtime_version=${runtime_version}" >> "$GITHUB_OUTPUT" echo "channel=${channel}" >> "$GITHUB_OUTPUT" @@ -233,7 +292,7 @@ jobs: owner: context.repo.owner, repo: context.repo.repo, workflow_id: 'publish-ota.yml', - ref: 'mobile-main', + ref: '${{ needs.create_tag.outputs.tag_version }}', inputs: { release_version: '${{ steps.ota_release.outputs.release_version }}', release_kind: '${{ steps.ota_release.outputs.release_kind }}',