Skip to content

Commit 42e59b7

Browse files
committed
bake: handle build checks from metadata
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 477e96d commit 42e59b7

File tree

8 files changed

+601
-13
lines changed

8 files changed

+601
-13
lines changed

__tests__/buildx/bake.test.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,9 @@ import {BuildMetadata} from '../../src/types/buildx/build';
2828

2929
const fixturesDir = path.join(__dirname, '..', 'fixtures');
3030
// prettier-ignore
31-
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-inputs-jest');
31+
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-bake-jest');
3232
const tmpName = path.join(tmpDir, '.tmpname-jest');
33-
const metadata: BuildMetadata = {
34-
app: {
35-
'buildx.build.ref': 'default/default/7frbdw1fmfozgtqavghowsepk'
36-
},
37-
db: {
38-
'buildx.build.ref': 'default/default/onic7g2axylf56rxetob7qruy'
39-
}
40-
};
33+
const metadata = JSON.parse(fs.readFileSync(path.join(fixturesDir, 'metadata-bake.json'), 'utf-8'));
4134

4235
jest.spyOn(Context, 'tmpDir').mockImplementation((): string => {
4336
if (!fs.existsSync(tmpDir)) {
@@ -66,7 +59,17 @@ describe('resolveRefs', () => {
6659
it('matches', async () => {
6760
const bake = new Bake();
6861
fs.writeFileSync(bake.getMetadataFilePath(), JSON.stringify(metadata));
69-
expect(bake.resolveRefs()).toEqual(['default/default/7frbdw1fmfozgtqavghowsepk', 'default/default/onic7g2axylf56rxetob7qruy']);
62+
expect(bake.resolveRefs()).toEqual(['default/default/x3tig9yrbzg2bp0ahn840m9hs', 'default/default/f9i6og3j529lrezk83aw9k8fr', 'default/default/yfq4itxr5kgustkcmp8jr4b9m']);
63+
});
64+
});
65+
66+
describe('resolveWarnings', () => {
67+
it('matches', async () => {
68+
const bake = new Bake();
69+
fs.writeFileSync(bake.getMetadataFilePath(), JSON.stringify(metadata));
70+
const warnings = bake.resolveWarnings();
71+
expect(warnings).toBeDefined();
72+
expect(warnings?.length).toEqual(13);
7073
});
7174
});
7275

__tests__/buildx/build.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import {Build} from '../../src/buildx/build';
2424

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

3131
jest.spyOn(Context, 'tmpDir').mockImplementation((): string => {
3232
if (!fs.existsSync(tmpDir)) {

__tests__/buildx/buildx.test.itg.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as core from '@actions/core';
2121

2222
import {Buildx} from '../../src/buildx/buildx';
2323
import {Build} from '../../src/buildx/build';
24+
import {Bake} from '../../src/buildx/bake';
2425
import {Exec} from '../../src/exec';
2526

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

3334
maybe('convertWarningsToGitHubAnnotations', () => {
34-
it('annotate lint issues', async () => {
35+
it('build lint issues', async () => {
3536
const buildx = new Buildx();
3637
const build = new Build({buildx: buildx});
3738

@@ -71,4 +72,46 @@ maybe('convertWarningsToGitHubAnnotations', () => {
7172
core.warning(annotation.message, annotation);
7273
}
7374
});
75+
76+
it('bake lint issues', async () => {
77+
const buildx = new Buildx();
78+
const bake = new Bake({buildx: buildx});
79+
80+
fs.mkdirSync(tmpDir, {recursive: true});
81+
await expect(
82+
(async () => {
83+
// prettier-ignore
84+
const buildCmd = await buildx.getCommand([
85+
'--builder', process.env.CTN_BUILDER_NAME ?? 'default',
86+
'bake',
87+
'-f', path.join(fixturesDir, 'lint.hcl'),
88+
'--metadata-file', bake.getMetadataFilePath()
89+
]);
90+
await Exec.exec(buildCmd.command, buildCmd.args, {
91+
cwd: fixturesDir,
92+
env: Object.assign({}, process.env, {
93+
BUILDX_METADATA_WARNINGS: 'true'
94+
}) as {
95+
[key: string]: string;
96+
}
97+
});
98+
})()
99+
).resolves.not.toThrow();
100+
101+
const metadata = bake.resolveMetadata();
102+
expect(metadata).toBeDefined();
103+
const buildRefs = bake.resolveRefs(metadata);
104+
expect(buildRefs).toBeDefined();
105+
expect(buildRefs?.length).toEqual(3);
106+
const buildWarnings = bake.resolveWarnings(metadata);
107+
expect(buildWarnings).toBeDefined();
108+
109+
const annotations = await Buildx.convertWarningsToGitHubAnnotations(buildWarnings ?? [], buildRefs ?? []);
110+
expect(annotations).toBeDefined();
111+
expect(annotations?.length).toBeGreaterThan(0);
112+
113+
for (const annotation of annotations ?? []) {
114+
core.warning(annotation.message, annotation);
115+
}
116+
});
74117
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# syntax=docker/dockerfile-upstream:master
2+
3+
# Copyright 2024 actions-toolkit authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
frOM busybox as base
18+
cOpy lint-other.Dockerfile .
19+
20+
froM busybox aS notused
21+
COPY lint-other.Dockerfile .
22+
23+
from scratch
24+
COPy --from=base \
25+
/lint-other.Dockerfile \
26+
/

__tests__/fixtures/lint.hcl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2024 actions-toolkit authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
group "default" {
16+
targets = ["lint", "lint-other", "lint-inline"]
17+
}
18+
target "lint" {
19+
dockerfile = "lint.Dockerfile"
20+
}
21+
target "lint-other" {
22+
dockerfile = "lint-other.Dockerfile"
23+
}
24+
target "lint-inline" {
25+
dockerfile-inline = "FRoM alpine\nENTRYPOINT [\"echo\", \"hello\"]"
26+
}

0 commit comments

Comments
 (0)