From 4ba329ef89f04b4a722482e05e2f5c7289e99e7f Mon Sep 17 00:00:00 2001 From: Ashhar Hasan Date: Sun, 15 Sep 2024 23:59:47 +0530 Subject: [PATCH] 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 --- src/main.ts | 13 +++++++++++++ src/state-helper.ts | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/main.ts b/src/main.ts index 13f0261..cf22ee3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -179,6 +179,7 @@ actionsToolkit.run( throw new Error(`Cannot set buildx as default builder without the Docker CLI`); } await core.group(`Setting buildx as default builder`, async () => { + stateHelper.setBuildxIsDefaultBuilder(true); const installCmd = await toolkit.buildx.getCommand(['install']); await Exec.getExecOutput(installCmd.command, installCmd.args, { ignoreReturnCode: true @@ -279,5 +280,17 @@ actionsToolkit.run( fs.rmSync(stateHelper.certsDir, {recursive: true}); }); } + + if (stateHelper.buildxIsDefaultBuilder) { + await core.group(`Restoring default builder`, async () => { + await Exec.getExecOutput('docker', ['buildx', 'uninstall'], { + ignoreReturnCode: true + }).then(res => { + if (res.stderr.length > 0 && res.exitCode != 0) { + core.warning(`${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`); + } + }); + }); + } } ); diff --git a/src/state-helper.ts b/src/state-helper.ts index d67c8c6..ca9d6b4 100644 --- a/src/state-helper.ts +++ b/src/state-helper.ts @@ -8,6 +8,7 @@ export const containerName = process.env['STATE_containerName'] || ''; export const certsDir = process.env['STATE_certsDir'] || ''; export const tmpDockerContext = process.env['STATE_tmpDockerContext'] || ''; export const cleanup = /true/i.test(process.env['STATE_cleanup'] || ''); +export const buildxIsDefaultBuilder = /true/i.test(process.env['STATE_buildxIsDefaultBuilder'] || ''); export function setDebug(debug: string) { core.saveState('isDebug', debug); @@ -40,3 +41,7 @@ export function setTmpDockerContext(tmpDockerContext: string) { export function setCleanup(cleanup: boolean) { core.saveState('cleanup', cleanup); } + +export function setBuildxIsDefaultBuilder(buildxIsDefaultBuilder: boolean) { + core.saveState('buildxIsDefaultBuilder', buildxIsDefaultBuilder); +}