feat(release): publish package binaries through Cloudflare

Move files deployments to Wrangler, make CLI and desktop own their publishing destinations, add direct-download update metadata and desktop feeds, and refresh installation docs.
This commit is contained in:
Dax Raad
2026-09-07 19:17:25 -04:00
Unverified
parent 0ab661a9cc
commit d1d1c6f890
24 changed files with 528 additions and 419 deletions
+2 -21
View File
@@ -3,7 +3,6 @@
import { Script } from "@opencode/script"
import { $ } from "bun"
import { fileURLToPath } from "url"
import { UpdateArtifact } from "./update-artifact"
console.log("=== publishing ===\n")
@@ -80,11 +79,6 @@ await $`bun ./packages/sdk/script/publish.ts`
console.log("\n=== ui ===\n")
await $`bun ./packages/ui/script/publish.ts`
if (Script.release) {
await $`bun ./packages/desktop/scripts/finalize-latest-json.ts`
await $`bun ./packages/desktop/scripts/finalize-latest-yml.ts`
}
if (Script.release && !Script.preview) {
await $`git commit -am "release: ${tag}"`
await $`git tag -d ${tag}`.nothrow()
@@ -99,19 +93,6 @@ if (Script.release && !Script.preview) {
}
if (Script.release) {
await $`gh release edit ${tag} --draft=false --repo ${process.env.GH_REPO}`
const repo = process.env.GH_REPO
if (!repo) throw new Error("GH_REPO is required")
await UpdateArtifact.publish({
channel: Script.channel,
name: "desktop",
distribution: "github",
version: Script.version,
metadata: await UpdateArtifact.desktopMetadata(Script.version, repo),
})
}
if ((Script.channel === "beta" || Script.channel === "latest") && Script.release) {
console.log("\n=== AUR ===\n")
await $`bun ./packages/cli/script/publish-aur.ts`
console.log("\n=== desktop ===\n")
await $`bun ./packages/desktop/scripts/publish.ts`
}
+64 -63
View File
@@ -1,3 +1,7 @@
import { createHash } from "node:crypto"
import { createReadStream } from "node:fs"
import path from "node:path"
type Artifact = {
channel: string
name: string
@@ -6,13 +10,6 @@ type Artifact = {
metadata: Record<string, unknown>
}
type DesktopFile = {
url: string
sha512: string
size: number
blockMapSize?: number
}
export namespace UpdateArtifact {
export async function publish(artifact: Artifact) {
if (process.env.GITHUB_ACTIONS !== "true") {
@@ -52,66 +49,70 @@ export namespace UpdateArtifact {
}
}
export async function desktopMetadata(version: string, repo: string) {
const directory = process.env.RUNNER_TEMP ?? "/tmp"
const entries = await Promise.all(
[
["desktop.yml", "latest.yml"],
["desktop-mac.yml", "latest-mac.yml"],
["desktop-linux.yml", "latest-linux.yml"],
["desktop-linux-arm64.yml", "latest-linux-arm64.yml"],
].map(async ([name, source]) => {
const file = Bun.file(`${directory}/${source}`)
if (!(await file.exists())) return
return [name, parseDesktop(await file.text(), version, repo)] as const
}),
)
const manifests = Object.fromEntries(entries.filter((entry) => entry !== undefined))
if (!Object.keys(manifests).length) throw new Error("No desktop update metadata found")
return { manifests }
}
}
function parseDesktop(content: string, version: string, repo: string) {
const lines = content.split("\n")
const found = lines
.find((line) => line.startsWith("version:"))
?.slice("version:".length)
.trim()
if (found !== version) throw new Error(`Desktop metadata version mismatch: expected ${version}, got ${found}`)
const releaseDate = lines
.find((line) => line.startsWith("releaseDate:"))
?.slice("releaseDate:".length)
.trim()
.replace(/^['"]|['"]$/g, "")
if (!releaseDate) throw new Error("Desktop metadata did not include a release date")
const files: DesktopFile[] = []
lines.forEach((line) => {
const value = line.trim()
if (value.startsWith("- url:")) {
const name = value.slice("- url:".length).trim()
files.push({
url: name.startsWith("http")
? name
: `https://github.com/${repo}/releases/download/v${version}/${encodeURIComponent(name)}`,
sha512: "",
size: 0,
})
return
export async function upload(input: { version: string; files: string[]; dryRun?: boolean }) {
const { PutObjectCommand, S3Client } = await import("@aws-sdk/client-s3")
const client = input.dryRun ? undefined : new S3Client(await credentials())
const prefix = `bin/${input.version}/`
const base = `https://opencode.ai/files/bin/${encodeURIComponent(input.version)}/`
try {
return Object.fromEntries(
await Promise.all(
input.files.map(async (filepath) => {
const file = Bun.file(filepath)
const name = path.basename(filepath)
if (!file.size) throw new Error(`Empty release file: ${filepath}`)
const hash = createHash("sha256")
for await (const chunk of file.stream()) hash.update(chunk)
const metadata = { url: `${base}${encodeURIComponent(name)}`, sha256: hash.digest("hex"), size: file.size }
console.log(`${input.dryRun ? "dry-run upload" : "upload"}: ${prefix}${name}`)
if (client) {
await client.send(
new PutObjectCommand({
Bucket: "opencode-production-files",
Key: `${prefix}${name}`,
Body: createReadStream(filepath),
ContentLength: file.size,
ContentType: name.endsWith(".tar.gz") ? "application/gzip" : file.type || "application/octet-stream",
ContentDisposition: `attachment; filename="${name}"`,
CacheControl: "public, max-age=31536000, immutable",
}),
)
}
return [name, metadata] as const
}),
),
)
} finally {
client?.destroy()
}
const current = files.at(-1)
if (!current) return
if (value.startsWith("sha512:")) current.sha512 = value.slice("sha512:".length).trim()
if (value.startsWith("size:")) current.size = Number(value.slice("size:".length).trim())
if (value.startsWith("blockMapSize:")) current.blockMapSize = Number(value.slice("blockMapSize:".length).trim())
})
if (!files.length || files.some((file) => !file.sha512 || !file.size)) {
throw new Error("Desktop metadata contained an incomplete file")
}
return { files, releaseDate }
}
function isRecord(input: unknown): input is Record<string, unknown> {
return typeof input === "object" && input !== null && !Array.isArray(input)
}
async function credentials() {
const account = process.env.CLOUDFLARE_ACCOUNT_ID
const token = process.env.CLOUDFLARE_API_TOKEN
if (!account || !token) throw new Error("CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN are required")
// R2 uses the token ID as its access key and SHA-256(token) as its secret.
for (const scope of [`accounts/${account}`, "user"]) {
const response = await fetch(`https://api.cloudflare.com/client/v4/${scope}/tokens/verify`, {
headers: { Authorization: `Bearer ${token}` },
})
if (!response.ok) continue
const data: unknown = await response.json()
if (!isRecord(data) || data.success !== true || !isRecord(data.result) || typeof data.result.id !== "string")
continue
return {
region: "auto",
endpoint: `https://${account}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: data.result.id,
secretAccessKey: createHash("sha256").update(token).digest("hex"),
},
}
}
throw new Error("Cloudflare token verification failed")
}