ci(ota): publish releases and trigger sync

This commit is contained in:
DIYgod
2026-04-10 22:00:59 +08:00
parent 49d02c2a00
commit 340fb54056
4 changed files with 277 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
#!/usr/bin/env node
import { pathToFileURL } from "node:url"
/**
* @typedef {{
* baseUrl: string
* token: string
* headerName: string
* }} TriggerOtaSyncOptions
*/
/**
* @param {TriggerOtaSyncOptions} options
*/
export async function triggerOtaSync({ baseUrl, token, headerName }) {
if (!baseUrl) {
throw new TypeError("OTA base URL is required")
}
if (!token) {
throw new TypeError("OTA sync token is required")
}
if (!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}`,
)
}
return response
}
async function main() {
try {
await triggerOtaSync({
baseUrl: process.env.OTA_BASE_URL ?? "",
token: process.env.OTA_SYNC_TOKEN ?? "",
headerName: process.env.OTA_SYNC_TOKEN_HEADER ?? "",
})
console.info("Triggered OTA sync successfully")
} catch (error) {
console.error(error instanceof Error ? error.message : String(error))
process.exitCode = 1
}
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
await main()
}
+72
View File
@@ -0,0 +1,72 @@
import { createServer } from "node:http"
import { afterEach, describe, expect, it } from "vitest"
const activeServers = new Set<ReturnType<typeof createServer>>()
afterEach(async () => {
await Promise.all(
Array.from(
activeServers,
(server) =>
new Promise<void>((resolve, reject) => {
server.close((error) => {
if (error) {
reject(error)
return
}
resolve()
})
}),
),
)
activeServers.clear()
})
describe("triggerOtaSync", () => {
it("POSTs to /internal/sync with the configured auth header", async () => {
const requests: Array<{ method?: string; url?: string; headerValue?: string }> = []
const headerName = "x-ota-sync-token"
const token = "sync-token-value"
const server = createServer((request, response) => {
requests.push({
method: request.method,
url: request.url,
headerValue: request.headers[headerName],
})
response.writeHead(204)
response.end()
})
activeServers.add(server)
await new Promise<void>((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 triggerOtaSync({
baseUrl: `http://127.0.0.1:${address.port}`,
token,
headerName,
})
expect(requests).toEqual([
{
method: "POST",
url: "/internal/sync",
headerValue: token,
},
])
})
})