Skip to content

Commit

Permalink
buildx(history): env var to override export build image
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jul 15, 2024
1 parent 2264b5a commit 8608cc9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
48 changes: 47 additions & 1 deletion __tests__/buildx/history.test.itg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {describe, expect, test} from '@jest/globals';
import {afterEach, beforeEach, describe, expect, it, jest, test} from '@jest/globals';
import * as fs from 'fs';
import * as path from 'path';

Expand Down Expand Up @@ -146,4 +146,50 @@ maybe('exportBuild', () => {
expect(fs.existsSync(exportRes?.dockerbuildFilename)).toBe(true);
expect(exportRes?.summaries).toBeDefined();
});

it('with custom export build image', async () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {
...originalEnv,
DOCKER_BUILD_EXPORT_BUILD_IMAGE: 'docker.io/dockereng/export-build:0.2.2'
};
});
afterEach(() => {
process.env = originalEnv;
});

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'),
'--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(fs.existsSync(exportRes?.dockerbuildFilename)).toBe(true);
expect(exportRes?.summaries).toBeDefined();
});
});
2 changes: 1 addition & 1 deletion __tests__/github.test.itg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {describe, expect, it, test} from '@jest/globals';
import {afterEach, beforeEach, describe, expect, it, jest, test} from '@jest/globals';
import fs from 'fs';
import * as path from 'path';

Expand Down
5 changes: 3 additions & 2 deletions src/buildx/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export interface HistoryOpts {
export class History {
private readonly buildx: Buildx;

private static readonly EXPORT_TOOL_IMAGE: string = 'docker.io/dockereng/export-build:latest';
private static readonly EXPORT_BUILD_IMAGE_DEFAULT: string = 'docker.io/dockereng/export-build:latest';
private static readonly EXPORT_BUILD_IMAGE_ENV: string = 'DOCKER_BUILD_EXPORT_BUILD_IMAGE';

constructor(opts?: HistoryOpts) {
this.buildx = opts?.buildx || new Buildx();
Expand Down Expand Up @@ -131,7 +132,7 @@ export class History {
'run', '--rm', '-i',
'-v', `${Buildx.refsDir}:/buildx-refs`,
'-v', `${outDir}:/out`,
opts.image || History.EXPORT_TOOL_IMAGE,
opts.image || process.env[History.EXPORT_BUILD_IMAGE_ENV] || History.EXPORT_BUILD_IMAGE_DEFAULT,
...ebargs
]
core.info(`[command]docker ${dockerRunArgs.join(' ')}`);
Expand Down

0 comments on commit 8608cc9

Please sign in to comment.