Skip to content

Commit 6229134

Browse files
authored
Merge pull request #427 from crazy-max/keep-state
Keep BuildKit state support
2 parents 3f1544e + c6f6a07 commit 6229134

File tree

9 files changed

+168
-42
lines changed

9 files changed

+168
-42
lines changed

.github/workflows/ci.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,44 @@ jobs:
603603
exit 1
604604
fi
605605
shell: bash
606+
607+
keep-state:
608+
runs-on: ubuntu-latest
609+
steps:
610+
-
611+
name: Checkout
612+
uses: actions/checkout@v4
613+
-
614+
name: Set up Docker Buildx
615+
uses: ./
616+
with:
617+
name: foo
618+
keep-state: true
619+
-
620+
name: Set up Docker Buildx
621+
uses: ./
622+
with:
623+
name: foo
624+
625+
keep-state-error:
626+
runs-on: ubuntu-latest
627+
steps:
628+
-
629+
name: Checkout
630+
uses: actions/checkout@v4
631+
-
632+
name: Set up Docker Buildx
633+
id: buildx
634+
continue-on-error: true
635+
uses: ./
636+
with:
637+
driver: docker
638+
keep-state: true
639+
-
640+
name: Check
641+
run: |
642+
echo "${{ toJson(steps.buildx) }}"
643+
if [ "${{ steps.buildx.outcome }}" != "failure" ] || [ "${{ steps.buildx.conclusion }}" != "success" ]; then
644+
echo "::error::Should have failed"
645+
exit 1
646+
fi

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,23 @@ The following inputs can be used as `step.with` keys:
8585
> platforms: linux/amd64,linux/arm64
8686
> ```
8787

88-
| Name | Type | Default | Description |
89-
|------------------------------|----------|--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
90-
| `version` | String | | [Buildx](https://github.com/docker/buildx) version. (eg. `v0.3.0`, `latest`, `https://github.com/docker/buildx.git#master`) |
91-
| `driver` | String | `docker-container` | Sets the [builder driver](https://docs.docker.com/engine/reference/commandline/buildx_create/#driver) to be used |
92-
| `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`) |
93-
| `buildkitd-flags` | String | | [BuildKit daemon flags](https://docs.docker.com/engine/reference/commandline/buildx_create/#buildkitd-flags) |
94-
| `buildkitd-config` \* | String | | [BuildKit daemon config file](https://docs.docker.com/engine/reference/commandline/buildx_create/#config) |
95-
| `buildkitd-config-inline` \* | String | | Same as `buildkitd-config` but inline |
96-
| `install` | Bool | `false` | Sets up `docker build` command as an alias to `docker buildx` |
97-
| `use` | Bool | `true` | Switch to this builder instance |
98-
| `endpoint` | String | | [Optional address for docker socket](https://docs.docker.com/engine/reference/commandline/buildx_create/#description) or context from `docker context ls` |
99-
| `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. |
100-
| `append` | YAML | | [Append additional nodes](https://docs.docker.com/build/ci/github-actions/configure-builder/#append-additional-nodes-to-the-builder) to the builder |
101-
| `cache-binary` | Bool | `true` | Cache buildx binary to GitHub Actions cache backend |
102-
| `cleanup` | Bool | `true` | Cleanup temp files and remove builder at the end of a job |
88+
| Name | Type | Default | Description |
89+
|------------------------------|----------|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
90+
| `version` | String | | [Buildx](https://github.com/docker/buildx) version. (eg. `v0.3.0`, `latest`, `https://github.com/docker/buildx.git#master`) |
91+
| `name` | String | | Name of the builder. If not specified, one will be generated or if it already exists, it will be used instead of creating a new one |
92+
| `driver` | String | `docker-container` | Sets the [builder driver](https://docs.docker.com/engine/reference/commandline/buildx_create/#driver) to be used |
93+
| `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`) |
94+
| `buildkitd-flags` | String | | [BuildKit daemon flags](https://docs.docker.com/engine/reference/commandline/buildx_create/#buildkitd-flags) |
95+
| `buildkitd-config` \* | String | | [BuildKit daemon config file](https://docs.docker.com/engine/reference/commandline/buildx_create/#config) |
96+
| `buildkitd-config-inline` \* | String | | Same as `buildkitd-config` but inline |
97+
| `install` | Bool | `false` | Sets up `docker build` command as an alias to `docker buildx` |
98+
| `use` | Bool | `true` | Switch to this builder instance |
99+
| `endpoint` | String | | [Optional address for docker socket](https://docs.docker.com/engine/reference/commandline/buildx_create/#description) or context from `docker context ls` |
100+
| `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 |
101+
| `append` | YAML | | [Append additional nodes](https://docs.docker.com/build/ci/github-actions/configure-builder/#append-additional-nodes-to-the-builder) to the builder |
102+
| `keep-state` | Bool | `false` | Keep BuildKit state on `cleanup`. This is only useful on persistent self-hosted runners |
103+
| `cache-binary` | Bool | `true` | Cache buildx binary to GitHub Actions cache backend |
104+
| `cleanup` | Bool | `true` | Cleanup temp files and remove builder at the end of a job |
103105

104106
> [!IMPORTANT]
105107
> If you set the `buildkitd-flags` input, the default flags (`--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host`)

__tests__/context.test.ts

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ describe('getCreateArgs', () => {
5656
['use', 'true'],
5757
['cache-binary', 'true'],
5858
['cleanup', 'true'],
59+
['keep-state', 'false']
5960
]),
6061
[
6162
'create',
@@ -74,6 +75,7 @@ describe('getCreateArgs', () => {
7475
['use', 'true'],
7576
['cache-binary', 'true'],
7677
['cleanup', 'true'],
78+
['keep-state', 'false']
7779
]),
7880
[
7981
'create',
@@ -92,6 +94,7 @@ describe('getCreateArgs', () => {
9294
['driver-opts', 'image=moby/buildkit:master\nnetwork=host'],
9395
['cache-binary', 'true'],
9496
['cleanup', 'true'],
97+
['keep-state', 'false']
9598
]),
9699
[
97100
'create',
@@ -112,6 +115,7 @@ describe('getCreateArgs', () => {
112115
['use', 'true'],
113116
['cache-binary', 'true'],
114117
['cleanup', 'true'],
118+
['keep-state', 'false']
115119
]),
116120
[
117121
'create',
@@ -132,6 +136,7 @@ describe('getCreateArgs', () => {
132136
['use', 'true'],
133137
['cache-binary', 'true'],
134138
['cleanup', 'true'],
139+
['keep-state', 'false']
135140
]),
136141
[
137142
'create',
@@ -151,6 +156,7 @@ describe('getCreateArgs', () => {
151156
['driver-opts', `"env.no_proxy=localhost,127.0.0.1,.mydomain"`],
152157
['cache-binary', 'true'],
153158
['cleanup', 'true'],
159+
['keep-state', 'false'],
154160
]),
155161
[
156162
'create',
@@ -169,6 +175,7 @@ describe('getCreateArgs', () => {
169175
['platforms', 'linux/amd64\n"linux/arm64,linux/arm/v7"'],
170176
['cache-binary', 'true'],
171177
['cleanup', 'true'],
178+
['keep-state', 'false'],
172179
]),
173180
[
174181
'create',
@@ -187,6 +194,7 @@ describe('getCreateArgs', () => {
187194
['driver', 'unknown'],
188195
['cache-binary', 'true'],
189196
['cleanup', 'true'],
197+
['keep-state', 'false'],
190198
]),
191199
[
192200
'create',
@@ -203,6 +211,7 @@ describe('getCreateArgs', () => {
203211
['buildkitd-config', path.join(fixturesDir, 'buildkitd.toml')],
204212
['cache-binary', 'true'],
205213
['cleanup', 'true'],
214+
['keep-state', 'false'],
206215
]),
207216
[
208217
'create',
@@ -221,6 +230,7 @@ describe('getCreateArgs', () => {
221230
['buildkitd-config-inline', 'debug = true'],
222231
['cache-binary', 'true'],
223232
['cleanup', 'true'],
233+
['keep-state', 'false'],
224234
]),
225235
[
226236
'create',
@@ -240,14 +250,53 @@ describe('getCreateArgs', () => {
240250
['buildkitd-flags', '--allow-insecure-entitlement network.host'],
241251
['cache-binary', 'true'],
242252
['cleanup', 'true'],
253+
['keep-state', 'false'],
243254
]),
244255
[
245256
'create',
246257
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
247258
'--driver', 'cloud',
248259
'--buildkitd-flags', '--allow-insecure-entitlement network.host',
249260
]
250-
]
261+
],
262+
[
263+
11,
264+
'v0.10.3',
265+
new Map<string, string>([
266+
['install', 'false'],
267+
['use', 'true'],
268+
['cleanup', 'true'],
269+
['cache-binary', 'true'],
270+
['keep-state', 'false'],
271+
['name', 'test-builder-name'],
272+
]),
273+
[
274+
'create',
275+
'--name', 'test-builder-name',
276+
'--driver', 'docker-container',
277+
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
278+
'--use'
279+
]
280+
],
281+
[
282+
12,
283+
'v0.10.3',
284+
new Map<string, string>([
285+
['install', 'false'],
286+
['use', 'true'],
287+
['cleanup', 'true'],
288+
['cache-binary', 'true'],
289+
['keep-state', 'true'],
290+
['name', 'test-builder-name'],
291+
]),
292+
[
293+
'create',
294+
'--name', 'test-builder-name',
295+
'--driver', 'docker-container',
296+
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
297+
'--use',
298+
]
299+
],
251300
])(
252301
'[%d] given buildx %s and %p as inputs, returns %p',
253302
async (num: number, buildxVersion: string, inputs: Map<string, string>, expected: Array<string>) => {
@@ -285,6 +334,7 @@ describe('getAppendArgs', () => {
285334
['use', 'true'],
286335
['cache-binary', 'true'],
287336
['cleanup', 'true'],
337+
['keep-state', 'false']
288338
]),
289339
{
290340
"name": "aws_graviton2",
@@ -343,6 +393,7 @@ describe('getVersion', () => {
343393
['use', 'true'],
344394
['cache-binary', 'true'],
345395
['cleanup', 'true'],
396+
['keep-state', 'false']
346397
]),
347398
''
348399
],
@@ -354,7 +405,8 @@ describe('getVersion', () => {
354405
['install', 'false'],
355406
['use', 'true'],
356407
['cache-binary', 'true'],
357-
['cleanup', 'true']
408+
['cleanup', 'true'],
409+
['keep-state', 'false']
358410
]),
359411
'latest'
360412
],
@@ -366,7 +418,8 @@ describe('getVersion', () => {
366418
['install', 'false'],
367419
['use', 'true'],
368420
['cache-binary', 'true'],
369-
['cleanup', 'true']
421+
['cleanup', 'true'],
422+
['keep-state', 'false']
370423
]),
371424
'edge'
372425
],
@@ -378,7 +431,8 @@ describe('getVersion', () => {
378431
['install', 'false'],
379432
['use', 'true'],
380433
['cache-binary', 'true'],
381-
['cleanup', 'true']
434+
['cleanup', 'true'],
435+
['keep-state', 'false']
382436
]),
383437
'v0.19.2'
384438
],
@@ -391,7 +445,8 @@ describe('getVersion', () => {
391445
['install', 'false'],
392446
['use', 'true'],
393447
['cache-binary', 'true'],
394-
['cleanup', 'true']
448+
['cleanup', 'true'],
449+
['keep-state', 'false']
395450
]),
396451
'cloud:latest'
397452
],
@@ -404,7 +459,8 @@ describe('getVersion', () => {
404459
['install', 'false'],
405460
['use', 'true'],
406461
['cache-binary', 'true'],
407-
['cleanup', 'true']
462+
['cleanup', 'true'],
463+
['keep-state', 'false']
408464
]),
409465
'cloud:edge'
410466
],
@@ -417,6 +473,7 @@ describe('getVersion', () => {
417473
['use', 'true'],
418474
['cache-binary', 'true'],
419475
['cleanup', 'true'],
476+
['keep-state', 'false']
420477
]),
421478
'cloud:latest'
422479
],
@@ -430,6 +487,7 @@ describe('getVersion', () => {
430487
['use', 'true'],
431488
['cache-binary', 'true'],
432489
['cleanup', 'true'],
490+
['keep-state', 'false']
433491
]),
434492
'cloud:v0.11.2-desktop.2'
435493
],
@@ -442,6 +500,7 @@ describe('getVersion', () => {
442500
['use', 'true'],
443501
['cache-binary', 'true'],
444502
['cleanup', 'true'],
503+
['keep-state', 'false']
445504
]),
446505
'cloud:v0.11.2-desktop.2'
447506
],

action.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ inputs:
3434
description: 'Switch to this builder instance'
3535
default: 'true'
3636
required: false
37+
name:
38+
description: 'Name of the builder. If not specified, one will be generated or if it already exists, it will be used instead of creating a new one.'
39+
required: false
3740
endpoint:
3841
description: 'Optional address for docker socket or context from `docker context ls`'
3942
required: false
@@ -43,6 +46,10 @@ inputs:
4346
append:
4447
description: 'Append additional nodes to the builder'
4548
required: false
49+
keep-state:
50+
description: 'Keep BuildKit state on cleanup. This is only useful on persistent self-hosted runners.'
51+
default: 'false'
52+
required: false
4653
cache-binary:
4754
description: 'Cache buildx binary to GitHub Actions cache backend'
4855
default: 'true'

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/context.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ export interface Inputs {
2525
append: string;
2626
cacheBinary: boolean;
2727
cleanup: boolean;
28+
keepState: boolean;
2829
}
2930

3031
export async function getInputs(): Promise<Inputs> {
3132
return {
3233
version: core.getInput('version'),
33-
name: await getBuilderName(core.getInput('driver') || 'docker-container'),
34+
name: await getBuilderName(core.getInput('name'), core.getInput('driver') || 'docker-container'),
3435
driver: core.getInput('driver') || 'docker-container',
3536
driverOpts: Util.getInputList('driver-opts', {ignoreComma: true, quote: false}),
3637
buildkitdFlags: core.getInput('buildkitd-flags'),
@@ -41,13 +42,14 @@ export async function getInputs(): Promise<Inputs> {
4142
buildkitdConfig: core.getInput('buildkitd-config') || core.getInput('config'),
4243
buildkitdConfigInline: core.getInput('buildkitd-config-inline') || core.getInput('config-inline'),
4344
append: core.getInput('append'),
45+
keepState: core.getBooleanInput('keep-state'),
4446
cacheBinary: core.getBooleanInput('cache-binary'),
4547
cleanup: core.getBooleanInput('cleanup')
4648
};
4749
}
4850

49-
export async function getBuilderName(driver: string): Promise<string> {
50-
return driver == 'docker' ? await Docker.context() : `builder-${crypto.randomUUID()}`;
51+
export async function getBuilderName(name: string, driver: string): Promise<string> {
52+
return driver == 'docker' ? await Docker.context() : name || `builder-${crypto.randomUUID()}`;
5153
}
5254

5355
export async function getCreateArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> {

0 commit comments

Comments
 (0)