Skip to content

Commit

Permalink
Remove aliases created by buildx when installing by default
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
hashhar committed Sep 15, 2024
1 parent b467d6a commit 640bfe6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,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
Expand Down Expand Up @@ -278,5 +279,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'}`);
}
});
});
}
}
);
5 changes: 5 additions & 0 deletions src/state-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

0 comments on commit 640bfe6

Please sign in to comment.