Skip to content

Commit

Permalink
Merge pull request #389 from crazy-max/bake-warnings
Browse files Browse the repository at this point in the history
bake: handle build checks from metadata
  • Loading branch information
crazy-max authored Jul 31, 2024
2 parents 477e96d + 42e59b7 commit a8cf500
Show file tree
Hide file tree
Showing 8 changed files with 601 additions and 13 deletions.
23 changes: 13 additions & 10 deletions __tests__/buildx/bake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,9 @@ import {BuildMetadata} from '../../src/types/buildx/build';

const fixturesDir = path.join(__dirname, '..', 'fixtures');
// prettier-ignore
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-inputs-jest');
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-bake-jest');
const tmpName = path.join(tmpDir, '.tmpname-jest');
const metadata: BuildMetadata = {
app: {
'buildx.build.ref': 'default/default/7frbdw1fmfozgtqavghowsepk'
},
db: {
'buildx.build.ref': 'default/default/onic7g2axylf56rxetob7qruy'
}
};
const metadata = JSON.parse(fs.readFileSync(path.join(fixturesDir, 'metadata-bake.json'), 'utf-8'));

jest.spyOn(Context, 'tmpDir').mockImplementation((): string => {
if (!fs.existsSync(tmpDir)) {
Expand Down Expand Up @@ -66,7 +59,17 @@ describe('resolveRefs', () => {
it('matches', async () => {
const bake = new Bake();
fs.writeFileSync(bake.getMetadataFilePath(), JSON.stringify(metadata));
expect(bake.resolveRefs()).toEqual(['default/default/7frbdw1fmfozgtqavghowsepk', 'default/default/onic7g2axylf56rxetob7qruy']);
expect(bake.resolveRefs()).toEqual(['default/default/x3tig9yrbzg2bp0ahn840m9hs', 'default/default/f9i6og3j529lrezk83aw9k8fr', 'default/default/yfq4itxr5kgustkcmp8jr4b9m']);
});
});

describe('resolveWarnings', () => {
it('matches', async () => {
const bake = new Bake();
fs.writeFileSync(bake.getMetadataFilePath(), JSON.stringify(metadata));
const warnings = bake.resolveWarnings();
expect(warnings).toBeDefined();
expect(warnings?.length).toEqual(13);
});
});

Expand Down
4 changes: 2 additions & 2 deletions __tests__/buildx/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import {Build} from '../../src/buildx/build';

const fixturesDir = path.join(__dirname, '..', 'fixtures');
// prettier-ignore
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-inputs-jest');
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-build-jest');
const tmpName = path.join(tmpDir, '.tmpname-jest');
const metadata = JSON.parse(fs.readFileSync(path.join(fixturesDir, 'metadata.json'), 'utf-8'));
const metadata = JSON.parse(fs.readFileSync(path.join(fixturesDir, 'metadata-build.json'), 'utf-8'));

jest.spyOn(Context, 'tmpDir').mockImplementation((): string => {
if (!fs.existsSync(tmpDir)) {
Expand Down
45 changes: 44 additions & 1 deletion __tests__/buildx/buildx.test.itg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as core from '@actions/core';

import {Buildx} from '../../src/buildx/buildx';
import {Build} from '../../src/buildx/build';
import {Bake} from '../../src/buildx/bake';
import {Exec} from '../../src/exec';

const fixturesDir = path.join(__dirname, '..', 'fixtures');
Expand All @@ -31,7 +32,7 @@ const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-jest');
const maybe = !process.env.GITHUB_ACTIONS || (process.env.GITHUB_ACTIONS === 'true' && process.env.ImageOS && process.env.ImageOS.startsWith('ubuntu')) ? describe : describe.skip;

maybe('convertWarningsToGitHubAnnotations', () => {
it('annotate lint issues', async () => {
it('build lint issues', async () => {
const buildx = new Buildx();
const build = new Build({buildx: buildx});

Expand Down Expand Up @@ -71,4 +72,46 @@ maybe('convertWarningsToGitHubAnnotations', () => {
core.warning(annotation.message, annotation);
}
});

it('bake lint issues', 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([
'--builder', process.env.CTN_BUILDER_NAME ?? 'default',
'bake',
'-f', path.join(fixturesDir, 'lint.hcl'),
'--metadata-file', bake.getMetadataFilePath()
]);
await Exec.exec(buildCmd.command, buildCmd.args, {
cwd: fixturesDir,
env: Object.assign({}, process.env, {
BUILDX_METADATA_WARNINGS: 'true'
}) as {
[key: string]: string;
}
});
})()
).resolves.not.toThrow();

const metadata = bake.resolveMetadata();
expect(metadata).toBeDefined();
const buildRefs = bake.resolveRefs(metadata);
expect(buildRefs).toBeDefined();
expect(buildRefs?.length).toEqual(3);
const buildWarnings = bake.resolveWarnings(metadata);
expect(buildWarnings).toBeDefined();

const annotations = await Buildx.convertWarningsToGitHubAnnotations(buildWarnings ?? [], buildRefs ?? []);
expect(annotations).toBeDefined();
expect(annotations?.length).toBeGreaterThan(0);

for (const annotation of annotations ?? []) {
core.warning(annotation.message, annotation);
}
});
});
26 changes: 26 additions & 0 deletions __tests__/fixtures/lint-other.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# syntax=docker/dockerfile-upstream:master

# 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 as base

Check warning on line 17 in __tests__/fixtures/lint-other.Dockerfile

View workflow job for this annotation

GitHub Actions / test-itg (buildx/buildx.test.itg.ts, ubuntu-latest)

All commands within the Dockerfile should use the same casing (either upper or lower)

ConsistentInstructionCasing: Command 'frOM' should match the case of the command majority (uppercase) More info: https://docs.docker.com/go/dockerfile/rule/consistent-instruction-casing/
cOpy lint-other.Dockerfile .

froM busybox aS notused
COPY lint-other.Dockerfile .

from scratch
COPy --from=base \
/lint-other.Dockerfile \
/
26 changes: 26 additions & 0 deletions __tests__/fixtures/lint.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.

group "default" {
targets = ["lint", "lint-other", "lint-inline"]
}
target "lint" {
dockerfile = "lint.Dockerfile"
}
target "lint-other" {
dockerfile = "lint-other.Dockerfile"
}
target "lint-inline" {
dockerfile-inline = "FRoM alpine\nENTRYPOINT [\"echo\", \"hello\"]"
}
Loading

0 comments on commit a8cf500

Please sign in to comment.