ensure sandbox build script is cross-platform (#2603)

Co-authored-by: Allen Hutchison <adh@google.com>
This commit is contained in:
doonrevver86 2025-08-12 19:18:06 +01:00 committed by GitHub
parent 67c6033147
commit c5c6966d08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 46 additions and 22 deletions

View File

@ -20,6 +20,7 @@
import { execSync } from 'child_process'; import { execSync } from 'child_process';
import { chmodSync, existsSync, readFileSync, rmSync, writeFileSync } from 'fs'; import { chmodSync, existsSync, readFileSync, rmSync, writeFileSync } from 'fs';
import { join } from 'path'; import { join } from 'path';
import os from 'os';
import yargs from 'yargs'; import yargs from 'yargs';
import { hideBin } from 'yargs/helpers'; import { hideBin } from 'yargs/helpers';
import cliPkgJson from '../packages/cli/package.json' with { type: 'json' }; import cliPkgJson from '../packages/cli/package.json' with { type: 'json' };
@ -117,12 +118,28 @@ chmodSync(
const buildStdout = process.env.VERBOSE ? 'inherit' : 'ignore'; const buildStdout = process.env.VERBOSE ? 'inherit' : 'ignore';
// Determine the appropriate shell based on OS
const isWindows = os.platform() === 'win32';
const shellToUse = isWindows ? 'powershell.exe' : '/bin/bash';
function buildImage(imageName, dockerfile) { function buildImage(imageName, dockerfile) {
console.log(`building ${imageName} ... (can be slow first time)`); console.log(`building ${imageName} ... (can be slow first time)`);
const buildCommand =
sandboxCommand === 'podman' let buildCommandArgs = '';
? `${sandboxCommand} build --authfile=<(echo '{}')` let tempAuthFile = '';
: `${sandboxCommand} build`;
if (sandboxCommand === 'podman') {
if (isWindows) {
// PowerShell doesn't support <() process substitution.
// Create a temporary auth file that we will clean up after.
tempAuthFile = join(os.tmpdir(), `gemini-auth-${Date.now()}.json`);
writeFileSync(tempAuthFile, '{}');
buildCommandArgs = `--authfile="${tempAuthFile}"`;
} else {
// Use bash-specific syntax for Linux/macOS
buildCommandArgs = `--authfile=<(echo '{}')`;
}
}
const npmPackageVersion = JSON.parse( const npmPackageVersion = JSON.parse(
readFileSync(join(process.cwd(), 'package.json'), 'utf-8'), readFileSync(join(process.cwd(), 'package.json'), 'utf-8'),
@ -132,11 +149,12 @@ function buildImage(imageName, dockerfile) {
process.env.GEMINI_SANDBOX_IMAGE_TAG || imageName.split(':')[1]; process.env.GEMINI_SANDBOX_IMAGE_TAG || imageName.split(':')[1];
const finalImageName = `${imageName.split(':')[0]}:${imageTag}`; const finalImageName = `${imageName.split(':')[0]}:${imageTag}`;
try {
execSync( execSync(
`${buildCommand} ${ `${sandboxCommand} build ${buildCommandArgs} ${
process.env.BUILD_SANDBOX_FLAGS || '' process.env.BUILD_SANDBOX_FLAGS || ''
} --build-arg CLI_VERSION_ARG=${npmPackageVersion} -f "${dockerfile}" -t "${finalImageName}" .`, } --build-arg CLI_VERSION_ARG=${npmPackageVersion} -f "${dockerfile}" -t "${imageName}" .`,
{ stdio: buildStdout, shell: '/bin/bash' }, { stdio: buildStdout, shell: shellToUse },
); );
console.log(`built ${finalImageName}`); console.log(`built ${finalImageName}`);
@ -154,6 +172,12 @@ function buildImage(imageName, dockerfile) {
} }
writeFileSync(argv.outputFile, finalImageName); writeFileSync(argv.outputFile, finalImageName);
} }
} finally {
// If we created a temp file, delete it now.
if (tempAuthFile) {
rmSync(tempAuthFile, { force: true });
}
}
} }
if (baseImage && baseDockerfile) { if (baseImage && baseDockerfile) {