mirror of
https://github.com/docker/actions-toolkit.git
synced 2024-11-23 03:16:09 +08:00
Merge pull request #322 from crazy-max/metadata-file-name
buildx: generate random metadata filename
This commit is contained in:
commit
ae20b6fb50
@ -59,19 +59,17 @@ afterEach(() => {
|
|||||||
|
|
||||||
describe('resolveMetadata', () => {
|
describe('resolveMetadata', () => {
|
||||||
it('matches', async () => {
|
it('matches', async () => {
|
||||||
const metadataFile = Bake.getMetadataFilePath();
|
const bake = new Bake();
|
||||||
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
|
await fs.writeFileSync(bake.getMetadataFilePath(), JSON.stringify(metadata));
|
||||||
const expected = Bake.resolveMetadata();
|
expect(bake.resolveMetadata()).toEqual(metadata as BakeMetadata);
|
||||||
expect(expected).toEqual(metadata as BakeMetadata);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('resolveRefs', () => {
|
describe('resolveRefs', () => {
|
||||||
it('matches', async () => {
|
it('matches', async () => {
|
||||||
const metadataFile = Bake.getMetadataFilePath();
|
const bake = new Bake();
|
||||||
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
|
await fs.writeFileSync(bake.getMetadataFilePath(), JSON.stringify(metadata));
|
||||||
const expected = Bake.resolveRefs();
|
expect(bake.resolveRefs()).toEqual(['default/default/7frbdw1fmfozgtqavghowsepk', 'default/default/onic7g2axylf56rxetob7qruy']);
|
||||||
expect(expected).toEqual(['default/default/7frbdw1fmfozgtqavghowsepk', 'default/default/onic7g2axylf56rxetob7qruy']);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -56,37 +56,33 @@ afterEach(() => {
|
|||||||
describe('resolveImageID', () => {
|
describe('resolveImageID', () => {
|
||||||
it('matches', async () => {
|
it('matches', async () => {
|
||||||
const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
|
const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
|
||||||
const imageIDFile = Build.getImageIDFilePath();
|
const build = new Build();
|
||||||
await fs.writeFileSync(imageIDFile, imageID);
|
await fs.writeFileSync(build.getImageIDFilePath(), imageID);
|
||||||
const expected = Build.resolveImageID();
|
expect(build.resolveImageID()).toEqual(imageID);
|
||||||
expect(expected).toEqual(imageID);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('resolveMetadata', () => {
|
describe('resolveMetadata', () => {
|
||||||
it('matches', async () => {
|
it('matches', async () => {
|
||||||
const metadataFile = Build.getMetadataFilePath();
|
const build = new Build();
|
||||||
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
|
await fs.writeFileSync(build.getMetadataFilePath(), JSON.stringify(metadata));
|
||||||
const expected = Build.resolveMetadata();
|
expect(build.resolveMetadata()).toEqual(metadata);
|
||||||
expect(expected).toEqual(metadata);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('resolveRef', () => {
|
describe('resolveRef', () => {
|
||||||
it('matches', async () => {
|
it('matches', async () => {
|
||||||
const metadataFile = Build.getMetadataFilePath();
|
const build = new Build();
|
||||||
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
|
await fs.writeFileSync(build.getMetadataFilePath(), JSON.stringify(metadata));
|
||||||
const expected = Build.resolveRef();
|
expect(build.resolveRef()).toEqual('default/default/n6ibcp9b2pw108rrz7ywdznvo');
|
||||||
expect(expected).toEqual('default/default/n6ibcp9b2pw108rrz7ywdznvo');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('resolveDigest', () => {
|
describe('resolveDigest', () => {
|
||||||
it('matches', async () => {
|
it('matches', async () => {
|
||||||
const metadataFile = Build.getMetadataFilePath();
|
const build = new Build();
|
||||||
await fs.writeFileSync(metadataFile, JSON.stringify(metadata));
|
await fs.writeFileSync(build.getMetadataFilePath(), JSON.stringify(metadata));
|
||||||
const expected = Build.resolveDigest();
|
expect(build.resolveDigest()).toEqual('sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c');
|
||||||
expect(expected).toEqual('sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -335,6 +335,22 @@ describe('formatFileSize', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('generateRandomString', () => {
|
||||||
|
it('should generate a random string of default length 10', async () => {
|
||||||
|
const res = Util.generateRandomString();
|
||||||
|
expect(typeof res).toBe('string');
|
||||||
|
expect(res.length).toBe(10);
|
||||||
|
expect(/^[0-9a-f]+$/i.test(res)).toBe(true);
|
||||||
|
});
|
||||||
|
it('should generate a random string of specified length', async () => {
|
||||||
|
const length = 15;
|
||||||
|
const res = Util.generateRandomString(length);
|
||||||
|
expect(typeof res).toBe('string');
|
||||||
|
expect(res.length).toBe(15);
|
||||||
|
expect(/^[0-9a-f]+$/i.test(res)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89
|
// See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89
|
||||||
function getInputName(name: string): string {
|
function getInputName(name: string): string {
|
||||||
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
|
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
|
||||||
|
@ -46,17 +46,19 @@ export interface BakeCmdOpts {
|
|||||||
|
|
||||||
export class Bake {
|
export class Bake {
|
||||||
private readonly buildx: Buildx;
|
private readonly buildx: Buildx;
|
||||||
|
private readonly metadataFilename: string;
|
||||||
|
|
||||||
constructor(opts?: BakeOpts) {
|
constructor(opts?: BakeOpts) {
|
||||||
this.buildx = opts?.buildx || new Buildx();
|
this.buildx = opts?.buildx || new Buildx();
|
||||||
|
this.metadataFilename = `bake-metadata-${Util.generateRandomString()}.json`;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static getMetadataFilePath(): string {
|
public getMetadataFilePath(): string {
|
||||||
return path.join(Context.tmpDir(), 'metadata-file');
|
return path.join(Context.tmpDir(), this.metadataFilename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static resolveMetadata(): BakeMetadata | undefined {
|
public resolveMetadata(): BakeMetadata | undefined {
|
||||||
const metadataFile = Bake.getMetadataFilePath();
|
const metadataFile = this.getMetadataFilePath();
|
||||||
if (!fs.existsSync(metadataFile)) {
|
if (!fs.existsSync(metadataFile)) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -67,11 +69,13 @@ export class Bake {
|
|||||||
return <BakeMetadata>JSON.parse(content);
|
return <BakeMetadata>JSON.parse(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static resolveRefs(): Array<string> | undefined {
|
public resolveRefs(metadata?: BakeMetadata): Array<string> | undefined {
|
||||||
const metadata = Bake.resolveMetadata();
|
if (!metadata) {
|
||||||
|
metadata = this.resolveMetadata();
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const refs = new Array<string>();
|
const refs = new Array<string>();
|
||||||
for (const key in metadata) {
|
for (const key in metadata) {
|
||||||
if ('buildx.build.ref' in metadata[key]) {
|
if ('buildx.build.ref' in metadata[key]) {
|
||||||
|
@ -19,31 +19,46 @@ import path from 'path';
|
|||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import {parse} from 'csv-parse/sync';
|
import {parse} from 'csv-parse/sync';
|
||||||
|
|
||||||
|
import {Buildx} from './buildx';
|
||||||
import {Context} from '../context';
|
import {Context} from '../context';
|
||||||
import {GitHub} from '../github';
|
import {GitHub} from '../github';
|
||||||
import {Util} from '../util';
|
import {Util} from '../util';
|
||||||
|
|
||||||
import {BuildMetadata} from '../types/build';
|
import {BuildMetadata} from '../types/build';
|
||||||
|
|
||||||
|
export interface BuildOpts {
|
||||||
|
buildx?: Buildx;
|
||||||
|
}
|
||||||
|
|
||||||
export class Build {
|
export class Build {
|
||||||
public static getImageIDFilePath(): string {
|
private readonly buildx: Buildx;
|
||||||
return path.join(Context.tmpDir(), 'iidfile');
|
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 getMetadataFilePath(): string {
|
public getImageIDFilePath(): string {
|
||||||
return path.join(Context.tmpDir(), 'metadata-file');
|
return path.join(Context.tmpDir(), this.iidFilename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static resolveImageID(): string | undefined {
|
public resolveImageID(): string | undefined {
|
||||||
const iidFile = Build.getImageIDFilePath();
|
const iidFile = this.getImageIDFilePath();
|
||||||
if (!fs.existsSync(iidFile)) {
|
if (!fs.existsSync(iidFile)) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return fs.readFileSync(iidFile, {encoding: 'utf-8'}).trim();
|
return fs.readFileSync(iidFile, {encoding: 'utf-8'}).trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static resolveMetadata(): BuildMetadata | undefined {
|
public getMetadataFilePath(): string {
|
||||||
const metadataFile = Build.getMetadataFilePath();
|
return path.join(Context.tmpDir(), this.metadataFilename);
|
||||||
|
}
|
||||||
|
|
||||||
|
public resolveMetadata(): BuildMetadata | undefined {
|
||||||
|
const metadataFile = this.getMetadataFilePath();
|
||||||
if (!fs.existsSync(metadataFile)) {
|
if (!fs.existsSync(metadataFile)) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -54,22 +69,26 @@ export class Build {
|
|||||||
return <BuildMetadata>JSON.parse(content);
|
return <BuildMetadata>JSON.parse(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static resolveRef(): string | undefined {
|
public resolveRef(metadata?: BuildMetadata): string | undefined {
|
||||||
const metadata = Build.resolveMetadata();
|
if (!metadata) {
|
||||||
|
metadata = this.resolveMetadata();
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if ('buildx.build.ref' in metadata) {
|
if ('buildx.build.ref' in metadata) {
|
||||||
return metadata['buildx.build.ref'];
|
return metadata['buildx.build.ref'];
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static resolveDigest(): string | undefined {
|
public resolveDigest(metadata?: BuildMetadata): string | undefined {
|
||||||
const metadata = Build.resolveMetadata();
|
if (!metadata) {
|
||||||
|
metadata = this.resolveMetadata();
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if ('containerimage.digest' in metadata) {
|
if ('containerimage.digest' in metadata) {
|
||||||
return metadata['containerimage.digest'];
|
return metadata['containerimage.digest'];
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {Buildx} from './buildx/buildx';
|
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 {Install as BuildxInstall} from './buildx/install';
|
||||||
import {Bake} from './buildx/bake';
|
|
||||||
import {Builder} from './buildx/builder';
|
import {Builder} from './buildx/builder';
|
||||||
import {BuildKit} from './buildkit/buildkit';
|
import {BuildKit} from './buildkit/buildkit';
|
||||||
import {GitHub} from './github';
|
import {GitHub} from './github';
|
||||||
@ -32,16 +33,18 @@ export interface ToolkitOpts {
|
|||||||
export class Toolkit {
|
export class Toolkit {
|
||||||
public github: GitHub;
|
public github: GitHub;
|
||||||
public buildx: Buildx;
|
public buildx: Buildx;
|
||||||
|
public buildxBuild: BuildxBuild;
|
||||||
|
public buildxBake: BuildxBake;
|
||||||
public buildxInstall: BuildxInstall;
|
public buildxInstall: BuildxInstall;
|
||||||
public bake: Bake;
|
|
||||||
public builder: Builder;
|
public builder: Builder;
|
||||||
public buildkit: BuildKit;
|
public buildkit: BuildKit;
|
||||||
|
|
||||||
constructor(opts: ToolkitOpts = {}) {
|
constructor(opts: ToolkitOpts = {}) {
|
||||||
this.github = new GitHub({token: opts.githubToken});
|
this.github = new GitHub({token: opts.githubToken});
|
||||||
this.buildx = new Buildx();
|
this.buildx = new Buildx();
|
||||||
|
this.buildxBuild = new BuildxBuild({buildx: this.buildx});
|
||||||
|
this.buildxBake = new BuildxBake({buildx: this.buildx});
|
||||||
this.buildxInstall = new BuildxInstall();
|
this.buildxInstall = new BuildxInstall();
|
||||||
this.bake = new Bake({buildx: this.buildx});
|
|
||||||
this.builder = new Builder({buildx: this.buildx});
|
this.builder = new Builder({buildx: this.buildx});
|
||||||
this.buildkit = new BuildKit({buildx: this.buildx});
|
this.buildkit = new BuildKit({buildx: this.buildx});
|
||||||
}
|
}
|
||||||
|
@ -174,4 +174,9 @@ export class Util {
|
|||||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static generateRandomString(length = 10) {
|
||||||
|
const bytes = crypto.randomBytes(Math.ceil(length / 2));
|
||||||
|
return bytes.toString('hex').slice(0, length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user