Skip to content

Commit 4665dd6

Browse files
author
Rishi Barad
authored
Add an option to clean up docker images after ECR push (#460)
* Add an option to clean up docker images after ECR push add rm param to test fix broken test modify ecr push tests use force remove to untag image references update message move -f to additionalCommandLineOptions and update tests * add changelog * refactor taskParameters
1 parent b2a1fea commit 4665dd6

File tree

7 files changed

+35
-8
lines changed

7 files changed

+35
-8
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Feature",
3+
"description": "Add an option to remove docker image after an ECR push"
4+
}

src/tasks/ECRPushImage/TaskOperations.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ export class TaskOperations {
8383
tl.setVariable(this.taskParameters.outputVariable, targetImageRef)
8484
}
8585

86+
if (this.taskParameters.removeDockerImage) {
87+
await this.removeDockerImage(sourceImageRef)
88+
}
89+
8690
console.log(tl.loc('TaskCompleted'))
8791
}
8892

@@ -118,4 +122,9 @@ export class TaskOperations {
118122
console.log(tl.loc('PushingImage', imageRef))
119123
await this.dockerHandler.runDockerCommand(this.dockerPath, 'push', [imageRef])
120124
}
125+
126+
private async removeDockerImage(imageRef: string): Promise<void> {
127+
console.log(tl.loc('RemovingImage', imageRef))
128+
await this.dockerHandler.runDockerCommand(this.dockerPath, 'rmi', [imageRef], '-f')
129+
}
121130
}

src/tasks/ECRPushImage/TaskParameters.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface TaskParameters {
2020
pushTag: string
2121
autoCreateRepository: boolean
2222
forceDockerNamingConventions: boolean
23+
removeDockerImage: boolean
2324
outputVariable: string
2425
}
2526

@@ -31,6 +32,7 @@ export function buildTaskParameters(): TaskParameters {
3132
pushTag: getInputOrEmpty('pushTag'),
3233
autoCreateRepository: tl.getBoolInput('autoCreateRepository', false),
3334
forceDockerNamingConventions: tl.getBoolInput('forceDockerNamingConventions', false),
35+
removeDockerImage: tl.getBoolInput('removeDockerImage', false),
3436
outputVariable: getInputOrEmpty('outputVariable'),
3537
sourceImageName: '',
3638
sourceImageId: '',

src/tasks/ECRPushImage/task.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"imageid": "Image ID"
5656
},
5757
"properties": {
58-
"EditableOptions": "false"
58+
"EditableOptions": "False"
5959
}
6060
},
6161
{
@@ -117,6 +117,14 @@
117117
"required": false,
118118
"helpMarkDown": "If enabled, the Docker repository name will be modified to follow Docker naming conventions. Converts upper case characters to lower case. Removes all characters except 0-9, -, . and _ ."
119119
},
120+
{
121+
"name": "removeDockerImage",
122+
"label": "Remove Docker image after ECR push",
123+
"type": "boolean",
124+
"defaultValue": false,
125+
"required": false,
126+
"helpMarkDown": "If enabled, this command removes the image and untags any references to it"
127+
},
120128
{
121129
"name": "outputVariable",
122130
"type": "string",
@@ -157,6 +165,7 @@
157165
"RequestingAuthToken": "Obtaining authentication token for ECR login",
158166
"AddingTag": "Adding tag '%s' to image '%s'",
159167
"PushingImage": "Pushing image '%s' to Elastic Container Registry",
168+
"RemovingImage": "Removing image '%s' and untagging any references to the image",
160169
"InvokingDockerCommand": "Invoking '%s' with command '%s'",
161170
"TestingForRepository": "Testing existence of repository '%s'",
162171
"CreatingRepository": "Repository not found, attempting to create",

src/tasks/SystemsManagerGetParameter/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
"UsingParameterNameForVariable": "Using original parameter name %s for build variable",
193193
"TaskCompleted": "Successfully created variables from parameter value(s).",
194194
"InvalidParameterVersion": "%s is not a valid parameter version number. Version numbers should be >= 1",
195-
"UnknownReadMode": "%s is not a valid paramater for ReadMode. Paramater must be one of \"single\" or \"hierarchy\"",
195+
"UnknownReadMode": "%s is not a valid parameter for ReadMode. Parameter must be one of \"single\" or \"hierarchy\"",
196196
"ErrorParametersEmpty": "Response did not have any parameters attached!"
197197
}
198198
}

tests/taskTests/ecrPushImage/ecrPushImage-test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const defaultTaskParameters: TaskParameters = {
2222
pushTag: '',
2323
autoCreateRepository: false,
2424
forceDockerNamingConventions: false,
25+
removeDockerImage: false,
2526
outputVariable: ''
2627
}
2728

@@ -85,19 +86,21 @@ describe('ECR Push image', () => {
8586
})
8687

8788
test('Runs docker commands', async () => {
88-
expect.assertions(5)
89+
expect.assertions(6)
8990
const ecr = new ECR() as any
9091
ecr.getAuthorizationToken = jest.fn(() => ecrReturnsToken)
9192
const dockerHandler = { ...defaultDocker }
92-
const runDockerCommand = jest.fn(async (thing1, thing2, thing3) => undefined)
93+
const runDockerCommand = jest.fn(async (thing1, thing2, thing3, thing4) => undefined)
9394
dockerHandler.runDockerCommand = runDockerCommand
94-
const taskOperations = new TaskOperations(ecr, dockerHandler, defaultTaskParameters)
95+
const taskParameters = { ...defaultTaskParameters, removeDockerImage: true }
96+
const taskOperations = new TaskOperations(ecr, dockerHandler, taskParameters)
9597
await taskOperations.execute()
9698
expect(ecr.getAuthorizationToken).toBeCalledTimes(1)
97-
expect(runDockerCommand).toBeCalledTimes(3)
99+
expect(runDockerCommand).toBeCalledTimes(4)
98100
expect(runDockerCommand.mock.calls[0][1]).toBe('tag')
99101
expect(runDockerCommand.mock.calls[1][1]).toBe('login')
100102
expect(runDockerCommand.mock.calls[2][1]).toBe('push')
103+
expect(runDockerCommand.mock.calls[3][1]).toBe('rmi')
101104
})
102105

103106
test('autocreate creates repository', async () => {

tests/taskTests/systemsManagerGetParameter/systemsManagerGetParameter-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ describe('Systems Manager Get Parameter', () => {
7676
expect(new TaskOperations(new SSM(), defaultTaskParameters)).not.toBeNull()
7777
})
7878

79-
test('Read mode unknown throws', () => {
79+
test('Read mode unknown throws', async () => {
8080
expect.assertions(1)
8181
const taskOperations = new TaskOperations(new SSM(), defaultTaskParameters)
82-
taskOperations.execute().catch(e => expect(e).toContain('is not a valid parameter'))
82+
await taskOperations.execute().catch(e => expect(`${e}`).toContain('is not a valid parameter'))
8383
})
8484

8585
test('Read mode single reads', async () => {

0 commit comments

Comments
 (0)