Skip to content

Commit

Permalink
Merge pull request #307 from crazy-max/build-export
Browse files Browse the repository at this point in the history
buildx: build history export
  • Loading branch information
crazy-max committed May 14, 2024
2 parents 7eb10a1 + ee84f2a commit 624e16f
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ jobs:
with:
version: ${{ env.BUILDX_VERSION }}
driver: docker
-
name: Set up container builder
if: startsWith(matrix.os, 'ubuntu')
id: builder
uses: docker/setup-buildx-action@v3
with:
version: ${{ env.BUILDX_VERSION }}
use: false
-
name: Install
run: yarn install
Expand All @@ -140,6 +148,7 @@ jobs:
yarn test:itg-coverage --runTestsByPath __tests__/${{ matrix.test }} --coverageDirectory=./coverage
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CTN_BUILDER_NAME: ${{ steps.builder.outputs.name }}
-
name: Check coverage
run: |
Expand Down
151 changes: 151 additions & 0 deletions __tests__/buildx/history.test.itg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* 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, jest, test} 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('exportBuild', () => {
// prettier-ignore
test.each([
[
'single',
[
'build',
'-f', path.join(fixturesDir, 'hello.Dockerfile'),
fixturesDir
],
],
[
'multi-platform',
[
'build',
'-f', path.join(fixturesDir, 'hello.Dockerfile'),
'--platform', 'linux/amd64,linux/arm64',
fixturesDir
],
]
])('export build %p', async (_, bargs) => {
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([
'--builder', process.env.CTN_BUILDER_NAME ?? 'default',
...bargs,
'--metadata-file', build.getMetadataFilePath()
]);
await Exec.exec(buildCmd.command, buildCmd.args);
})()
).resolves.not.toThrow();

const metadata = build.resolveMetadata();
expect(metadata).toBeDefined();
const buildRef = build.resolveRef(metadata);
expect(buildRef).toBeDefined();

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

expect(exportRes).toBeDefined();
expect(exportRes?.dockerbuildFilename).toBeDefined();
expect(exportRes?.dockerbuildSize).toBeDefined();
expect(fs.existsSync(exportRes?.dockerbuildFilename)).toBe(true);
});

// prettier-ignore
test.each([
[
'single',
[
'bake',
'-f', path.join(fixturesDir, 'hello-bake.hcl'),
'hello'
],
],
[
'group',
[
'bake',
'-f', path.join(fixturesDir, 'hello-bake.hcl'),
'hello-all'
],
],
[
'matrix',
[
'bake',
'-f', path.join(fixturesDir, 'hello-bake.hcl'),
'hello-matrix'
],
]
])('export bake build %p', async (_, bargs) => {
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([
'--builder', process.env.CTN_BUILDER_NAME ?? 'default',
...bargs,
'--metadata-file', bake.getMetadataFilePath()
]);
await Exec.exec(buildCmd.command, buildCmd.args, {
cwd: fixturesDir
});
})()
).resolves.not.toThrow();

const metadata = bake.resolveMetadata();
expect(metadata).toBeDefined();
const buildRefs = bake.resolveRefs(metadata);
expect(buildRefs).toBeDefined();

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

expect(exportRes).toBeDefined();
expect(exportRes?.dockerbuildFilename).toBeDefined();
expect(exportRes?.dockerbuildSize).toBeDefined();
expect(fs.existsSync(exportRes?.dockerbuildFilename)).toBe(true);
});
});
39 changes: 39 additions & 0 deletions __tests__/fixtures/hello-bake.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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"]
}

target "hello-matrix" {
name = "matrix-${name}"
matrix = {
name = ["bar", "baz", "boo", "far", "faz", "foo", "aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj"]
}
dockerfile = "hello.Dockerfile"
args = {
NAME = name
}
}
20 changes: 20 additions & 0 deletions __tests__/fixtures/hello.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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
ARG TARGETPLATFORM
RUN echo "Hello $NAME from $TARGETPLATFORM"
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 624e16f

Please sign in to comment.