Skip to content

Commit

Permalink
Add cacheTo argument to ci action
Browse files Browse the repository at this point in the history
  • Loading branch information
sebst committed Sep 2, 2024
1 parent 4f6b93e commit 80f211c
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ inputs:
required: false
default: false
description: Builds the image with `--no-cache` (takes precedence over `cacheFrom`)
cacheTo:
required: false
description: Specify the image to cache the built image to
outputs:
runCmdOutput:
description: The output of the command specified in the runCmd input
Expand Down
2 changes: 2 additions & 0 deletions azdo-task/DevcontainersCi/src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function buildImage(
subFolder: string,
skipContainerUserIdUpdate: boolean,
cacheFrom: string[],
cacheTo: string | undefined,
): Promise<string> {
console.log('🏗 Building dev container...');
try {
Expand All @@ -23,6 +24,7 @@ export async function buildImage(
subFolder,
skipContainerUserIdUpdate,
cacheFrom,
cacheTo,
);
} catch (error) {
task.setResult(task.TaskResult.Failed, error);
Expand Down
2 changes: 2 additions & 0 deletions azdo-task/DevcontainersCi/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export async function runMain(): Promise<void> {
const inputEnvsWithDefaults = populateDefaults(envs, inheritEnv);
const cacheFrom = task.getInput('cacheFrom')?.split('\n') ?? [];
const noCache = (task.getInput('noCache') ?? 'false') === 'true';
const cacheTo = task.getInput('cacheTo') ?? undefined;
const skipContainerUserIdUpdate =
(task.getInput('skipContainerUserIdUpdate') ?? 'false') === 'true';

Expand Down Expand Up @@ -101,6 +102,7 @@ export async function runMain(): Promise<void> {
additionalCacheFroms: cacheFrom,
output: buildxOutput,
noCache,
cacheTo,
};

console.log('\n\n');
Expand Down
6 changes: 6 additions & 0 deletions azdo-task/DevcontainersCi/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@
"type": "boolean",
"label": "Builds the image with `--no-cache` (takes precedence over `cacheFrom`)",
"required": false
},
{
"name": "cacheTo",
"type": "string",
"label": "Specify the image to cache the built image to",
"required": false
}
],
"outputVariables": [{
Expand Down
1 change: 1 addition & 0 deletions azdo-task/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ In the example above, the devcontainer-build-run will perform the following step
| skipContainerUserIdUpdate | false | For non-root Dev Containers (i.e. where `remoteUser` is specified), the action attempts to make the container user UID and GID match those of the host user. Set this to true to skip this step (defaults to false) |
| cacheFrom | false | Specify additional images to use for build caching |
| noCache | false | Builds the image with `--no-cache` (takes precedence over `cacheFrom`) |
| cacheTo | false | Specify the image to cache the built image to
| platform | false | Platforms for which the image should be built. If omitted, defaults to the platform of the GitHub Actions Runner. Multiple platforms should be comma separated. |

## Outputs
Expand Down
1 change: 1 addition & 0 deletions common/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface DevContainerConfig {
context?: string;
args?: Record<string, string>;
cacheFrom?: string | string[];
cacheTo?: string;
};
runArgs?: string[];
mounts?: string[];
Expand Down
4 changes: 4 additions & 0 deletions common/src/dev-container-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export interface DevContainerCliBuildArgs {
userDataFolder?: string;
output?: string,
noCache?: boolean,
cacheTo?: string,
}
async function devContainerBuild(
args: DevContainerCliBuildArgs,
Expand Down Expand Up @@ -194,6 +195,9 @@ async function devContainerBuild(
args.additionalCacheFroms.forEach(cacheFrom =>
commandArgs.push('--cache-from', cacheFrom),
);
if (args.cacheTo) {
commandArgs.push('--cache-to', args.cacheTo);
}
}
return await runSpecCliJsonCommand<DevContainerCliBuildResult>({
args: commandArgs,
Expand Down
12 changes: 10 additions & 2 deletions common/src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function buildImage(
subFolder: string,
skipContainerUserIdUpdate: boolean,
cacheFrom: string[],
cacheTo: string | undefined,
): Promise<string> {
const folder = path.join(checkoutPath, subFolder);
const devcontainerJsonPath = path.join(
Expand All @@ -37,6 +38,7 @@ export async function buildImage(
folder,
devcontainerConfig,
cacheFrom,
cacheTo,
);

if (!devcontainerConfig.remoteUser || skipContainerUserIdUpdate == true) {
Expand All @@ -61,6 +63,7 @@ async function buildImageBase(
folder: string,
devcontainerConfig: config.DevContainerConfig,
cacheFrom: string[],
cacheTo: string | undefined,
): Promise<void> {
const configDockerfile = config.getDockerfile(devcontainerConfig);
if (!configDockerfile) {
Expand All @@ -85,8 +88,13 @@ async function buildImageBase(
);
}
cacheFrom.forEach(cacheValue => args.push('--cache-from', cacheValue));
args.push('--cache-to');
args.push('type=inline');
if (cacheTo) {
args.push('--cache-to');
args.push(cacheTo);
} else {
args.push('--cache-to');
args.push('type=inline');
}
args.push('--output=type=docker');

const buildArgs = devcontainerConfig.build?.args;
Expand Down
2 changes: 2 additions & 0 deletions github-action/src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function buildImage(
subFolder: string,
skipContainerUserIdUpdate: boolean,
cacheFrom: string[],
cacheTo: string | undefined,
): Promise<string> {
core.startGroup('🏗 Building dev container...');
try {
Expand All @@ -23,6 +24,7 @@ export async function buildImage(
subFolder,
skipContainerUserIdUpdate,
cacheFrom,
cacheTo,
);
} catch (error) {
core.setFailed(error);
Expand Down
2 changes: 2 additions & 0 deletions github-action/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function runMain(): Promise<void> {
const inputEnvsWithDefaults = populateDefaults(inputEnvs, inheritEnv);
const cacheFrom: string[] = core.getMultilineInput('cacheFrom');
const noCache: boolean = core.getBooleanInput('noCache');
const cacheTo = emptyStringAsUndefined(core.getInput('cacheTo'));
const skipContainerUserIdUpdate = core.getBooleanInput(
'skipContainerUserIdUpdate',
);
Expand Down Expand Up @@ -121,6 +122,7 @@ export async function runMain(): Promise<void> {
userDataFolder,
output: buildxOutput,
noCache,
cacheTo,
};
const result = await devcontainer.build(args, log);

Expand Down

0 comments on commit 80f211c

Please sign in to comment.