From 5facd98b0d5d0c3b5f108960d7d6c569fafa0c11 Mon Sep 17 00:00:00 2001 From: yiling Date: Thu, 11 Jun 2026 10:10:55 +0800 Subject: [PATCH] feat(docker): pin runtime dependencies using dynamic package.json --- Dockerfile | 10 +++-- scripts/extract-runtime-deps.ts | 71 +++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 scripts/extract-runtime-deps.ts diff --git a/Dockerfile b/Dockerfile index d088d8a6..b0509815 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,6 +19,9 @@ COPY . . # Build API and Worker RUN bun run build.ts +# Generate runner-package.json for the runner stage +RUN bun run scripts/extract-runtime-deps.ts + # Stage 2: Runner FROM oven/bun:1 AS runner @@ -54,10 +57,9 @@ RUN mkdir -p /app/data && chown -R bun:bun /app USER bun WORKDIR /app -# Manually install temporalio packages and prisma -# These need native bindings or CLI access in the runner environment -RUN bun add @temporalio/activity @temporalio/client @temporalio/worker @temporalio/workflow && \ - bun add --dev --omit peer prisma sharp +# Install runtime dependencies with exact matching versions extracted from the builder stage +COPY --chown=bun:bun --from=builder /app/runner-package.json ./package.json +RUN bun install # Copy build artifacts and prisma config COPY --chown=bun:bun --from=builder /app/dist ./ diff --git a/scripts/extract-runtime-deps.ts b/scripts/extract-runtime-deps.ts new file mode 100644 index 00000000..98103545 --- /dev/null +++ b/scripts/extract-runtime-deps.ts @@ -0,0 +1,71 @@ +import { execSync } from 'node:child_process'; +import { writeFileSync } from 'node:fs'; + +/** + * This script extracts the exact versions of specific runtime dependencies + * from the current workspace using `bun pm ls --all`. + * It generates a `runner-package.json` that is used in the final stage of the Docker build + * to ensure that production dependencies match development/build versions exactly. + */ + +const depsToExtract = [ + '@temporalio/activity', + '@temporalio/client', + '@temporalio/worker', + '@temporalio/workflow', + 'prisma', + 'sharp' +]; + +function run(cmd: string) { + try { + return execSync(cmd, { encoding: 'utf-8', stdio: 'pipe' }); + } catch (e) { + console.error(`Command failed: ${cmd}`); + return null; + } +} + +console.log('Extracting runtime dependency versions using `bun pm ls --all`...'); + +const output = run('bun pm ls --all'); + +if (!output) { + console.error('Failed to get dependency list from bun.'); + process.exit(1); +} + +const deps: Record = {}; + +for (const dep of depsToExtract) { + // regex to find the version after the package name in `bun pm ls` output + // e.g., ├── @temporalio/client@1.17.2 + const escapedDep = dep.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + const regex = new RegExp(`(?:^|\\s)${escapedDep}@(\\d+\\.\\d+\\.\\d+[^\\s]*)`, 'g'); + const match = regex.exec(output); + + if (match && match[1]) { + // If there are multiple matches (different versions in workspace), we take the first one found. + // In our pinned workspace, they should all be the same. + deps[dep] = match[1]; + console.log(` Found ${dep}@${match[1]}`); + } else { + console.warn(` Warning: Could not find version for ${dep} in \`bun pm ls --all\` output.`); + } +} + +if (Object.keys(deps).length === 0) { + console.error('Error: No dependencies were extracted. Check if `bun install` was run correctly.'); + process.exit(1); +} + +const runnerPkg = { + name: "shumai-runner", + version: "1.0.0", + private: true, + dependencies: deps +}; + +const outputPath = 'runner-package.json'; +writeFileSync(outputPath, JSON.stringify(runnerPkg, null, 2) + '\n', 'utf-8'); +console.log(`\nGenerated ${outputPath} with ${Object.keys(deps).length} dependencies for Docker runner stage.`);