Skip to content

Commit 2320807

Browse files
committed
buildx(history): env var to override export build image
Signed-off-by: CrazyMax <[email protected]>
1 parent 2264b5a commit 2320807

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

__tests__/buildx/history.test.itg.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {describe, expect, test} from '@jest/globals';
17+
import {afterEach, beforeEach, describe, expect, it, jest, test} from '@jest/globals';
1818
import * as fs from 'fs';
1919
import * as path from 'path';
2020

@@ -147,3 +147,52 @@ maybe('exportBuild', () => {
147147
expect(exportRes?.summaries).toBeDefined();
148148
});
149149
});
150+
151+
maybe('exportBuild custom image', () => {
152+
const originalEnv = process.env;
153+
beforeEach(() => {
154+
jest.resetModules();
155+
process.env = {
156+
...originalEnv,
157+
DOCKER_BUILD_EXPORT_BUILD_IMAGE: 'docker.io/dockereng/export-build:0.2.2'
158+
};
159+
});
160+
afterEach(() => {
161+
process.env = originalEnv;
162+
});
163+
164+
it('with custom image', async () => {
165+
const buildx = new Buildx();
166+
const build = new Build({buildx: buildx});
167+
168+
fs.mkdirSync(tmpDir, {recursive: true});
169+
await expect(
170+
(async () => {
171+
// prettier-ignore
172+
const buildCmd = await buildx.getCommand([
173+
'--builder', process.env.CTN_BUILDER_NAME ?? 'default',
174+
'build', '-f', path.join(fixturesDir, 'hello.Dockerfile'),
175+
'--metadata-file', build.getMetadataFilePath(),
176+
fixturesDir
177+
]);
178+
await Exec.exec(buildCmd.command, buildCmd.args);
179+
})()
180+
).resolves.not.toThrow();
181+
182+
const metadata = build.resolveMetadata();
183+
expect(metadata).toBeDefined();
184+
const buildRef = build.resolveRef(metadata);
185+
expect(buildRef).toBeDefined();
186+
187+
const history = new History({buildx: buildx});
188+
const exportRes = await history.export({
189+
refs: [buildRef ?? '']
190+
});
191+
192+
expect(exportRes).toBeDefined();
193+
expect(exportRes?.dockerbuildFilename).toBeDefined();
194+
expect(exportRes?.dockerbuildSize).toBeDefined();
195+
expect(fs.existsSync(exportRes?.dockerbuildFilename)).toBe(true);
196+
expect(exportRes?.summaries).toBeDefined();
197+
});
198+
});

src/buildx/history.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export interface HistoryOpts {
3636
export class History {
3737
private readonly buildx: Buildx;
3838

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

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

0 commit comments

Comments
 (0)