Skip to content

Commit

Permalink
buildx: build history export
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Apr 18, 2024
1 parent 2bba1c1 commit 285f252
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 0 deletions.
72 changes: 72 additions & 0 deletions __tests__/buildx/history.test.itg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright 2024 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {beforeEach, describe, expect, it, jest} from '@jest/globals';
import * as fs from 'fs';
import * as path from 'path';

import {Buildx} from '../../src/buildx/buildx';
import {History} from '../../src/buildx/history';
import {Exec} from '../../src/exec';

const fixturesDir = path.join(__dirname, '..', 'fixtures');

// prettier-ignore
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-history-jest');

const maybe = !process.env.GITHUB_ACTIONS || (process.env.GITHUB_ACTIONS === 'true' && process.env.ImageOS && process.env.ImageOS.startsWith('ubuntu')) ? describe : describe.skip;

beforeEach(() => {
jest.clearAllMocks();
});

maybe('export', () => {
it('exports build record', async () => {
const buildx = new Buildx();

fs.mkdirSync(tmpDir, {recursive: true});
const metadataFile = path.join(tmpDir, 'history-export-metadata.json');

await expect(
(async () => {
// prettier-ignore
const buildCmd = await buildx.getCommand([
'build',
'-f', path.join(fixturesDir, 'hello.Dockerfile'),
'--metadata-file', metadataFile,
fixturesDir
]);
await Exec.exec(buildCmd.command, buildCmd.args);
})()
).resolves.not.toThrow();

expect(fs.existsSync(metadataFile)).toBe(true);
const metadata = JSON.parse(fs.readFileSync(metadataFile, {encoding: 'utf-8'}));
expect(metadata).toBeDefined();
console.log('metadata', JSON.stringify(metadata, null, 2));
expect(metadata['buildx.build.ref']).toBeDefined();
const buildRef = metadata['buildx.build.ref'];
console.log('buildRef', buildRef);

const history = new History({buildx: buildx});
const res = await history.export({
ref: metadata['buildx.build.ref']
});
expect(res).toBeDefined();
expect(res?.filename).toBeDefined();
expect(res?.size).toBeDefined();
});
});
18 changes: 18 additions & 0 deletions __tests__/fixtures/hello.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# syntax=docker/dockerfile:1

# Copyright 2024 actions-toolkit authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM busybox
RUN echo "Hello, World!"
15 changes: 15 additions & 0 deletions jest.config.itg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
* limitations under the License.
*/

import fs from 'fs';
import os from 'os';
import path from 'path';

const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-actions-toolkit-'));

process.env = Object.assign({}, process.env, {
TEMP: tmpDir,
GITHUB_REPOSITORY: 'docker/actions-toolkit',
RUNNER_TEMP: path.join(tmpDir, 'runner-temp'),
RUNNER_TOOL_CACHE: path.join(tmpDir, 'runner-tool-cache')
}) as {
[key: string]: string;
};

module.exports = {
clearMocks: true,
testEnvironment: 'node',
Expand Down
112 changes: 112 additions & 0 deletions src/buildx/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* Copyright 2024 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {ChildProcessByStdio, spawn} from 'child_process';
import fs from 'fs';
import {Readable, Writable} from 'node:stream';
import os from 'os';
import path from 'path';
import * as core from '@actions/core';

import {Buildx} from './buildx';
import {Context} from '../context';
import {Exec} from '../exec';

import {ExportRecordOpts, ExportRecordResponse} from '../types/history';

export interface HistoryOpts {
buildx?: Buildx;
}

export class History {
private readonly buildx: Buildx;

private static readonly EXPORT_TOOL_IMAGE: string = 'docker.io/tonistiigi/test:export-build';

constructor(opts?: HistoryOpts) {
this.buildx = opts?.buildx || new Buildx();
}

public async export(opts: ExportRecordOpts): Promise<ExportRecordResponse> {
if (os.platform() === 'win32') {
throw new Error('Exporting a build record is currently not supported on Windows');
}

const refParts = opts.ref.split('/');
if (refParts.length != 3) {
throw new Error(`Invalid build ref: ${opts.ref}`);
}

const builderName = refParts[0];
const nodeName = refParts[1];
const refID = refParts[2];
const outDir = path.join(Context.tmpDir(), 'export');
const outFile = `${builderName}-${refID}.dockerbuild`;
const outFilename = path.join(outDir, outFile);

core.info(`exporting build record to ${outFilename}`);
fs.mkdirSync(outDir, {recursive: true});

const buildxInFifoPath = Context.tmpName({
template: 'buildx-in-XXXXXX.fifo',
tmpdir: Context.tmpDir()
});
await Exec.exec('mkfifo', [buildxInFifoPath]);

const buildxOutFifoPath = Context.tmpName({
template: 'buildx-out-XXXXXX.fifo',
tmpdir: Context.tmpDir()
});
await Exec.exec('mkfifo', [buildxOutFifoPath]);

const buildxDialStdioProc = History.spawn('docker', ['buildx', '--builder', builderName, 'dial-stdio']);
fs.createReadStream(buildxInFifoPath).pipe(buildxDialStdioProc.stdin);
buildxDialStdioProc.stdout.pipe(fs.createWriteStream(buildxOutFifoPath));

return new Promise<ExportRecordResponse>((resolve, reject) => {
const dockerRunProc = History.spawn('docker', ['run', '-i', '-v', `${outDir}:/out`, opts.image || History.EXPORT_TOOL_IMAGE, `--ref=${refID}`, `--output=${outFile}`]);
fs.createReadStream(buildxOutFifoPath).pipe(dockerRunProc.stdin);
dockerRunProc.stdout.pipe(fs.createWriteStream(buildxInFifoPath));

dockerRunProc.on('close', code => {
if (code === 0) {
const outStats = fs.statSync(outFilename);
resolve({
filename: outFilename,
size: outStats.size,
builderName: builderName,
nodeName: nodeName,
refID: refID
});
} else {
reject(new Error(`Process "docker run" exited with code ${code}`));
}
});

dockerRunProc.on('error', err => {
core.error(`Error executing buildx dial-stdio: ${err}`);
reject(err);
});
});
}

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']
});
}
}
28 changes: 28 additions & 0 deletions src/types/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright 2024 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export interface ExportRecordOpts {
ref: string;
image?: string;
}

export interface ExportRecordResponse {
filename: string;
size: number;
builderName: string;
nodeName: string;
refID: string;
}

0 comments on commit 285f252

Please sign in to comment.