Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cacheTo argument to ci action #300

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
sebst marked this conversation as resolved.
Show resolved Hide resolved
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[],
): 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')?.split('\n') ?? [];
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",
sebst marked this conversation as resolved.
Show resolved Hide resolved
"type": "multiLine",
"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 | string[];
};
runArgs?: string[];
mounts?: string[];
Expand Down
12 changes: 12 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 @@ -195,6 +196,11 @@ async function devContainerBuild(
commandArgs.push('--cache-from', cacheFrom),
);
}
if (args.cacheTo) {
args.cacheTo.forEach(cacheTo =>
commandArgs.push('--cache-to', cacheTo),
);
}
return await runSpecCliJsonCommand<DevContainerCliBuildResult>({
args: commandArgs,
log,
Expand All @@ -211,6 +217,7 @@ export interface DevContainerCliUpArgs {
workspaceFolder: string;
configFile: string | undefined;
additionalCacheFroms?: string[];
cacheTo?: string[];
skipContainerUserIdUpdate?: boolean;
env?: string[];
userDataFolder?: string;
Expand All @@ -235,6 +242,11 @@ async function devContainerUp(
commandArgs.push('--cache-from', cacheFrom),
);
}
if (args.cacheTo) {
args.cacheTo.forEach(cacheTo =>
commandArgs.push('--cache-to', cacheTo),
);
}
if (args.userDataFolder) {
commandArgs.push("--user-data-folder", args.userDataFolder);
}
Expand Down
13 changes: 11 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[],
): 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[],
): Promise<void> {
const configDockerfile = config.getDockerfile(devcontainerConfig);
if (!configDockerfile) {
Expand All @@ -85,8 +88,14 @@ async function buildImageBase(
);
}
cacheFrom.forEach(cacheValue => args.push('--cache-from', cacheValue));
args.push('--cache-to');
args.push('type=inline');
if (cacheTo) {
coerceToArray(cacheTo).forEach(cacheValue =>
args.push('--cache-to', cacheValue),
);
} else {
args.push('--cache-to');
args.push('type=inline');
}
args.push('--output=type=docker');

const buildArgs = devcontainerConfig.build?.args;
Expand Down
1 change: 1 addition & 0 deletions docs/azure-devops-task.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 docs/github-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ The [`devcontainers/ci` action](https://github.com/marketplace/actions/devcontai
| 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
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[],
): 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: string[] = core.getMultilineInput('cacheFrom');
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