Skip to content

Commit 3998f9e

Browse files
Merge pull request #300 from sebst/feature/cache-from-to
Add `cacheTo` argument to `ci` action
2 parents 4f6b93e + 2b6980d commit 3998f9e

File tree

12 files changed

+44
-2
lines changed

12 files changed

+44
-2
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ inputs:
6363
required: false
6464
default: false
6565
description: Builds the image with `--no-cache` (takes precedence over `cacheFrom`)
66+
cacheTo:
67+
required: false
68+
description: Specify the image to cache the built image to
6669
outputs:
6770
runCmdOutput:
6871
description: The output of the command specified in the runCmd input

azdo-task/DevcontainersCi/src/docker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export async function buildImage(
1212
subFolder: string,
1313
skipContainerUserIdUpdate: boolean,
1414
cacheFrom: string[],
15+
cacheTo: string[],
1516
): Promise<string> {
1617
console.log('🏗 Building dev container...');
1718
try {
@@ -23,6 +24,7 @@ export async function buildImage(
2324
subFolder,
2425
skipContainerUserIdUpdate,
2526
cacheFrom,
27+
cacheTo,
2628
);
2729
} catch (error) {
2830
task.setResult(task.TaskResult.Failed, error);

azdo-task/DevcontainersCi/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export async function runMain(): Promise<void> {
4848
const inputEnvsWithDefaults = populateDefaults(envs, inheritEnv);
4949
const cacheFrom = task.getInput('cacheFrom')?.split('\n') ?? [];
5050
const noCache = (task.getInput('noCache') ?? 'false') === 'true';
51+
const cacheTo = task.getInput('cacheTo')?.split('\n') ?? [];
5152
const skipContainerUserIdUpdate =
5253
(task.getInput('skipContainerUserIdUpdate') ?? 'false') === 'true';
5354

@@ -101,6 +102,7 @@ export async function runMain(): Promise<void> {
101102
additionalCacheFroms: cacheFrom,
102103
output: buildxOutput,
103104
noCache,
105+
cacheTo,
104106
};
105107

106108
console.log('\n\n');

azdo-task/DevcontainersCi/task.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@
122122
"type": "boolean",
123123
"label": "Builds the image with `--no-cache` (takes precedence over `cacheFrom`)",
124124
"required": false
125+
},
126+
{
127+
"name": "cacheTo",
128+
"type": "multiLine",
129+
"label": "Specify the image to cache the built image to",
130+
"required": false
125131
}
126132
],
127133
"outputVariables": [{

azdo-task/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ In the example above, the devcontainer-build-run will perform the following step
8282
| 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) |
8383
| cacheFrom | false | Specify additional images to use for build caching |
8484
| noCache | false | Builds the image with `--no-cache` (takes precedence over `cacheFrom`) |
85+
| cacheTo | false | Specify the image to cache the built image to |
8586
| 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. |
8687

8788
## Outputs

common/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface DevContainerConfig {
1515
context?: string;
1616
args?: Record<string, string>;
1717
cacheFrom?: string | string[];
18+
cacheTo?: string | string[];
1819
};
1920
runArgs?: string[];
2021
mounts?: string[];

common/src/dev-container-cli.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export interface DevContainerCliBuildArgs {
161161
userDataFolder?: string;
162162
output?: string,
163163
noCache?: boolean,
164+
cacheTo?: string[],
164165
}
165166
async function devContainerBuild(
166167
args: DevContainerCliBuildArgs,
@@ -195,6 +196,11 @@ async function devContainerBuild(
195196
commandArgs.push('--cache-from', cacheFrom),
196197
);
197198
}
199+
if (args.cacheTo) {
200+
args.cacheTo.forEach(cacheTo =>
201+
commandArgs.push('--cache-to', cacheTo),
202+
);
203+
}
198204
return await runSpecCliJsonCommand<DevContainerCliBuildResult>({
199205
args: commandArgs,
200206
log,
@@ -211,6 +217,7 @@ export interface DevContainerCliUpArgs {
211217
workspaceFolder: string;
212218
configFile: string | undefined;
213219
additionalCacheFroms?: string[];
220+
cacheTo?: string[];
214221
skipContainerUserIdUpdate?: boolean;
215222
env?: string[];
216223
userDataFolder?: string;
@@ -235,6 +242,11 @@ async function devContainerUp(
235242
commandArgs.push('--cache-from', cacheFrom),
236243
);
237244
}
245+
if (args.cacheTo) {
246+
args.cacheTo.forEach(cacheTo =>
247+
commandArgs.push('--cache-to', cacheTo),
248+
);
249+
}
238250
if (args.userDataFolder) {
239251
commandArgs.push("--user-data-folder", args.userDataFolder);
240252
}

common/src/docker.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export async function buildImage(
2121
subFolder: string,
2222
skipContainerUserIdUpdate: boolean,
2323
cacheFrom: string[],
24+
cacheTo: string[],
2425
): Promise<string> {
2526
const folder = path.join(checkoutPath, subFolder);
2627
const devcontainerJsonPath = path.join(
@@ -37,6 +38,7 @@ export async function buildImage(
3738
folder,
3839
devcontainerConfig,
3940
cacheFrom,
41+
cacheTo,
4042
);
4143

4244
if (!devcontainerConfig.remoteUser || skipContainerUserIdUpdate == true) {
@@ -61,6 +63,7 @@ async function buildImageBase(
6163
folder: string,
6264
devcontainerConfig: config.DevContainerConfig,
6365
cacheFrom: string[],
66+
cacheTo: string[],
6467
): Promise<void> {
6568
const configDockerfile = config.getDockerfile(devcontainerConfig);
6669
if (!configDockerfile) {
@@ -85,8 +88,14 @@ async function buildImageBase(
8588
);
8689
}
8790
cacheFrom.forEach(cacheValue => args.push('--cache-from', cacheValue));
88-
args.push('--cache-to');
89-
args.push('type=inline');
91+
if (cacheTo) {
92+
coerceToArray(cacheTo).forEach(cacheValue =>
93+
args.push('--cache-to', cacheValue),
94+
);
95+
} else {
96+
args.push('--cache-to');
97+
args.push('type=inline');
98+
}
9099
args.push('--output=type=docker');
91100

92101
const buildArgs = devcontainerConfig.build?.args;

docs/azure-devops-task.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ In the example above, the devcontainer-build-run will perform the following step
8282
| 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) |
8383
| cacheFrom | false | Specify additional images to use for build caching |
8484
| noCache | false | Builds the image with `--no-cache` (takes precedence over `cacheFrom`) |
85+
| cacheTo | false | Specify the image to cache the built image to |
8586
| 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. |
8687

8788
## Outputs

docs/github-action.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ The [`devcontainers/ci` action](https://github.com/marketplace/actions/devcontai
141141
| 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) |
142142
| cacheFrom | false | Specify additional images to use for build caching |
143143
| noCache | false | Builds the image with `--no-cache` (takes precedence over `cacheFrom`) |
144+
| cacheTo | false | Specify the image to cache the built image to |
144145
| 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. |
145146

146147
## Outputs

0 commit comments

Comments
 (0)