Skip to content

Commit

Permalink
feat: add docker layer caching support (#147)
Browse files Browse the repository at this point in the history
see #146

Co-authored-by: Antoine CORDIER <acordier@chopin.local>
Co-authored-by: Kyle a.k.a. TechSquidTV <33272306+KyleTryon@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 29, 2022
1 parent d9db669 commit 34a9dfa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/lib/Components/Commands/exports/Native/SetupRemoteDocker.ts
Expand Up @@ -31,6 +31,17 @@ export class SetupRemoteDocker implements Command {
get generableType(): GenerableType {
return GenerableType.SETUP_REMOTE_DOCKER;
}

/**
* Enable docker image layer caching
* @param enabled - If true, docker layer caching is enabled for the job.
* @returns SetupRemoteDocker - The current instance of the SetupRemoteDocker Command.
* @see {@link https://circleci.com/docs/2.0/docker-layer-caching/}
*/
setDockerLayerCaching(enabled: boolean): SetupRemoteDocker {
this.parameters.docker_layer_caching = enabled;
return this;
}
}

/**
Expand All @@ -42,6 +53,7 @@ export interface SetupRemoteDockerParameters extends CommandParameters {
* Will be interpreted relative to the working_directory of the job.
*/
version: StringParameter;
docker_layer_caching?: boolean;
}

/**
Expand Down
22 changes: 21 additions & 1 deletion src/lib/Components/Executors/exports/MachineExecutor.ts
Expand Up @@ -16,13 +16,22 @@ export class MachineExecutor extends Executor<MachineResourceClass> {
* @see - https://circleci.com/developer/machine
*/
image = 'ubuntu-2004:202010-01';
constructor(resource_class: MachineResourceClass = 'medium', image?: string) {
docker_layer_caching?: boolean;

constructor(
resource_class: MachineResourceClass = 'medium',
image?: string,
docker_layer_caching?: boolean,
) {
super(resource_class);
this.image = image || this.image;
this.docker_layer_caching = docker_layer_caching;
}

generateContents(): MachineExecutorShape {
return {
image: this.image,
docker_layer_caching: this.docker_layer_caching,
};
}

Expand All @@ -33,4 +42,15 @@ export class MachineExecutor extends Executor<MachineResourceClass> {
get executorLiteral(): ExecutorLiteral {
return 'machine';
}

/**
* Enable docker image layer caching
* @param enabled - If true, docker layer caching is enabled for the machine executor.
* @returns MachineExecutor - The current instance of the MachineExecutor Command.
* @see {@link https://circleci.com/docs/2.0/docker-layer-caching/}
*/
setDockerLayerCaching(enabled: boolean): MachineExecutor {
this.docker_layer_caching = enabled;
return this;
}
}
14 changes: 14 additions & 0 deletions tests/Commands.test.ts
Expand Up @@ -83,6 +83,20 @@ describe('Instantiate a Setup_Remote_Docker step', () => {
);
expect(srdExample.name).toBe('setup_remote_docker');
});

const srdWithDlcExample =
new CircleCI.commands.SetupRemoteDocker().setDockerLayerCaching(true);

const srdWithDlcResult = {
setup_remote_docker: {
version: '20.10.6',
docker_layer_caching: true,
},
};

it('Should produce setup_remote_docker step and enable docker layer caching', () => {
expect(srdWithDlcExample.generate()).toEqual(srdWithDlcResult);
});
});

describe('Save and load cache', () => {
Expand Down
6 changes: 5 additions & 1 deletion tests/Executor.test.ts
Expand Up @@ -315,7 +315,10 @@ describe('Generate a config with a Reusable Executor with parameters', () => {
describe('Generate a config with a Reusable Executor', () => {
const myConfig = new CircleCI.Config();

const machine = new CircleCI.executors.MachineExecutor('large');
const machine = new CircleCI.executors.MachineExecutor(
'large',
).setDockerLayerCaching(true);

const dockerBase = new CircleCI.executors.DockerExecutor(
'cimg/base:<< parameters.tag >>',
);
Expand Down Expand Up @@ -370,6 +373,7 @@ describe('Generate a config with a Reusable Executor', () => {
default: {
machine: {
image: 'ubuntu-2004:202010-01',
docker_layer_caching: true,
},
parameters: {
version: {
Expand Down

0 comments on commit 34a9dfa

Please sign in to comment.