2024-04-18 22:33:56 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2024-06-18 04:04:28 +08:00
|
|
|
import {describe, expect, it, test} from '@jest/globals';
|
2024-05-13 22:51:00 +08:00
|
|
|
import fs from 'fs';
|
2024-04-18 22:33:56 +08:00
|
|
|
import * as path from 'path';
|
|
|
|
|
2024-05-13 22:51:00 +08:00
|
|
|
import {Buildx} from '../src/buildx/buildx';
|
|
|
|
import {Bake} from '../src/buildx/bake';
|
|
|
|
import {Build} from '../src/buildx/build';
|
|
|
|
import {Exec} from '../src/exec';
|
2024-04-18 22:33:56 +08:00
|
|
|
import {GitHub} from '../src/github';
|
2024-05-13 22:51:00 +08:00
|
|
|
import {History} from '../src/buildx/history';
|
2024-04-18 22:33:56 +08:00
|
|
|
|
|
|
|
const fixturesDir = path.join(__dirname, 'fixtures');
|
|
|
|
|
2024-05-13 22:51:00 +08:00
|
|
|
// prettier-ignore
|
|
|
|
const tmpDir = path.join(process.env.TEMP || '/tmp', 'github-jest');
|
|
|
|
|
2024-04-18 22:33:56 +08:00
|
|
|
const maybe = !process.env.GITHUB_ACTIONS || (process.env.GITHUB_ACTIONS === 'true' && process.env.ImageOS && process.env.ImageOS.startsWith('ubuntu')) ? describe : describe.skip;
|
|
|
|
|
|
|
|
maybe('uploadArtifact', () => {
|
|
|
|
it('uploads an artifact', async () => {
|
|
|
|
const res = await GitHub.uploadArtifact({
|
|
|
|
filename: path.join(fixturesDir, 'github-repo.json'),
|
|
|
|
mimeType: 'application/json',
|
|
|
|
retentionDays: 1
|
|
|
|
});
|
|
|
|
expect(res).toBeDefined();
|
|
|
|
console.log('uploadArtifactResponse', res);
|
|
|
|
expect(res?.url).toBeDefined();
|
|
|
|
});
|
|
|
|
});
|
2024-05-13 22:51:00 +08:00
|
|
|
|
|
|
|
maybe('writeBuildSummary', () => {
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
"single",
|
|
|
|
[
|
|
|
|
'build',
|
|
|
|
'-f', path.join(fixturesDir, 'hello.Dockerfile'),
|
|
|
|
fixturesDir
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"multiplatform",
|
|
|
|
[
|
|
|
|
'build',
|
|
|
|
'-f', path.join(fixturesDir, 'hello.Dockerfile'),
|
|
|
|
'--platform', 'linux/amd64,linux/arm64',
|
|
|
|
fixturesDir
|
|
|
|
],
|
|
|
|
]
|
|
|
|
])('write build summary %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(exportRes?.summaries).toBeDefined();
|
|
|
|
|
|
|
|
const uploadRes = await GitHub.uploadArtifact({
|
|
|
|
filename: exportRes?.dockerbuildFilename,
|
|
|
|
mimeType: 'application/gzip',
|
|
|
|
retentionDays: 1
|
|
|
|
});
|
|
|
|
expect(uploadRes).toBeDefined();
|
|
|
|
expect(uploadRes?.url).toBeDefined();
|
|
|
|
|
|
|
|
await GitHub.writeBuildSummary({
|
|
|
|
exportRes: exportRes,
|
|
|
|
uploadRes: uploadRes,
|
|
|
|
inputs: {
|
|
|
|
context: fixturesDir,
|
|
|
|
file: path.join(fixturesDir, 'hello.Dockerfile')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
'single',
|
2024-06-20 02:40:35 +08:00
|
|
|
path.join(fixturesDir, 'hello-bake.hcl'),
|
|
|
|
'hello'
|
2024-05-13 22:51:00 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'group',
|
2024-06-20 02:40:35 +08:00
|
|
|
path.join(fixturesDir, 'hello-bake.hcl'),
|
|
|
|
'hello-all'
|
2024-05-13 22:51:00 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'matrix',
|
2024-06-20 02:40:35 +08:00
|
|
|
path.join(fixturesDir, 'hello-bake.hcl'),
|
|
|
|
'hello-matrix'
|
2024-05-13 22:51:00 +08:00
|
|
|
]
|
2024-06-20 02:40:35 +08:00
|
|
|
])('write bake summary %p', async (_, file, target) => {
|
2024-05-13 22:51:00 +08:00
|
|
|
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',
|
2024-06-20 02:40:35 +08:00
|
|
|
'bake',
|
|
|
|
'-f', file,
|
|
|
|
target,
|
2024-05-13 22:51:00 +08:00
|
|
|
'--metadata-file', bake.getMetadataFilePath()
|
|
|
|
]);
|
|
|
|
await Exec.exec(buildCmd.command, buildCmd.args, {
|
|
|
|
cwd: fixturesDir
|
|
|
|
});
|
|
|
|
})()
|
|
|
|
).resolves.not.toThrow();
|
|
|
|
|
2024-06-20 02:40:35 +08:00
|
|
|
const definition = await bake.getDefinition(
|
|
|
|
{
|
|
|
|
files: [file],
|
|
|
|
targets: [target],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cwd: fixturesDir
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-05-13 22:51:00 +08:00
|
|
|
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(exportRes?.summaries).toBeDefined();
|
|
|
|
|
|
|
|
const uploadRes = await GitHub.uploadArtifact({
|
|
|
|
filename: exportRes?.dockerbuildFilename,
|
|
|
|
mimeType: 'application/gzip',
|
|
|
|
retentionDays: 1
|
|
|
|
});
|
|
|
|
expect(uploadRes).toBeDefined();
|
|
|
|
expect(uploadRes?.url).toBeDefined();
|
|
|
|
|
|
|
|
await GitHub.writeBuildSummary({
|
|
|
|
exportRes: exportRes,
|
|
|
|
uploadRes: uploadRes,
|
|
|
|
inputs: {
|
|
|
|
files: path.join(fixturesDir, 'hello-bake.hcl')
|
2024-06-20 02:40:35 +08:00
|
|
|
},
|
|
|
|
bakeDefinition: definition
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with dockerfile syntax issue', async () => {
|
|
|
|
const startedTime = new Date();
|
|
|
|
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',
|
|
|
|
'build',
|
|
|
|
'-f', path.join(fixturesDir, 'hello-err.Dockerfile'),
|
|
|
|
fixturesDir,
|
|
|
|
'--metadata-file', build.getMetadataFilePath()
|
|
|
|
]);
|
|
|
|
await Exec.exec(buildCmd.command, buildCmd.args);
|
|
|
|
})()
|
|
|
|
).rejects.toThrow();
|
|
|
|
|
|
|
|
const refs = Buildx.refs({
|
|
|
|
dir: Buildx.refsDir,
|
|
|
|
builderName: process.env.CTN_BUILDER_NAME ?? 'default',
|
|
|
|
since: startedTime
|
|
|
|
});
|
|
|
|
expect(refs).toBeDefined();
|
|
|
|
expect(Object.keys(refs).length).toBeGreaterThan(0);
|
|
|
|
|
|
|
|
const history = new History({buildx: buildx});
|
|
|
|
const exportRes = await history.export({
|
|
|
|
refs: [Object.keys(refs)[0] ?? '']
|
|
|
|
});
|
|
|
|
expect(exportRes).toBeDefined();
|
|
|
|
expect(exportRes?.dockerbuildFilename).toBeDefined();
|
|
|
|
expect(exportRes?.dockerbuildSize).toBeDefined();
|
|
|
|
expect(exportRes?.summaries).toBeDefined();
|
|
|
|
|
|
|
|
const uploadRes = await GitHub.uploadArtifact({
|
|
|
|
filename: exportRes?.dockerbuildFilename,
|
|
|
|
mimeType: 'application/gzip',
|
|
|
|
retentionDays: 1
|
|
|
|
});
|
|
|
|
expect(uploadRes).toBeDefined();
|
|
|
|
expect(uploadRes?.url).toBeDefined();
|
|
|
|
|
|
|
|
await GitHub.writeBuildSummary({
|
|
|
|
exportRes: exportRes,
|
|
|
|
uploadRes: uploadRes,
|
|
|
|
inputs: {
|
|
|
|
context: fixturesDir,
|
|
|
|
file: path.join(fixturesDir, 'hello-err.Dockerfile')
|
2024-05-13 22:51:00 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2024-07-02 15:48:21 +08:00
|
|
|
|
|
|
|
it('without build record', async () => {
|
|
|
|
const startedTime = new Date();
|
|
|
|
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',
|
|
|
|
'build',
|
|
|
|
'-f', path.join(fixturesDir, 'hello.Dockerfile'),
|
|
|
|
fixturesDir,
|
|
|
|
'--metadata-file', build.getMetadataFilePath()
|
|
|
|
]);
|
|
|
|
await Exec.exec(buildCmd.command, buildCmd.args);
|
|
|
|
})()
|
|
|
|
).resolves.not.toThrow();
|
|
|
|
|
|
|
|
const refs = Buildx.refs({
|
|
|
|
dir: Buildx.refsDir,
|
|
|
|
builderName: process.env.CTN_BUILDER_NAME ?? 'default',
|
|
|
|
since: startedTime
|
|
|
|
});
|
|
|
|
expect(refs).toBeDefined();
|
|
|
|
expect(Object.keys(refs).length).toBeGreaterThan(0);
|
|
|
|
|
|
|
|
const history = new History({buildx: buildx});
|
|
|
|
const exportRes = await history.export({
|
|
|
|
refs: [Object.keys(refs)[0] ?? '']
|
|
|
|
});
|
|
|
|
expect(exportRes).toBeDefined();
|
|
|
|
expect(exportRes?.dockerbuildFilename).toBeDefined();
|
|
|
|
expect(exportRes?.dockerbuildSize).toBeDefined();
|
|
|
|
expect(exportRes?.summaries).toBeDefined();
|
|
|
|
|
|
|
|
await GitHub.writeBuildSummary({
|
|
|
|
exportRes: exportRes,
|
|
|
|
inputs: {
|
|
|
|
context: fixturesDir,
|
|
|
|
file: path.join(fixturesDir, 'hello.Dockerfile')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2024-05-13 22:51:00 +08:00
|
|
|
});
|
2024-06-18 21:26:43 +08:00
|
|
|
|
|
|
|
maybe('annotateBuildWarnings', () => {
|
|
|
|
it('annoate lint issues', 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([
|
|
|
|
'--builder', process.env.CTN_BUILDER_NAME ?? 'default',
|
|
|
|
'build',
|
|
|
|
'-f', path.join(fixturesDir, 'lint.Dockerfile'),
|
|
|
|
fixturesDir,
|
|
|
|
'--metadata-file', build.getMetadataFilePath()
|
|
|
|
]);
|
|
|
|
await Exec.exec(buildCmd.command, buildCmd.args, {
|
|
|
|
env: Object.assign({}, process.env, {
|
|
|
|
BUILDX_METADATA_WARNINGS: 'true'
|
|
|
|
}) as {
|
|
|
|
[key: string]: string;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})()
|
|
|
|
).resolves.not.toThrow();
|
|
|
|
|
|
|
|
const metadata = build.resolveMetadata();
|
|
|
|
expect(metadata).toBeDefined();
|
|
|
|
const buildRef = build.resolveRef(metadata);
|
|
|
|
expect(buildRef).toBeDefined();
|
|
|
|
const buildWarnings = build.resolveWarnings(metadata);
|
|
|
|
expect(buildWarnings).toBeDefined();
|
|
|
|
|
|
|
|
await GitHub.annotateBuildWarnings(path.join(fixturesDir, 'lint.Dockerfile'), buildWarnings);
|
|
|
|
});
|
|
|
|
});
|