Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildx(history): use "docker cp" instead of volume mounts #387

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 64 additions & 23 deletions src/buildx/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ export class History {
const tmpDockerbuildFilename = path.join(outDir, 'rec.dockerbuild');
const summaryFilename = path.join(outDir, 'summary.json');

await new Promise<void>((resolve, reject) => {
const ebargs: Array<string> = ['--ref-state-dir=/buildx-refs', `--node=${builderName}/${nodeName}`];
const exportBuild = async (resolve, reject) => {
const ebargs: Array<string> = [`--output=/out`, `--node=${builderName}/${nodeName}`];
if (fs.existsSync(Buildx.refsDir)) {
ebargs.push('--ref-state-dir=/buildx-refs');
}
for (const ref of refs) {
ebargs.push(`--ref=${ref}`);
}
Expand All @@ -111,32 +114,70 @@ export class History {
if (typeof process.getgid === 'function') {
ebargs.push(`--gid=${process.getgid()}`);
}

// prettier-ignore
const dockerRunProc = History.spawn('docker', [
'run', '--rm', '-i',
'-v', `${Buildx.refsDir}:/buildx-refs`,
'-v', `${outDir}:/out`,
const ctnExportBuildID = await Exec.getExecOutput(`docker`, [
'create', '-i',
opts.image || History.EXPORT_TOOL_IMAGE,
...ebargs
]);
fs.createReadStream(buildxOutFifoPath).pipe(dockerRunProc.stdin);
dockerRunProc.stdout.pipe(fs.createWriteStream(buildxInFifoPath));
dockerRunProc.on('close', code => {
if (code === 0) {
if (!fs.existsSync(tmpDockerbuildFilename)) {
reject(new Error(`Failed to export build record: ${tmpDockerbuildFilename} not found`));
} else {
resolve();
}
} else {
reject(new Error(`Process "docker run" exited with code ${code}`));
], {
ignoreReturnCode: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
reject(new Error(`Error executing "docker create": ${res.stderr.trim()}`));
}
core.info(`Export build container created with ID "${res.stdout.trim()}"`);
return res.stdout.trim();
});
dockerRunProc.on('error', err => {
core.error(`Error executing buildx dial-stdio: ${err}`);
reject(err);
});
}).catch(err => {

try {
if (fs.existsSync(Buildx.refsDir)) {
await Exec.getExecOutput(`docker`, ['cp', Buildx.refsDir, `${ctnExportBuildID}:/buildx-refs`], {
ignoreReturnCode: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
reject(new Error(`Error executing "docker cp": ${res.stderr.trim()}`));
}
});
}

const dockerStartProc = History.spawn('docker', ['start', '-a', ctnExportBuildID]);
fs.createReadStream(buildxOutFifoPath).pipe(dockerStartProc.stdin);
dockerStartProc.stdout.pipe(fs.createWriteStream(buildxInFifoPath));
dockerStartProc.on('close', async code => {
if (code === 0) {
await Exec.getExecOutput(`docker`, ['cp', `${ctnExportBuildID}:/out`, outDir], {
ignoreReturnCode: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
reject(new Error(`Error executing "docker cp": ${res.stderr.trim()}`));
}
});
if (!fs.existsSync(tmpDockerbuildFilename)) {
reject(new Error(`Failed to export build record: ${tmpDockerbuildFilename} not found`));
} else {
resolve();
}
} else {
reject(new Error(`Process "docker start" exited with code ${code}`));
}
});
dockerStartProc.on('error', err => {
core.error(`Error executing "docker start": ${err}`);
reject(err);
});
} finally {
await Exec.getExecOutput(`docker`, ['rm', '-f', ctnExportBuildID], {
ignoreReturnCode: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
reject(new Error(`Error executing "docker rm": ${res.stderr.trim()}`));
}
});
}
};

await new Promise<void>(exportBuild).catch(err => {
throw err;
});

Expand Down
Loading