buildx: generate random metadata filename

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-04-26 13:41:03 +02:00
parent 62e3923775
commit 5820a0ba86
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4
5 changed files with 59 additions and 45 deletions

View File

@ -59,19 +59,17 @@ afterEach(() => {
describe('resolveMetadata', () => {
it('matches', async () => {
const metadataFile = Bake.getMetadataFilePath();
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
const expected = Bake.resolveMetadata();
expect(expected).toEqual(metadata as BakeMetadata);
const bake = new Bake();
await fs.writeFileSync(bake.getMetadataFilePath(), JSON.stringify(metadata));
expect(bake.resolveMetadata()).toEqual(metadata as BakeMetadata);
});
});
describe('resolveRefs', () => {
it('matches', async () => {
const metadataFile = Bake.getMetadataFilePath();
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
const expected = Bake.resolveRefs();
expect(expected).toEqual(['default/default/7frbdw1fmfozgtqavghowsepk', 'default/default/onic7g2axylf56rxetob7qruy']);
const bake = new Bake();
await fs.writeFileSync(bake.getMetadataFilePath(), JSON.stringify(metadata));
expect(bake.resolveRefs()).toEqual(['default/default/7frbdw1fmfozgtqavghowsepk', 'default/default/onic7g2axylf56rxetob7qruy']);
});
});

View File

@ -56,37 +56,33 @@ afterEach(() => {
describe('resolveImageID', () => {
it('matches', async () => {
const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
const imageIDFile = Build.getImageIDFilePath();
await fs.writeFileSync(imageIDFile, imageID);
const expected = Build.resolveImageID();
expect(expected).toEqual(imageID);
const build = new Build();
await fs.writeFileSync(build.getImageIDFilePath(), imageID);
expect(build.resolveImageID()).toEqual(imageID);
});
});
describe('resolveMetadata', () => {
it('matches', async () => {
const metadataFile = Build.getMetadataFilePath();
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
const expected = Build.resolveMetadata();
expect(expected).toEqual(metadata);
const build = new Build();
await fs.writeFileSync(build.getMetadataFilePath(), JSON.stringify(metadata));
expect(build.resolveMetadata()).toEqual(metadata);
});
});
describe('resolveRef', () => {
it('matches', async () => {
const metadataFile = Build.getMetadataFilePath();
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
const expected = Build.resolveRef();
expect(expected).toEqual('default/default/n6ibcp9b2pw108rrz7ywdznvo');
const build = new Build();
await fs.writeFileSync(build.getMetadataFilePath(), JSON.stringify(metadata));
expect(build.resolveRef()).toEqual('default/default/n6ibcp9b2pw108rrz7ywdznvo');
});
});
describe('resolveDigest', () => {
it('matches', async () => {
const metadataFile = Build.getMetadataFilePath();
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
const expected = Build.resolveDigest();
expect(expected).toEqual('sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c');
const build = new Build();
await fs.writeFileSync(build.getMetadataFilePath(), JSON.stringify(metadata));
expect(build.resolveDigest()).toEqual('sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c');
});
});

View File

@ -46,17 +46,19 @@ export interface BakeCmdOpts {
export class Bake {
private readonly buildx: Buildx;
private readonly metadataFilename: string;
constructor(opts?: BakeOpts) {
this.buildx = opts?.buildx || new Buildx();
this.metadataFilename = `bake-metadata-${Util.generateRandomString()}.json`;
}
public static getMetadataFilePath(): string {
return path.join(Context.tmpDir(), 'bake-metadata.json');
public getMetadataFilePath(): string {
return path.join(Context.tmpDir(), this.metadataFilename);
}
public static resolveMetadata(): BakeMetadata | undefined {
const metadataFile = Bake.getMetadataFilePath();
public resolveMetadata(): BakeMetadata | undefined {
const metadataFile = this.getMetadataFilePath();
if (!fs.existsSync(metadataFile)) {
return undefined;
}
@ -67,8 +69,8 @@ export class Bake {
return <BakeMetadata>JSON.parse(content);
}
public static resolveRefs(): Array<string> | undefined {
const metadata = Bake.resolveMetadata();
public resolveRefs(): Array<string> | undefined {
const metadata = this.resolveMetadata();
if (!metadata) {
return undefined;
}

View File

@ -19,31 +19,46 @@ import path from 'path';
import * as core from '@actions/core';
import {parse} from 'csv-parse/sync';
import {Buildx} from './buildx';
import {Context} from '../context';
import {GitHub} from '../github';
import {Util} from '../util';
import {BuildMetadata} from '../types/build';
export interface BuildOpts {
buildx?: Buildx;
}
export class Build {
public static getImageIDFilePath(): string {
return path.join(Context.tmpDir(), 'build-iidfile.txt');
private readonly buildx: Buildx;
private readonly iidFilename: string;
private readonly metadataFilename: string;
constructor(opts?: BuildOpts) {
this.buildx = opts?.buildx || new Buildx();
this.iidFilename = `build-iidfile-${Util.generateRandomString()}.txt`;
this.metadataFilename = `build-metadata-${Util.generateRandomString()}.json`;
}
public static resolveImageID(): string | undefined {
const iidFile = Build.getImageIDFilePath();
public getImageIDFilePath(): string {
return path.join(Context.tmpDir(), this.iidFilename);
}
public resolveImageID(): string | undefined {
const iidFile = this.getImageIDFilePath();
if (!fs.existsSync(iidFile)) {
return undefined;
}
return fs.readFileSync(iidFile, {encoding: 'utf-8'}).trim();
}
public static getMetadataFilePath(): string {
return path.join(Context.tmpDir(), 'build-metadata.json');
public getMetadataFilePath(): string {
return path.join(Context.tmpDir(), this.metadataFilename);
}
public static resolveMetadata(): BuildMetadata | undefined {
const metadataFile = Build.getMetadataFilePath();
public resolveMetadata(): BuildMetadata | undefined {
const metadataFile = this.getMetadataFilePath();
if (!fs.existsSync(metadataFile)) {
return undefined;
}
@ -54,8 +69,8 @@ export class Build {
return <BuildMetadata>JSON.parse(content);
}
public static resolveRef(): string | undefined {
const metadata = Build.resolveMetadata();
public resolveRef(): string | undefined {
const metadata = this.resolveMetadata();
if (!metadata) {
return undefined;
}
@ -65,8 +80,8 @@ export class Build {
return undefined;
}
public static resolveDigest(): string | undefined {
const metadata = Build.resolveMetadata();
public resolveDigest(): string | undefined {
const metadata = this.resolveMetadata();
if (!metadata) {
return undefined;
}

View File

@ -15,8 +15,9 @@
*/
import {Buildx} from './buildx/buildx';
import {Build as BuildxBuild} from './buildx/build';
import {Bake as BuildxBake} from './buildx/bake';
import {Install as BuildxInstall} from './buildx/install';
import {Bake} from './buildx/bake';
import {Builder} from './buildx/builder';
import {BuildKit} from './buildkit/buildkit';
import {GitHub} from './github';
@ -32,16 +33,18 @@ export interface ToolkitOpts {
export class Toolkit {
public github: GitHub;
public buildx: Buildx;
public buildxBuild: BuildxBuild;
public buildxBake: BuildxBake;
public buildxInstall: BuildxInstall;
public bake: Bake;
public builder: Builder;
public buildkit: BuildKit;
constructor(opts: ToolkitOpts = {}) {
this.github = new GitHub({token: opts.githubToken});
this.buildx = new Buildx();
this.buildxBuild = new BuildxBuild({buildx: this.buildx});
this.buildxBake = new BuildxBake({buildx: this.buildx});
this.buildxInstall = new BuildxInstall();
this.bake = new Bake({buildx: this.buildx});
this.builder = new Builder({buildx: this.buildx});
this.buildkit = new BuildKit({buildx: this.buildx});
}