Commit e9df3a17 authored by Matt Mastracci's avatar Matt Mastracci

Replace docker-image-cache.js with BuildKit registry cache

Use a reusable docker-buildx action with BuildKit registry cache backed
by the turbo remote cache server. The builder is kept alive between runs
on self-hosted runners so layers stay in BuildKit's local content store.

Removes the old docker-image-cache.js which used zstd-compressed tarballs.
parent 3e847dfe
name: 'Docker Buildx with cache registry'
description: 'Set up Docker Buildx with BuildKit cache backed by the turbo remote cache registry.'
inputs:
target:
description: 'Dockerfile target stage to build (e.g. linux, windows). Omit to build the default stage.'
required: false
default: ''
image-tag:
description: 'Local image tag (e.g. next-swc-builder-win:latest)'
required: true
cache-ref:
description: 'Registry cache ref name (e.g. next-swc-builder-win)'
required: true
dockerfile:
description: 'Path to Dockerfile'
required: false
default: 'scripts/native-builder.Dockerfile'
context:
description: 'Docker build context path'
required: false
default: '.'
outputs:
registry-host:
description: 'The resolved registry host:port'
value: ${{ steps.resolve.outputs.host }}
runs:
using: 'composite'
steps:
- name: Resolve cache registry
id: resolve
shell: bash -leo pipefail {0}
run: |
REGISTRY_HOST=$(echo "${TURBO_API}" | sed 's|^https\?://||')
REGISTRY_IP=$(echo "${REGISTRY_HOST}" | sed 's|:.*||')
echo "::add-mask::${REGISTRY_HOST}"
echo "::add-mask::${REGISTRY_IP}"
echo "host=${REGISTRY_HOST}" >> "$GITHUB_OUTPUT"
echo "TURBO_TOKEN=${TURBO_TOKEN}" >> "$GITHUB_ENV"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
# Reuse the builder across runs on self-hosted runners so BuildKit's
# internal content store acts as a local layer cache.
name: next-swc-builder
cleanup: false
driver-opts: network=host
buildkitd-config-inline: |
[registry."${{ steps.resolve.outputs.host }}"]
http = true
insecure = true
- name: Write registry credentials
shell: bash
run: |
REGISTRY="${{ steps.resolve.outputs.host }}"
AUTH=$(echo -n "cache:${TURBO_TOKEN}" | base64)
mkdir -p ~/.docker
CONFIG=$(cat ~/.docker/config.json 2>/dev/null || echo '{}')
echo "$CONFIG" | node -e "
const c = JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));
c.auths = c.auths || {};
c.auths['${REGISTRY}'] = { auth: '${AUTH}' };
process.stdout.write(JSON.stringify(c, null, 2));
" > ~/.docker/config.json
- name: Build Docker image
uses: docker/build-push-action@v6
with:
context: ${{ inputs.context }}
file: ${{ inputs.dockerfile }}
target: ${{ inputs.target || '' }}
load: true
tags: ${{ inputs.image-tag }}
cache-from: type=registry,ref=${{ steps.resolve.outputs.host }}/${{ inputs.cache-ref }}:cache
cache-to: type=registry,ref=${{ steps.resolve.outputs.host }}/${{ inputs.cache-ref }}:cache,mode=max
...@@ -264,7 +264,10 @@ jobs: ...@@ -264,7 +264,10 @@ jobs:
# Only load the Docker image if we need to compile (native cache miss) # Only load the Docker image if we need to compile (native cache miss)
- name: Build/restore Docker image - name: Build/restore Docker image
if: ${{ matrix.docker && steps.native-cache.outputs.HIT != 'yes' }} if: ${{ matrix.docker && steps.native-cache.outputs.HIT != 'yes' }}
run: node scripts/docker-image-cache.js uses: ./.github/actions/docker-buildx
with:
image-tag: ${{ matrix.docker }}
cache-ref: next-swc-builder
- name: Build in docker - name: Build in docker
if: ${{ matrix.docker && steps.native-cache.outputs.HIT != 'yes' }} if: ${{ matrix.docker && steps.native-cache.outputs.HIT != 'yes' }}
......
#!/usr/bin/env node
//
// Build or restore the next-swc-builder Docker image using turbo remote cache.
//
// Computes a cache key from the Dockerfile + rust-toolchain.toml contents,
// then checks the turbo cache API via scripts/turbo-cache.mjs.
// Uses docker export/import (flat filesystem) instead of save/load (layered)
// to avoid including redundant base image layers. Compressed with zstd.
//
// Usage:
// node scripts/docker-image-cache.js # restore from cache or build + upload
// node scripts/docker-image-cache.js --force # always rebuild and re-upload
const { execSync } = require('child_process')
const crypto = require('crypto')
const { createHash } = crypto
const path = require('path')
const fs = require('fs')
const os = require('os')
const { parseArgs } = require('node:util')
const { values: flags } = parseArgs({
args: process.argv.slice(2),
options: {
force: { type: 'boolean', default: false },
},
})
const REPO_ROOT = path.resolve(__dirname, '..')
const IMAGE_NAME = 'next-swc-builder:latest'
// docker export/import strips all image metadata. These --change flags
// restore the ENV and WORKDIR that the Dockerfile sets, so that tools
// like cargo, rustc, napi, sccache are found in PATH.
const DOCKER_IMPORT_CHANGES = [
'ENV PATH=/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
'ENV DEBIAN_FRONTEND=noninteractive',
'WORKDIR /build',
]
// Files baked into the Docker image — only these affect the image content.
// Scripts that run on the host (docker-image-cache.js, docker-native-build.*)
// are NOT included since they're mounted at runtime, not COPY'd.
const CACHE_INPUTS = [
path.join(REPO_ROOT, 'scripts/native-builder.Dockerfile'),
path.join(REPO_ROOT, 'rust-toolchain.toml'),
]
function computeCacheKey() {
// Turbo cache keys must be hex-only (^[a-fA-F0-9]+$).
const hash = createHash('sha256')
hash.update('docker-image-v4\0')
// Include host architecture — the image contains native binaries
// (Rust toolchain, cargo-xwin, etc.) that are arch-specific.
hash.update(`arch:${os.arch()}\0`)
for (const file of CACHE_INPUTS) {
hash.update(file + '\0')
hash.update(fs.readFileSync(file))
}
return hash.digest('hex')
}
function buildImage() {
console.log(`Building Docker image: ${IMAGE_NAME}`)
const ctx = fs.mkdtempSync(path.join(os.tmpdir(), 'next-swc-docker-'))
fs.copyFileSync(
path.join(REPO_ROOT, 'rust-toolchain.toml'),
path.join(ctx, 'rust-toolchain.toml')
)
try {
execSync(
`docker build -t ${IMAGE_NAME} -f ${path.join(REPO_ROOT, 'scripts/native-builder.Dockerfile')} ${ctx}`,
{ stdio: 'inherit' }
)
} finally {
fs.rmSync(ctx, { recursive: true, force: true })
}
}
function tmpFile(name) {
const suffix = crypto.randomBytes(6).toString('hex')
return path.join(process.env.RUNNER_TEMP || os.tmpdir(), `${name}.${suffix}`)
}
function sh(cmd) {
execSync(cmd, { stdio: 'inherit', shell: true })
}
async function main() {
const cache = await import('./turbo-cache.mjs')
const key = computeCacheKey()
// Show redacted endpoint for debugging (scheme + first 2 chars of host)
const apiUrl = new URL(process.env.TURBO_API || 'https://vercel.com')
const redactedApi = `${apiUrl.protocol}//${apiUrl.hostname.slice(0, 2)}***`
console.log(`Docker image: ${IMAGE_NAME}`)
console.log(`Cache key: ${key}`)
console.log(`Cache endpoint: ${redactedApi}`)
if (!process.env.TURBO_TOKEN) {
console.log('No TURBO_TOKEN — building without cache')
buildImage()
return
}
// Try to restore from cache (unless --force)
if (!flags.force) {
const hit = await cache.exists(key)
console.log(hit ? 'Cache HIT' : 'Cache MISS')
if (hit) {
const zstFile = tmpFile('docker-image-cache.tar.zst')
let restored = false
for (let attempt = 1; attempt <= 3; attempt++) {
try {
console.log(
`Downloading cached image${attempt > 1 ? ` (retry ${attempt})` : ''}...`
)
const result = await cache.getToFile(key, zstFile, { retries: 0 })
if (!result.ok) throw new Error('download failed')
if (result.stats) {
console.log(`Downloaded: ${cache.formatStats(result.stats)}`)
}
console.log('Decompressing and importing into Docker...')
const changeFlags = DOCKER_IMPORT_CHANGES.map(
(c) => `--change '${c}'`
).join(' ')
sh(
`zstd -d -c --long=27 --threads=0 ${zstFile} | docker import ${changeFlags} - ${IMAGE_NAME}`
)
console.log('Docker image restored from turbo cache')
restored = true
break
} catch (e) {
console.log(`WARNING: Attempt ${attempt} failed: ${e.message}`)
try {
execSync(`docker rmi -f ${IMAGE_NAME}`, { stdio: 'ignore' })
} catch {}
} finally {
try {
fs.unlinkSync(zstFile)
} catch {}
}
}
if (restored) return
console.log('All restore attempts failed — rebuilding from scratch')
}
}
// Cache miss or --force: always rebuild since inputs changed
buildImage()
// Export and compress with zstd (docker export produces uncompressed tar).
const zstFile = tmpFile('docker-image-cache.tar.zst')
const containerName = `next-swc-export-${process.pid}`
try {
sh(`docker create --name ${containerName} ${IMAGE_NAME} true`)
sh(`docker export ${containerName} | zstd -1 -T0 --long=27 -o ${zstFile}`)
sh(`docker rm ${containerName}`)
const size = fs.statSync(zstFile).size
console.log(
`Exported + compressed: ${(size / 1024 / 1024).toFixed(0)} MB — uploading...`
)
try {
// Stream upload from file (avoids 2GB Buffer limit)
await cache.put(key, zstFile)
console.log('Docker image uploaded to turbo cache')
} catch (e) {
console.log(`WARNING: Failed to upload: ${e.message}`)
}
} finally {
try {
fs.unlinkSync(zstFile)
} catch {}
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
// Usage: node scripts/docker-native-build.js [flags] [filter] // Usage: node scripts/docker-native-build.js [flags] [filter]
// --quick Use release-with-assertions profile (no LTO, faster) // --quick Use release-with-assertions profile (no LTO, faster)
// --host-target Share host target/ dir with container for caching // --host-target Share host target/ dir with container for caching
// --rebuild Force Docker image rebuild
// --test Smoke-test built binaries (native arch only) // --test Smoke-test built binaries (native arch only)
// filter Substring match on target name (e.g. "musl", "x86_64") // filter Substring match on target name (e.g. "musl", "x86_64")
...@@ -54,7 +53,6 @@ const { values: flags, positionals } = parseArgs({ ...@@ -54,7 +53,6 @@ const { values: flags, positionals } = parseArgs({
options: { options: {
quick: { type: 'boolean', default: false }, quick: { type: 'boolean', default: false },
'host-target': { type: 'boolean', default: false }, 'host-target': { type: 'boolean', default: false },
rebuild: { type: 'boolean', default: false },
test: { type: 'boolean', default: false }, test: { type: 'boolean', default: false },
help: { type: 'boolean', short: 'h', default: false }, help: { type: 'boolean', short: 'h', default: false },
}, },
...@@ -63,14 +61,13 @@ const { values: flags, positionals } = parseArgs({ ...@@ -63,14 +61,13 @@ const { values: flags, positionals } = parseArgs({
if (flags.help) { if (flags.help) {
console.log( console.log(
'Usage: node scripts/docker-native-build.js [--quick] [--host-target] [--rebuild] [--test] [filter]' 'Usage: node scripts/docker-native-build.js [--quick] [--host-target] [--test] [filter]'
) )
process.exit(0) process.exit(0)
} }
const quick = flags.quick const quick = flags.quick
const hostTarget = flags['host-target'] const hostTarget = flags['host-target']
const rebuild = flags.rebuild
const test = flags.test const test = flags.test
const filter = positionals[0] || '' const filter = positionals[0] || ''
...@@ -85,12 +82,19 @@ if (targets.length === 0) { ...@@ -85,12 +82,19 @@ if (targets.length === 0) {
process.exit(1) process.exit(1)
} }
// --- Build/restore Docker image --- // --- Build Docker image locally (CI uses .github/actions/docker-buildx instead) ---
function ensureDockerImage() { function ensureDockerImage() {
const args = rebuild ? ['--force'] : [] console.log(`Building Docker image: ${DOCKER_IMAGE}`)
execFileSync( execFileSync(
'node', 'docker',
[path.join(__dirname, 'docker-image-cache.js'), ...args], [
'build',
'-t',
DOCKER_IMAGE,
'-f',
path.join(REPO_ROOT, 'scripts/native-builder.Dockerfile'),
REPO_ROOT,
],
{ stdio: 'inherit' } { stdio: 'inherit' }
) )
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment