Skip to content

Commit 1b8ab18

Browse files
authored
Merge pull request #61 from jackmisbach/revert-extraction-and-builder-default
Revert extraction and builder default
2 parents 6f699a7 + a7be0c2 commit 1b8ab18

File tree

7 files changed

+40
-27
lines changed

7 files changed

+40
-27
lines changed

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ inputs:
2828
default: "ghcr.io/containerd/busybox:latest"
2929
description: "The container image to use for injecting and extracting the cache"
3030
builder:
31-
default: default
32-
description: "The name of the builder. Default: 'default'"
31+
default: ""
32+
description: "The name of the buildx builder to use. If not specified, the currently active builder is used."
3333
runs:
3434
using: 'node20'
3535
main: 'dist/index.js'

dist/index.js

Lines changed: 5 additions & 7 deletions
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/extract-cache.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { promises as fs } from 'fs';
22
import path from 'path';
33
import { CacheOptions, Opts, getBuilder, getCacheMap, getMountArgsString, getTargetPath } from './opts.js';
4-
import { run } from './run.js';
4+
import { run, runPiped } from './run.js';
55

66
async function extractCache(cacheSource: string, cacheOptions: CacheOptions, scratchDir: string, containerImage: string, builder: string) {
77
// Prepare Timestamp for Layer Cache Busting
@@ -15,19 +15,35 @@ async function extractCache(cacheSource: string, cacheOptions: CacheOptions, scr
1515
const mountArgs = getMountArgsString(cacheOptions);
1616

1717
const dancefileContent = `
18-
FROM ${containerImage} AS dance-extract
18+
FROM ${containerImage}
1919
COPY buildstamp buildstamp
2020
RUN --mount=${mountArgs} \
2121
mkdir -p /var/dance-cache/ \
2222
&& cp -p -R ${targetPath}/. /var/dance-cache/ || true
23-
FROM scratch
24-
COPY --from=dance-extract --chmod=u=rwX,go=rX /var/dance-cache /
2523
`;
2624
await fs.writeFile(path.join(scratchDir, 'Dancefile.extract'), dancefileContent);
2725
console.log(dancefileContent);
2826

29-
// Extract cache
30-
await run('docker', ['buildx', 'build', '--builder', builder, '-f', path.join(scratchDir, 'Dancefile.extract'), '--tag', 'dance:extract', '--output', `type=local,dest=${cacheSource}`, scratchDir]);
27+
// Extract Data into Docker Image
28+
await run('docker', ['buildx', 'build', ...(builder ? ['--builder', builder] : []), '-f', path.join(scratchDir, 'Dancefile.extract'), '--tag', 'dance:extract', '--load', scratchDir]);
29+
30+
// Create Extraction Image
31+
try {
32+
await run('docker', ['rm', '-f', 'cache-container']);
33+
} catch (error) {
34+
// Ignore error if container does not exist
35+
}
36+
await run('docker', ['create', '-ti', '--name', 'cache-container', 'dance:extract']);
37+
38+
// Unpack Docker Image into Scratch
39+
await runPiped(
40+
['docker', ['cp', '-L', 'cache-container:/var/dance-cache', '-']],
41+
['tar', ['-H', 'posix', '-x', '-C', scratchDir]]
42+
);
43+
44+
// Move Cache into Its Place
45+
await run('sudo', ['rm', '-rf', cacheSource]);
46+
await fs.rename(path.join(scratchDir, 'dance-cache'), cacheSource);
3147
}
3248

3349
export async function extractCaches(opts: Opts) {

src/inject-cache.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ RUN --mount=${mountArgs} \
3939
console.log(dancefileContent);
4040

4141
// Inject Data into Docker Cache
42-
await run('docker', ['buildx', 'build', '--builder', builder ,'-f', path.join(scratchDir, 'Dancefile.inject'), '--tag', 'dance:inject', cacheSource]);
43-
42+
await run('docker', ['buildx', 'build', ...(builder ? ['--builder', builder] : []), '-f', path.join(scratchDir, 'Dancefile.inject'), '--tag', 'dance:inject', cacheSource]);
4443
// Clean Directories
4544
try {
4645
await fs.rm(cacheSource, { recursive: true, force: true });

src/opts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function parseOpts(args: string[]): mri.Argv<Opts> {
2929
"skip-extraction": (getInput("skip-extraction") || "false") === "true",
3030
"extract": process.env[`STATE_POST`] !== undefined,
3131
"utility-image": getInput("utility-image") || "ghcr.io/containerd/busybox:latest",
32-
"builder": getInput("builder") || "default",
32+
"builder": getInput("builder"),
3333
"help": false,
3434
},
3535
string: ["cache-map", "dockerfile", "cache-dir", "scratch-dir", "cache-source", "cache-target", "utility-image", "builder"],
@@ -184,5 +184,5 @@ export function getMountArgsString(cacheOptions: CacheOptions): string {
184184
}
185185

186186
export function getBuilder(opts: Opts): string {
187-
return opts["builder"] == null || opts["builder"] == "" ? "default" : opts["builder"];
187+
return opts["builder"] ?? "";
188188
}

tests/opts.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('parseOpts with no arguments', () => {
1515
"h": false,
1616
"help": false,
1717
"utility-image": "ghcr.io/containerd/busybox:latest",
18-
"builder": "default"
18+
"builder": ""
1919
})
2020
})
2121

@@ -32,7 +32,7 @@ test('parseOpts with cache-map argument', () => {
3232
"h": false,
3333
"help": false,
3434
"utility-image": "ghcr.io/containerd/busybox:latest",
35-
"builder": "default"
35+
"builder": ""
3636
})
3737
})
3838

@@ -51,7 +51,7 @@ test('parseOpts with deprecated cache-source and cache-target arguments', () =>
5151
"cache-source": 'source',
5252
"cache-target": 'target',
5353
"utility-image": "ghcr.io/containerd/busybox:latest",
54-
"builder": "default"
54+
"builder": ""
5555
})
5656
})
5757

@@ -68,7 +68,7 @@ test('parseOpts with utility-image argument', () => {
6868
"h": false,
6969
"help": false,
7070
"utility-image": "alpine:1",
71-
"builder": "default"
71+
"builder": ""
7272
})
7373
})
7474

@@ -102,7 +102,7 @@ test('parseOpts with dockerfile argument', () => {
102102
"h": false,
103103
"help": false,
104104
"utility-image": "ghcr.io/containerd/busybox:latest",
105-
"builder": "default"
105+
"builder": ""
106106
})
107107
})
108108

@@ -119,7 +119,7 @@ test('parseOpts with cache-dir argument', () => {
119119
"h": false,
120120
"help": false,
121121
"utility-image": "ghcr.io/containerd/busybox:latest",
122-
"builder": "default"
122+
"builder": ""
123123
})
124124
})
125125

@@ -136,7 +136,7 @@ test('parseOpts with help argument', () => {
136136
"h": true,
137137
"help": true,
138138
"utility-image": "ghcr.io/containerd/busybox:latest",
139-
"builder": "default"
139+
"builder": ""
140140
})
141141
})
142142

0 commit comments

Comments
 (0)