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 29, 2024
1 parent ae20b6f commit fefa577
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 0 deletions.
171 changes: 171 additions & 0 deletions __tests__/buildx/history.test.itg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/**
* 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 {Bake} from '../../src/buildx/bake';
import {Build} from '../../src/buildx/build';
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', async () => {
const buildx = new Buildx();
const build = new Build({buildx: buildx});

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

const metadata = build.resolveMetadata();
expect(metadata).toBeDefined();
console.log('metadata', JSON.stringify(metadata, null, 2));
const buildRef = build.resolveRef(metadata);
expect(buildRef).toBeDefined();
console.log('buildRef', buildRef);

const history = new History({buildx: buildx});
const res = await history.export({
refs: [buildRef ?? '']
});

expect(res).toBeDefined();
console.log('exportRecordResponse', res);

expect(res?.dockerbuildFilename).toBeDefined();
expect(res?.dockerbuildSize).toBeDefined();
expect(res?.summaryFilename).toBeDefined();
expect(res?.summarySize).toBeDefined();

const summary = JSON.parse(fs.readFileSync(res?.summaryFilename, {encoding: 'utf-8'}));
expect(summary).toBeDefined();
console.log('summary', JSON.stringify(summary, null, 2));
});

it('exports bake build', async () => {
const buildx = new Buildx();
const bake = new Bake({buildx: buildx});

fs.mkdirSync(tmpDir, {recursive: true});
await expect(
(async () => {
// prettier-ignore
const buildCmd = await buildx.getCommand([
'bake',
'-f', path.join(fixturesDir, 'hello-bake.hcl'),
'--metadata-file', bake.getMetadataFilePath(),
'build'
]);
await Exec.exec(buildCmd.command, buildCmd.args, {
cwd: fixturesDir
});
})()
).resolves.not.toThrow();

const metadata = bake.resolveMetadata();
expect(metadata).toBeDefined();
console.log('metadata', JSON.stringify(metadata, null, 2));
const buildRefs = bake.resolveRefs(metadata);
expect(buildRefs).toBeDefined();
console.log('buildRefs', buildRefs);

const history = new History({buildx: buildx});
const res = await history.export({
refs: buildRefs ?? []
});

expect(res).toBeDefined();
console.log('exportRecordResponse', res);

expect(res?.dockerbuildFilename).toBeDefined();
expect(res?.dockerbuildSize).toBeDefined();
expect(res?.summaryFilename).toBeDefined();
expect(res?.summarySize).toBeDefined();

const summary = JSON.parse(fs.readFileSync(res?.summaryFilename, {encoding: 'utf-8'}));
expect(summary).toBeDefined();
console.log('summary', JSON.stringify(summary, null, 2));
});

it('exports bake build group', async () => {
const buildx = new Buildx();
const bake = new Bake({buildx: buildx});

fs.mkdirSync(tmpDir, {recursive: true});
await expect(
(async () => {
// prettier-ignore
const buildCmd = await buildx.getCommand([
'bake',
'-f', path.join(fixturesDir, 'hello-bake.hcl'),
'--metadata-file', bake.getMetadataFilePath(),
'hello-all'
]);
await Exec.exec(buildCmd.command, buildCmd.args, {
cwd: fixturesDir
});
})()
).resolves.not.toThrow();

const metadata = bake.resolveMetadata();
expect(metadata).toBeDefined();
console.log('metadata', JSON.stringify(metadata, null, 2));
const buildRefs = bake.resolveRefs(metadata);
expect(buildRefs).toBeDefined();
console.log('buildRefs', buildRefs);

const history = new History({buildx: buildx});
const res = await history.export({
refs: buildRefs ?? []
});

expect(res).toBeDefined();
console.log('exportRecordResponse', res);

expect(res?.dockerbuildFilename).toBeDefined();
expect(res?.dockerbuildSize).toBeDefined();
expect(res?.summaryFilename).toBeDefined();
expect(res?.summarySize).toBeDefined();

const summary = JSON.parse(fs.readFileSync(res?.summaryFilename, {encoding: 'utf-8'}));
expect(summary).toBeDefined();
console.log('summary', JSON.stringify(summary, null, 2));
});
});
28 changes: 28 additions & 0 deletions __tests__/fixtures/hello-bake.hcl
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.

target "hello" {
dockerfile = "hello.Dockerfile"
}

target "hello-bar" {
dockerfile = "hello.Dockerfile"
args = {
NAME = "bar"
}
}

group "hello-all" {
targets = ["hello", "hello-bar"]
}
19 changes: 19 additions & 0 deletions __tests__/fixtures/hello.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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
ARG NAME=foo
RUN echo "Hello $NAME!"
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
Loading

0 comments on commit fefa577

Please sign in to comment.