Files
BrowserOS/scripts/build/server/archive.ts
Nikhil 9fdb361d67 feat: build prod server resource artifact pipeline (#417)
* feat: build prod server resource artifacts with cloudflare r2

* fix: address PR review comments for prod_server_resources_cloudflare

* feat: fix prod server build setup and CLI ergonomics

* fix: prevent build env secret inlining and template fallback

* fix: read from fileenv

* feat: add ripgrep

* feat: upload prod artifacts to latest and version prefixes
2026-03-05 14:26:46 -08:00

66 lines
1.8 KiB
TypeScript

import { rm } from 'node:fs/promises'
import { basename, dirname, isAbsolute, join, resolve } from 'node:path'
import type { S3Client } from '@aws-sdk/client-s3'
import { runCommand } from './command'
import { joinObjectKey, uploadFileToObject } from './r2'
import type { R2Config, StagedArtifact, UploadResult } from './types'
function zipPathForArtifact(artifact: StagedArtifact): string {
return join(
dirname(artifact.rootDir),
`browseros-server-resources-${artifact.target.id}.zip`,
)
}
export async function zipArtifactRoot(
artifactRoot: string,
outputZipPath: string,
): Promise<void> {
const absoluteOutputZipPath = isAbsolute(outputZipPath)
? outputZipPath
: resolve(outputZipPath)
await rm(absoluteOutputZipPath, { force: true })
await runCommand(
'zip',
['-r', '-q', absoluteOutputZipPath, '.'],
process.env,
artifactRoot,
)
}
export async function archiveAndUploadArtifacts(
artifacts: StagedArtifact[],
version: string,
client: S3Client,
r2: R2Config,
upload: boolean,
): Promise<UploadResult[]> {
const results: UploadResult[] = []
for (const artifact of artifacts) {
const zipPath = zipPathForArtifact(artifact)
await zipArtifactRoot(artifact.rootDir, zipPath)
if (!upload) {
results.push({ targetId: artifact.target.id, zipPath })
continue
}
const fileName = basename(zipPath)
const latestR2Key = joinObjectKey(r2.uploadPrefix, 'latest', fileName)
const versionR2Key = joinObjectKey(r2.uploadPrefix, version, fileName)
await uploadFileToObject(client, r2, latestR2Key, zipPath)
await uploadFileToObject(client, r2, versionR2Key, zipPath)
results.push({
targetId: artifact.target.id,
zipPath,
latestR2Key,
versionR2Key,
})
}
return results
}