| 121 | const shellToUse = isWindows ? 'powershell.exe' : '/bin/bash'; |
| 122 | |
| 123 | function buildImage(imageName, dockerfile) { |
| 124 | console.log(`building ${imageName} ... (can be slow first time)`); |
| 125 | |
| 126 | let buildCommandArgs = ''; |
| 127 | let tempAuthFile = ''; |
| 128 | |
| 129 | if (sandboxCommand === 'podman') { |
| 130 | if (isWindows) { |
| 131 | // PowerShell doesn't support <() process substitution. |
| 132 | // Create a temporary auth file that we will clean up after. |
| 133 | tempAuthFile = join(os.tmpdir(), `anus-auth-${Date.now()}.json`); |
| 134 | writeFileSync(tempAuthFile, '{}'); |
| 135 | buildCommandArgs = `--authfile="${tempAuthFile}"`; |
| 136 | } else { |
| 137 | // Use bash-specific syntax for Linux/macOS |
| 138 | buildCommandArgs = `--authfile=<(echo '{}')`; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | const npmPackageVersion = JSON.parse( |
| 143 | readFileSync(join(process.cwd(), 'package.json'), 'utf-8'), |
| 144 | ).version; |
| 145 | |
| 146 | const imageTag = |
| 147 | process.env.ANUS_SANDBOX_IMAGE_TAG || imageName.split(':')[1]; |
| 148 | const finalImageName = `${imageName.split(':')[0]}:${imageTag}`; |
| 149 | |
| 150 | try { |
| 151 | execSync( |
| 152 | `${sandboxCommand} build ${buildCommandArgs} ${ |
| 153 | process.env.BUILD_SANDBOX_FLAGS || '' |
| 154 | } --build-arg CLI_VERSION_ARG=${npmPackageVersion} -f "${dockerfile}" -t "${imageName}" .`, |
| 155 | { stdio: buildStdout, shell: shellToUse }, |
| 156 | ); |
| 157 | console.log(`built ${finalImageName}`); |
| 158 | |
| 159 | // If an output file path was provided via command-line, write the final image URI to it. |
| 160 | if (argv.outputFile) { |
| 161 | console.log( |
| 162 | `Writing final image URI for CI artifact to: ${argv.outputFile}`, |
| 163 | ); |
| 164 | // The publish step only supports one image. If we build multiple, only the last one |
| 165 | // will be published. Throw an error to make this failure explicit if the file already exists. |
| 166 | if (existsSync(argv.outputFile)) { |
| 167 | throw new Error( |
| 168 | `CI artifact file ${argv.outputFile} already exists. Refusing to overwrite.`, |
| 169 | ); |
| 170 | } |
| 171 | writeFileSync(argv.outputFile, finalImageName); |
| 172 | } |
| 173 | } finally { |
| 174 | // If we created a temp file, delete it now. |
| 175 | if (tempAuthFile) { |
| 176 | rmSync(tempAuthFile, { force: true }); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |