const fs = require('node:fs'); const path = require('node:path'); const { spawnSync } = require('node:child_process'); const config = JSON.parse(fs.readFileSync('/run/secrets/build-config', 'utf8')); const env = { ...process.env, NODE_ENV: 'development', ...config.env, NODE_OPTIONS: '--max-old-space-size=4096', npm_config_jobs: '2', MAKEFLAGS: '-j2', CMAKE_BUILD_PARALLEL_LEVEL: '2', UV_THREADPOOL_SIZE: '2', GOMAXPROCS: '2', }; for (const [phase, command] of [['install', config.installCommand], ['build', config.buildCommand]]) { console.log(`[artifact] ${phase} started inside resource-limited worker`); const result = spawnSync('/bin/sh', ['-c', command], { cwd: '/workspace', env, stdio: 'inherit' }); if (result.error || result.status !== 0) { console.error(`[artifact] ${phase} failed: ${result.error?.message || result.signal || result.status}`); process.exit(result.status || 1); } } const output = fs.realpathSync(path.resolve('/workspace', config.outputDir)); if (!output.startsWith('/workspace/')) throw new Error('Artifact output escaped the checkout'); fs.cpSync(output, '/build-output', { recursive: true });