Skip to content

Commit 4ba329e

Browse files
hashharcrazy-max
authored andcommitted
Remove aliases created by buildx when installing by default
If the action is configured to install buildx by default using the input then docker buildx sets up docker build as an alias for buildx making all docker build calls use the buildx builder instead of traditional builders. The action didn't perform cleanup in this case to uninstall the aliases which meant that any future workflows running on same GitHub Actions runner would get the buildx builders even if it did not explicitly request it. This commit tracks if the aliases were installed and removes them during post step of the action if so. Signed-off-by: Ashhar Hasan <hashhar_dev@outlook.com>
1 parent e600775 commit 4ba329e

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/main.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ actionsToolkit.run(
179179
throw new Error(`Cannot set buildx as default builder without the Docker CLI`);
180180
}
181181
await core.group(`Setting buildx as default builder`, async () => {
182+
stateHelper.setBuildxIsDefaultBuilder(true);
182183
const installCmd = await toolkit.buildx.getCommand(['install']);
183184
await Exec.getExecOutput(installCmd.command, installCmd.args, {
184185
ignoreReturnCode: true
@@ -279,5 +280,17 @@ actionsToolkit.run(
279280
fs.rmSync(stateHelper.certsDir, {recursive: true});
280281
});
281282
}
283+
284+
if (stateHelper.buildxIsDefaultBuilder) {
285+
await core.group(`Restoring default builder`, async () => {
286+
await Exec.getExecOutput('docker', ['buildx', 'uninstall'], {
287+
ignoreReturnCode: true
288+
}).then(res => {
289+
if (res.stderr.length > 0 && res.exitCode != 0) {
290+
core.warning(`${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
291+
}
292+
});
293+
});
294+
}
282295
}
283296
);

src/state-helper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const containerName = process.env['STATE_containerName'] || '';
88
export const certsDir = process.env['STATE_certsDir'] || '';
99
export const tmpDockerContext = process.env['STATE_tmpDockerContext'] || '';
1010
export const cleanup = /true/i.test(process.env['STATE_cleanup'] || '');
11+
export const buildxIsDefaultBuilder = /true/i.test(process.env['STATE_buildxIsDefaultBuilder'] || '');
1112

1213
export function setDebug(debug: string) {
1314
core.saveState('isDebug', debug);
@@ -40,3 +41,7 @@ export function setTmpDockerContext(tmpDockerContext: string) {
4041
export function setCleanup(cleanup: boolean) {
4142
core.saveState('cleanup', cleanup);
4243
}
44+
45+
export function setBuildxIsDefaultBuilder(buildxIsDefaultBuilder: boolean) {
46+
core.saveState('buildxIsDefaultBuilder', buildxIsDefaultBuilder);
47+
}

0 commit comments

Comments
 (0)