buildx(history): detach dial-stdio process

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-06-29 15:04:30 +02:00
parent 2cb2c5573f
commit 200e43c426
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4

View File

@ -14,9 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
import {ChildProcessByStdio, spawn} from 'child_process'; import {spawn} from 'child_process';
import fs from 'fs'; import fs from 'fs';
import {Readable, Writable} from 'node:stream';
import os from 'os'; import os from 'os';
import path from 'path'; import path from 'path';
import * as core from '@actions/core'; import * as core from '@actions/core';
@ -93,9 +92,17 @@ export class History {
await Exec.exec('mkfifo', [buildxOutFifoPath]); await Exec.exec('mkfifo', [buildxOutFifoPath]);
const buildxCmd = await this.buildx.getCommand(['--builder', builderName, 'dial-stdio']); const buildxCmd = await this.buildx.getCommand(['--builder', builderName, 'dial-stdio']);
const buildxDialStdioProc = History.spawn(buildxCmd.command, buildxCmd.args);
core.info(`[command]${buildxCmd.command} ${buildxCmd.args.join(' ')}`);
const buildxDialStdioProc = spawn(buildxCmd.command, buildxCmd.args, {
stdio: ['pipe', 'pipe', 'inherit'],
detached: true
});
fs.createReadStream(buildxInFifoPath).pipe(buildxDialStdioProc.stdin); fs.createReadStream(buildxInFifoPath).pipe(buildxDialStdioProc.stdin);
buildxDialStdioProc.stdout.pipe(fs.createWriteStream(buildxOutFifoPath)); buildxDialStdioProc.stdout.pipe(fs.createWriteStream(buildxOutFifoPath));
buildxDialStdioProc.on('exit', code => {
core.info(`Process "buildx dial-stdio" exited with code ${code}`);
});
const tmpDockerbuildFilename = path.join(outDir, 'rec.dockerbuild'); const tmpDockerbuildFilename = path.join(outDir, 'rec.dockerbuild');
const summaryFilename = path.join(outDir, 'summary.json'); const summaryFilename = path.join(outDir, 'summary.json');
@ -112,13 +119,17 @@ export class History {
ebargs.push(`--gid=${process.getgid()}`); ebargs.push(`--gid=${process.getgid()}`);
} }
// prettier-ignore // prettier-ignore
const dockerRunProc = History.spawn('docker', [ const dockerRunArgs = [
'run', '--rm', '-i', 'run', '--rm', '-i',
'-v', `${Buildx.refsDir}:/buildx-refs`, '-v', `${Buildx.refsDir}:/buildx-refs`,
'-v', `${outDir}:/out`, '-v', `${outDir}:/out`,
opts.image || History.EXPORT_TOOL_IMAGE, opts.image || History.EXPORT_TOOL_IMAGE,
...ebargs ...ebargs
]); ]
core.info(`[command]docker ${dockerRunArgs.join(' ')}`);
const dockerRunProc = spawn('docker', dockerRunArgs, {
stdio: ['pipe', 'pipe', 'inherit']
});
fs.createReadStream(buildxOutFifoPath).pipe(dockerRunProc.stdin); fs.createReadStream(buildxOutFifoPath).pipe(dockerRunProc.stdin);
dockerRunProc.stdout.pipe(fs.createWriteStream(buildxInFifoPath)); dockerRunProc.stdout.pipe(fs.createWriteStream(buildxInFifoPath));
dockerRunProc.on('close', code => { dockerRunProc.on('close', code => {
@ -133,9 +144,12 @@ export class History {
} }
}); });
dockerRunProc.on('error', err => { dockerRunProc.on('error', err => {
core.error(`Error executing buildx dial-stdio: ${err}`); core.error(`Error executing "docker run": ${err}`);
reject(err); reject(err);
}); });
dockerRunProc.on('exit', code => {
core.info(`Process "docker run" exited with code ${code}`);
});
}).catch(err => { }).catch(err => {
throw err; throw err;
}); });
@ -162,11 +176,4 @@ export class History {
refs: refs refs: refs
}; };
} }
private static spawn(command: string, args?: ReadonlyArray<string>): ChildProcessByStdio<Writable, Readable, null> {
core.info(`[command]${command}${args ? ` ${args.join(' ')}` : ''}`);
return spawn(command, args || [], {
stdio: ['pipe', 'pipe', 'inherit']
});
}
} }