Skip to content

Commit

Permalink
add builder
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Wong <[email protected]>
  • Loading branch information
ty2 committed Nov 12, 2024
1 parent 5b6db76 commit dd860b2
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 292 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Options:
--cache-map The map of actions source to container destination paths for the cache paths
--scratch-dir Where the action is stores some temporary files for its processing. Default: 'scratch'
--skip-extraction Skip the extraction of the cache from the docker container
--builder The name of the buildx builder. Default: 'default'
--help Show this help
```

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ inputs:
save-always:
default: "false"
description: "Run the post step to save the cache even if another step before fails"
builder:
default: default
description: "The name of the builder. Default: 'default'"
runs:
using: 'node20'
main: 'dist/index.js'
Expand Down
568 changes: 291 additions & 277 deletions 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.

9 changes: 5 additions & 4 deletions src/extract-cache.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs/promises';
import path from 'path';
import { CacheOptions, Opts, getCacheMap, getMountArgsString, getTargetPath } from './opts.js';
import {CacheOptions, Opts, getCacheMap, getMountArgsString, getTargetPath, getBuilder} from './opts.js';
import { run, runPiped } from './run.js';

async function extractCache(cacheSource: string, cacheOptions: CacheOptions, scratchDir: string) {
async function extractCache(cacheSource: string, cacheOptions: CacheOptions, scratchDir: string, builder: string) {
// Prepare Timestamp for Layer Cache Busting
const date = new Date().toISOString();

Expand All @@ -25,7 +25,7 @@ RUN --mount=${mountArgs} \
console.log(dancefileContent);

// Extract Data into Docker Image
await run('docker', ['buildx', 'build', '-f', path.join(scratchDir, 'Dancefile.extract'), '--tag', 'dance:extract', '--load', scratchDir]);
await run('docker', ['buildx', 'build', '--builder', builder, '-f', path.join(scratchDir, 'Dancefile.extract'), '--tag', 'dance:extract', '--load', scratchDir]);

// Create Extraction Image
try {
Expand Down Expand Up @@ -54,9 +54,10 @@ export async function extractCaches(opts: Opts) {

const cacheMap = getCacheMap(opts);
const scratchDir = opts['scratch-dir'];
const builder = getBuilder(opts);

// Extract Caches for each source-target pair
for (const [cacheSource, cacheOptions] of Object.entries(cacheMap)) {
await extractCache(cacheSource, cacheOptions, scratchDir);
await extractCache(cacheSource, cacheOptions, scratchDir, builder);
}
}
19 changes: 14 additions & 5 deletions src/inject-cache.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import fs from 'fs/promises';
import path from 'path';
import { CacheOptions, Opts, getCacheMap, getMountArgsString, getTargetPath, getUID, getGID } from './opts.js';
import {
CacheOptions,
Opts,
getCacheMap,
getMountArgsString,
getTargetPath,
getUID,
getGID,
getBuilder
} from './opts.js';
import { run } from './run.js';
import { notice } from '@actions/core';

async function injectCache(cacheSource: string, cacheOptions: CacheOptions, scratchDir: string) {
async function injectCache(cacheSource: string, cacheOptions: CacheOptions, scratchDir: string, builder: string) {
// Clean Scratch Directory
await fs.rm(scratchDir, { recursive: true, force: true });
await fs.mkdir(scratchDir, { recursive: true });
Expand Down Expand Up @@ -39,7 +48,7 @@ RUN --mount=${mountArgs} \
console.log(dancefileContent);

// Inject Data into Docker Cache
await run('docker', ['buildx', 'build', '-f', path.join(scratchDir, 'Dancefile.inject'), '--tag', 'dance:inject', cacheSource]);
await run('docker', ['buildx', 'build', '--builder', builder ,'-f', path.join(scratchDir, 'Dancefile.inject'), '--tag', 'dance:inject', cacheSource]);

// Clean Directories
try {
Expand All @@ -54,9 +63,9 @@ RUN --mount=${mountArgs} \
export async function injectCaches(opts: Opts) {
const cacheMap = getCacheMap(opts);
const scratchDir = opts['scratch-dir'];

const builder = getBuilder(opts);
// Inject Caches for each source-target pair
for (const [cacheSource, cacheOptions] of Object.entries(cacheMap)) {
await injectCache(cacheSource, cacheOptions, scratchDir);
await injectCache(cacheSource, cacheOptions, scratchDir, builder);
}
}
9 changes: 8 additions & 1 deletion src/opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type Opts = {
"cache-source"?: string
/** @deprecated Use `cache-map` instead */
"cache-target"?: string
"builder"?: string
}

export function parseOpts(args: string[]): mri.Argv<Opts> {
Expand All @@ -20,9 +21,10 @@ export function parseOpts(args: string[]): mri.Argv<Opts> {
"scratch-dir": getInput("scratch-dir") || "scratch",
"skip-extraction": (getInput("skip-extraction") || "false") === "true",
"extract": process.env[`STATE_POST`] !== undefined,
"builder": getInput("builder") || "default",
"help": false,
},
string: ["cache-map", "scratch-dir", "cache-source", "cache-target"],
string: ["cache-map", "scratch-dir", "cache-source", "cache-target", "builder"],
boolean: ["skip-extraction", "help", "extract"],
alias: {
"help": ["h"],
Expand All @@ -49,6 +51,7 @@ Options:
--cache-map The map of actions source paths to container destination paths or mount arguments
--scratch-dir Where the action is stores some temporary files for its processing. Default: 'scratch'
--skip-extraction Skip the extraction of the cache from the docker container
--builder The name of the builder to use for the cache injection
--help Show this help
`);
}
Expand Down Expand Up @@ -125,3 +128,7 @@ export function getMountArgsString(cacheOptions: CacheOptions): string {
return `type=cache,${otherOptions}`;
}
}

