Skip to content

Commit

Permalink
cleanup input to remove builder and temp files
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Mar 3, 2023
1 parent 03a7a3d commit 8b13d48
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 24 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,21 @@ jobs:
uses: ./
with:
driver: docker

cleanup:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
cleanup:
- true
- false
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Set up Docker Buildx
uses: ./
with:
cleanup: ${{ matrix.cleanup }}
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,20 @@ Following inputs can be used as `step.with` keys:
> platforms: linux/amd64,linux/arm64
> ```

| Name | Type | Description |
|-------------------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `version` | String | [Buildx](https://github.com/docker/buildx) version. (eg. `v0.3.0`, `latest`, `https://github.com/docker/buildx.git#master`) |
| `driver` | String | Sets the [builder driver](https://docs.docker.com/engine/reference/commandline/buildx_create/#driver) to be used (default `docker-container`) |
| `driver-opts` | List | List of additional [driver-specific options](https://docs.docker.com/engine/reference/commandline/buildx_create/#driver-opt) (eg. `image=moby/buildkit:master`) |
| `buildkitd-flags` | String | [Flags for buildkitd](https://docs.docker.com/engine/reference/commandline/buildx_create/#buildkitd-flags) daemon (since [buildx v0.3.0](https://github.com/docker/buildx/releases/tag/v0.3.0)) |
| `install` | Bool | Sets up `docker build` command as an alias to `docker buildx` (default `false`) |
| `use` | Bool | Switch to this builder instance (default `true`) |
| `endpoint` | String | [Optional address for docker socket](https://docs.docker.com/engine/reference/commandline/buildx_create/#description) or context from `docker context ls` |
| `platforms` | List/CSV | Fixed [platforms](https://docs.docker.com/engine/reference/commandline/buildx_create/#platform) for current node. If not empty, values take priority over the detected ones. |
| `config`¹ | String | [BuildKit config file](https://docs.docker.com/engine/reference/commandline/buildx_create/#config) |
| `config-inline`¹ | String | Same as `config` but inline |
| `append` | YAML | [Append additional nodes](docs/advanced/append-nodes.md) to the builder |
| Name | Type | Default | Description |
|-------------------|----------|--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `version` | String | | [Buildx](https://github.com/docker/buildx) version. (eg. `v0.3.0`, `latest`, `https://github.com/docker/buildx.git#master`) |
| `driver` | String | `docker-container` | Sets the [builder driver](https://docs.docker.com/engine/reference/commandline/buildx_create/#driver) to be used |
| `driver-opts` | List | | List of additional [driver-specific options](https://docs.docker.com/engine/reference/commandline/buildx_create/#driver-opt) (eg. `image=moby/buildkit:master`) |
| `buildkitd-flags` | String | | [Flags for buildkitd](https://docs.docker.com/engine/reference/commandline/buildx_create/#buildkitd-flags) daemon |
| `install` | Bool | `false` | Sets up `docker build` command as an alias to `docker buildx` |
| `use` | Bool | `true` | Switch to this builder instance |
| `endpoint` | String | | [Optional address for docker socket](https://docs.docker.com/engine/reference/commandline/buildx_create/#description) or context from `docker context ls` |
| `platforms` | List/CSV | | Fixed [platforms](https://docs.docker.com/engine/reference/commandline/buildx_create/#platform) for current node. If not empty, values take priority over the detected ones. |
| `config`¹ | String | | [BuildKit config file](https://docs.docker.com/engine/reference/commandline/buildx_create/#config) |
| `config-inline`¹ | String | | Same as `config` but inline |
| `append` | YAML | | [Append additional nodes](docs/advanced/append-nodes.md) to the builder |
| `cleanup` | Bool | `true` | Cleanup temp files and remove builder at the end of a job |

> * ¹ `config` and `config-inline` are mutually exclusive

Expand Down
39 changes: 32 additions & 7 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {beforeEach, describe, expect, jest, test} from '@jest/globals';
import * as uuid from 'uuid';
import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx';
import {Docker} from '@docker/actions-toolkit/lib/docker';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
import {Node} from '@docker/actions-toolkit/lib/types/builder';
Expand Down Expand Up @@ -27,9 +28,11 @@ describe('getCreateArgs', () => {
test.each([
[
0,
'v0.10.3',
new Map<string, string>([
['install', 'false'],
['use', 'true'],
['cleanup', 'true'],
]),
[
'create',
Expand All @@ -41,10 +44,12 @@ describe('getCreateArgs', () => {
],
[
1,
'v0.10.3',
new Map<string, string>([
['driver', 'docker'],
['install', 'false'],
['use', 'true'],
['cleanup', 'true'],
]),
[
'create',
Expand All @@ -56,10 +61,12 @@ describe('getCreateArgs', () => {
],
[
2,
'v0.10.3',
new Map<string, string>([
['install', 'false'],
['use', 'false'],
['driver-opts', 'image=moby/buildkit:master\nnetwork=host'],
['cleanup', 'true'],
]),
[
'create',
Expand All @@ -72,11 +79,13 @@ describe('getCreateArgs', () => {
],
[
3,
'v0.10.3',
new Map<string, string>([
['driver', 'remote'],
['endpoint', 'tls://foo:1234'],
['install', 'false'],
['use', 'true'],
['cleanup', 'true'],
]),
[
'create',
Expand All @@ -88,12 +97,14 @@ describe('getCreateArgs', () => {
],
[
4,
'v0.10.3',
new Map<string, string>([
['driver', 'remote'],
['platforms', 'linux/arm64,linux/arm/v7'],
['endpoint', 'tls://foo:1234'],
['install', 'false'],
['use', 'true'],
['cleanup', 'true'],
]),
[
'create',
Expand All @@ -106,10 +117,12 @@ describe('getCreateArgs', () => {
],
[
5,
'v0.10.3',
new Map<string, string>([
['install', 'false'],
['use', 'false'],
['driver-opts', `"env.no_proxy=localhost,127.0.0.1,.mydomain"`],
['cleanup', 'true'],
]),
[
'create',
Expand All @@ -121,10 +134,12 @@ describe('getCreateArgs', () => {
],
[
6,
'v0.10.3',
new Map<string, string>([
['install', 'false'],
['use', 'false'],
['platforms', 'linux/amd64\n"linux/arm64,linux/arm/v7"'],
['cleanup', 'true'],
]),
[
'create',
Expand All @@ -133,15 +148,19 @@ describe('getCreateArgs', () => {
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
'--platform', 'linux/amd64,linux/arm64,linux/arm/v7'
]
],
]
])(
'[%d] given %p as inputs, returns %p',
async (num: number, inputs: Map<string, string>, expected: Array<string>) => {
'[%d] given buildx %s and %p as inputs, returns %p',
async (num: number, buildxVersion: string, inputs: Map<string, string>, expected: Array<string>) => {
inputs.forEach((value: string, name: string) => {
setInput(name, value);
});
const toolkit = new Toolkit();
jest.spyOn(Buildx.prototype, 'version').mockImplementation(async (): Promise<string> => {
return buildxVersion;
});
const inp = await context.getInputs();
const res = await context.getCreateArgs(inp, new Toolkit());
const res = await context.getCreateArgs(inp, toolkit);
expect(res).toEqual(expected);
}
);
Expand All @@ -161,9 +180,11 @@ describe('getAppendArgs', () => {
test.each([
[
0,
'v0.10.3',
new Map<string, string>([
['install', 'false'],
['use', 'true'],
['cleanup', 'true'],
]),
{
"name": "aws_graviton2",
Expand All @@ -186,13 +207,17 @@ describe('getAppendArgs', () => {
]
]
])(
'[%d] given %p as inputs, returns %p',
async (num: number, inputs: Map<string, string>, node: Node, expected: Array<string>) => {
'[%d] given buildx %s and %p as inputs, returns %p',
async (num: number, buildxVersion: string, inputs: Map<string, string>, node: Node, expected: Array<string>) => {
inputs.forEach((value: string, name: string) => {
setInput(name, value);
});
const toolkit = new Toolkit();
jest.spyOn(Buildx.prototype, 'version').mockImplementation(async (): Promise<string> => {
return buildxVersion;
});
const inp = await context.getInputs();
const res = await context.getAppendArgs(inp, node, new Toolkit());
const res = await context.getAppendArgs(inp, node, toolkit);
expect(res).toEqual(expected);
}
);
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ inputs:
append:
description: 'Append additional nodes to the builder'
required: false
cleanup:
description: 'Cleanup temp files and remove builder at the end of a job'
default: 'true'
required: false

outputs:
name:
Expand Down
4 changes: 3 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface Inputs {
config: string;
configInline: string;
append: string;
cleanup: boolean;
}

export async function getInputs(): Promise<Inputs> {
Expand All @@ -35,7 +36,8 @@ export async function getInputs(): Promise<Inputs> {
endpoint: core.getInput('endpoint'),
config: core.getInput('config'),
configInline: core.getInput('config-inline'),
append: core.getInput('append')
append: core.getInput('append'),
cleanup: core.getBooleanInput('cleanup')
};
}

Expand Down
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ actionsToolkit.run(
// main
async () => {
const inputs: context.Inputs = await context.getInputs();
const toolkit = new Toolkit();
stateHelper.setCleanup(inputs.cleanup);

const toolkit = new Toolkit();
const standalone = await toolkit.buildx.isStandalone();
stateHelper.setStandalone(standalone);

Expand Down Expand Up @@ -164,9 +165,13 @@ actionsToolkit.run(
});
}

if (!stateHelper.cleanup) {
return;
}

if (stateHelper.builderName.length > 0) {
await core.group(`Removing builder`, async () => {
const buildx = new Buildx({standalone: /true/i.test(stateHelper.standalone)});
const buildx = new Buildx({standalone: stateHelper.standalone});
const rmCmd = await buildx.getCommand(['rm', stateHelper.builderName]);
await exec
.getExecOutput(rmCmd.command, rmCmd.args, {
Expand Down
7 changes: 6 additions & 1 deletion src/state-helper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as core from '@actions/core';

export const IsDebug = !!process.env['STATE_isDebug'];
export const standalone = process.env['STATE_standalone'] || '';
export const standalone = /true/i.test(process.env['STATE_standalone'] || '');
export const builderName = process.env['STATE_builderName'] || '';
export const containerName = process.env['STATE_containerName'] || '';
export const certsDir = process.env['STATE_certsDir'] || '';
export const cleanup = /true/i.test(process.env['STATE_cleanup'] || '');

export function setDebug(debug: string) {
core.saveState('isDebug', debug);
Expand All @@ -25,3 +26,7 @@ export function setContainerName(containerName: string) {
export function setCertsDir(certsDir: string) {
core.saveState('certsDir', certsDir);
}

export function setCleanup(cleanup: boolean) {
core.saveState('cleanup', cleanup);
}

0 comments on commit 8b13d48

Please sign in to comment.