Skip to content

Commit

Permalink
merge upstream repo
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
2 parents 0160dc1 + 7c89267 commit f23aed3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 29 deletions.
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"
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'"
Expand Down
17 changes: 11 additions & 6 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.

7 changes: 4 additions & 3 deletions src/extract-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
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, builder: 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 @@ -15,7 +15,7 @@ async function extractCache(cacheSource: string, cacheOptions: CacheOptions, scr
const mountArgs = getMountArgsString(cacheOptions);

const dancefileContent = `
FROM busybox:1
FROM ${containerImage}
COPY buildstamp buildstamp
RUN --mount=${mountArgs} \
mkdir -p /var/dance-cache/ \
Expand Down Expand Up @@ -54,10 +54,11 @@ 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, builder);
await extractCache(cacheSource, cacheOptions, scratchDir, containerImage, builder);
}
}
19 changes: 6 additions & 13 deletions src/inject-cache.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import fs from 'fs/promises';
import path from 'path';
import {
CacheOptions,
Opts,
getCacheMap,
getMountArgsString,
getTargetPath,
getUID,
getGID,
getBuilder
} 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, builder: 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 All @@ -38,7 +29,7 @@ async function injectCache(cacheSource: string, cacheOptions: CacheOptions, scra

// Prepare Dancefile to Access Caches
const dancefileContent = `
FROM busybox:1
FROM ${containerImage}
COPY buildstamp buildstamp
RUN --mount=${mountArgs} \
--mount=type=bind,source=.,target=/var/dance-cache \
Expand All @@ -63,9 +54,11 @@ RUN --mount=${mountArgs} \
export async function injectCaches(opts: Opts) {
const cacheMap = getCacheMap(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, builder);
await injectCache(cacheSource, cacheOptions, scratchDir, containerImage, builder);
}
}
7 changes: 5 additions & 2 deletions src/opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ export type Opts = {
"cache-map": string
"scratch-dir": string
"skip-extraction": boolean
"utility-image": string
"builder"?: string
help: boolean
/** @deprecated Use `cache-map` instead */
"cache-source"?: string
/** @deprecated Use `cache-map` instead */
"cache-target"?: string
"builder"?: string
}

export function parseOpts(args: string[]): mri.Argv<Opts> {
Expand All @@ -21,10 +22,11 @@ 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,
"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", "builder"],
string: ["cache-map", "scratch-dir", "cache-source", "cache-target", "utility-image", "builder"],
boolean: ["skip-extraction", "help", "extract"],
alias: {
"help": ["h"],
Expand All @@ -51,6 +53,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
--utility-image The container image to use for injecting and extracting the cache. Default: 'ghcr.io/containerd/busybox:latest'
--builder The name of the builder to use for the cache injection
--help Show this help
`);
Expand Down
26 changes: 22 additions & 4 deletions tests/opts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ test('parseOpts with no arguments', () => {
"skip-extraction": false,
"extract": false,
"h": false,
"help": false
"help": false,
"utility-image": "ghcr.io/containerd/busybox:latest"
})
})

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

Expand All @@ -38,7 +40,22 @@ 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"
})
})

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"
})
})

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

Expand Down Expand Up @@ -131,4 +149,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 f23aed3

Please sign in to comment.