export function getBuilder(opts: Opts): string {
return opts["builder"] == null || opts["builder"] == "" ? "default" : opts["builder"];
}
46 changes: 42 additions & 4 deletions tests/opts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ test('parseOpts with no arguments', () => {
"skip-extraction": false,
"extract": false,
"h": false,
"help": false
"help": false,
"utility-image": "ghcr.io/containerd/busybox:latest",
"builder": "default",
})
})

Expand All @@ -23,7 +25,9 @@ test('parseOpts with cache-map argument', () => {
"skip-extraction": false,
"extract": false,
"h": false,
"help": false
"help": false,
"utility-image": "ghcr.io/containerd/busybox:latest",
"builder": "default",
})
})

Expand All @@ -38,7 +42,39 @@ test('parseOpts with deprecated cache-source and cache-target arguments', () =>
"h": false,
"help": false,
"cache-source": 'source',
"cache-target": 'target'
"cache-target": 'target',
"utility-image": "ghcr.io/containerd/busybox:latest",
"builder": "default",
})
})

test('parseOpts with utility-image argument', () => {
const opts = parseOpts(['--utility-image', 'alpine:1'])
expect(opts).toEqual({
"_": [],
"cache-map": '{}',
"scratch-dir": "scratch",
"skip-extraction": false,
"extract": false,
"h": false,
"help": false,
"utility-image": "alpine:1",
"builder": "default",
})
})

test('parseOpts with builder argument', () => {
const opts = parseOpts(['--builder', 'another-builder'])
expect(opts).toEqual({
"_": [],
"cache-map": '{}',
"scratch-dir": "scratch",
"skip-extraction": false,
"extract": false,
"h": false,
"help": false,
"utility-image": "ghcr.io/containerd/busybox:latest",
"builder": "another-builder",
})
})

Expand All @@ -52,6 +88,8 @@ test('parseOpts with help argument', () => {
"extract": false,
"h": true,
"help": true,
"utility-image": "ghcr.io/containerd/busybox:latest",
"builder": "default",
})
})

Expand Down Expand Up @@ -131,4 +169,4 @@ test('getGID with object with gid', () => {
const cacheOptions = { target: 'targetPath', shared: true, id: 1, gid: 1000 }
const gid = getGID(cacheOptions)
expect(gid).toBe('1000')
})
})

0 comments on commit dd860b2

Please sign in to comment.