Skip to content

Commit

Permalink
add builder arg
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 7c89267 commit ba9d021
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 22 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 @@ -22,6 +22,9 @@ inputs:
utility-image:
default: "ghcr.io/containerd/busybox:latest"
description: "The container image to use for injecting and extracting the cache"
builder:
default: default
description: "The name of the builder. Default: 'default'"
runs:
using: 'node20'
main: 'dist/index.js'
Expand Down
22 changes: 15 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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, containerImage: string) {
async function extractCache(cacheSource: string, cacheOptions: CacheOptions, scratchDir: string, containerImage: 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 @@ -55,9 +55,10 @@ export async function extractCaches(opts: Opts) {
const cacheMap = getCacheMap(opts);
const scratchDir = opts['scratch-dir'];
const containerImage = opts['utility-image'];
const builder = getBuilder(opts);

// Extract Caches for each source-target pair
for (const [cacheSource, cacheOptions] of Object.entries(cacheMap)) {
await extractCache(cacheSource, cacheOptions, scratchDir, containerImage);
await extractCache(cacheSource, cacheOptions, scratchDir, containerImage, builder);
}
}
9 changes: 5 additions & 4 deletions src/inject-cache.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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, containerImage: string) {
async function injectCache(cacheSource: string, cacheOptions: CacheOptions, scratchDir: string, containerImage: 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 +39,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 @@ -56,8 +56,9 @@ export async function injectCaches(opts: Opts) {
const scratchDir = opts['scratch-dir'];
const containerImage = opts['utility-image'];

const builder = getBuilder(opts);
// Inject Caches for each source-target pair
for (const [cacheSource, cacheOptions] of Object.entries(cacheMap)) {
await injectCache(cacheSource, cacheOptions, scratchDir, containerImage);
await injectCache(cacheSource, cacheOptions, scratchDir, containerImage, builder);
}
}
9 changes: 8 additions & 1 deletion src/opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type Opts = {
"scratch-dir": string
"skip-extraction": boolean
"utility-image": string
"builder"?: string
help: boolean
/** @deprecated Use `cache-map` instead */
"cache-source"?: string
Expand All @@ -22,9 +23,10 @@ export function parseOpts(args: string[]): mri.Argv<Opts> {
"skip-extraction": (getInput("skip-extraction") || "false") === "true",
"extract": process.env[`STATE_POST`] !== undefined,
"utility-image": getInput("utility-image") || "ghcr.io/containerd/busybox:latest",
"builder": getInput("builder") || "default",
"help": false,
},
string: ["cache-map", "scratch-dir", "cache-source", "cache-target", "utility-image"],
string: ["cache-map", "scratch-dir", "cache-source", "cache-target", "utility-image", "builder"],
boolean: ["skip-extraction", "help", "extract"],
alias: {
"help": ["h"],
Expand Down Expand Up @@ -52,6 +54,7 @@ Options:
--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
--utility-image The container image to use for injecting and extracting the cache. Default: 'ghcr.io/containerd/busybox:latest'
--builder The name of the buildx builder to use for the cache injection
--help Show this help
`);
}
Expand Down Expand Up @@ -128,3 +131,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"];
}
30 changes: 25 additions & 5 deletions tests/opts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ test('parseOpts with no arguments', () => {
"extract": false,
"h": false,
"help": false,
"utility-image": "ghcr.io/containerd/busybox:latest"
"utility-image": "ghcr.io/containerd/busybox:latest",
"builder": "default"
})
})

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

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

Expand All @@ -55,7 +58,23 @@ test('parseOpts with utility-image argument', () => {
"extract": false,
"h": false,
"help": false,
"utility-image": "alpine:1"
"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 @@ -69,7 +88,8 @@ test('parseOpts with help argument', () => {
"extract": false,
"h": true,
"help": true,
"utility-image": "ghcr.io/containerd/busybox:latest"
"utility-image": "ghcr.io/containerd/busybox:latest",
"builder": "default"
})
})

Expand Down

0 comments on commit ba9d021

Please sign in to comment.