Skip to content

Commit f103f60

Browse files
committed
feat: enable ecs managed tags
1 parent 6b5f48e commit f103f60

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ inputs:
6464
wait-for-task-stopped:
6565
description: 'Whether to wait for the task to stop when running it outside of a service. Will default to not wait.'
6666
required: false
67+
enable-ecs-managed-tags:
68+
description: "Determines whether to turn on Amazon ECS managed tags 'aws:ecs:serviceName' and 'aws:ecs:clusterName' for the tasks in the service."
69+
required: false
6770
outputs:
6871
task-definition-arn:
6972
description: 'The ARN of the registered ECS task definition'

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,15 @@ async function tasksExitCode(ecs, clusterName, taskArns) {
128128
}
129129

130130
// Deploy to a service that uses the 'ECS' deployment controller
131-
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount) {
131+
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags) {
132132
core.debug('Updating the service');
133133

134134
let params = {
135135
cluster: clusterName,
136136
service: service,
137137
taskDefinition: taskDefArn,
138-
forceNewDeployment: forceNewDeployment
138+
forceNewDeployment: forceNewDeployment,
139+
enableECSManagedTags: enableECSManagedTags
139140
};
140141

141142
// Add the desiredCount property only if it is defined and a number.
@@ -391,6 +392,8 @@ async function run() {
391392
const forceNewDeployInput = core.getInput('force-new-deployment', { required: false }) || 'false';
392393
const forceNewDeployment = forceNewDeployInput.toLowerCase() === 'true';
393394
const desiredCount = parseInt((core.getInput('desired-count', {required: false})));
395+
const enableECSManagedTagsInput = core.getInput('enable-ecs-managed-tags', { required: false }) || 'false';
396+
const enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true';
394397

395398
// Register the task definition
396399
core.debug('Registering the task definition');
@@ -442,7 +445,7 @@ async function run() {
442445
if (!serviceResponse.deploymentController || !serviceResponse.deploymentController.type || serviceResponse.deploymentController.type === 'ECS') {
443446
// Service uses the 'ECS' deployment controller, so we can call UpdateService
444447
core.debug('Updating service...');
445-
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount);
448+
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags);
446449

447450
} else if (serviceResponse.deploymentController.type === 'CODE_DEPLOY') {
448451
// Service uses CodeDeploy, so we should start a CodeDeploy deployment

index.test.js

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ describe('Deploy to ECS', () => {
182182
cluster: 'cluster-789',
183183
service: 'service-456',
184184
taskDefinition: 'task:def:arn',
185-
forceNewDeployment: false
185+
forceNewDeployment: false,
186+
enableECSManagedTags: false
186187
});
187188
expect(waitUntilServicesStable).toHaveBeenCalledTimes(0);
188189
expect(core.info).toBeCalledWith("Deployment started. Watch this deployment's progress in the Amazon ECS console: https://fake-region.console.aws.amazon.com/ecs/v2/clusters/cluster-789/services/service-456/events?region=fake-region");
@@ -213,7 +214,8 @@ describe('Deploy to ECS', () => {
213214
cluster: 'cluster-789',
214215
service: 'service-456',
215216
taskDefinition: 'task:def:arn',
216-
forceNewDeployment: false
217+
forceNewDeployment: false,
218+
enableECSManagedTags: false
217219
});
218220
expect(waitUntilServicesStable).toHaveBeenCalledTimes(0);
219221
expect(core.info).toBeCalledWith("Deployment started. Watch this deployment's progress in the Amazon ECS console: https://fake-region.console.aws.amazon.com/ecs/v2/clusters/cluster-789/services/service-456/events?region=fake-region");
@@ -708,6 +710,7 @@ describe('Deploy to ECS', () => {
708710
.mockReturnValueOnce('') // force-new-deployment
709711
.mockReturnValueOnce('') // run-task
710712
.mockReturnValueOnce('') // desired count
713+
.mockReturnValueOnce('') // enable-ecs-managed-tags
711714
.mockReturnValueOnce('/hello/appspec.json') // codedeploy-appspec
712715
.mockReturnValueOnce('MyApplication') // codedeploy-application
713716
.mockReturnValueOnce('MyDeploymentGroup'); // codedeploy-deployment-group
@@ -944,7 +947,8 @@ describe('Deploy to ECS', () => {
944947
cluster: 'cluster-789',
945948
service: 'service-456',
946949
taskDefinition: 'task:def:arn',
947-
forceNewDeployment: false
950+
forceNewDeployment: false,
951+
enableECSManagedTags: false
948952
});
949953
expect(waitUntilServicesStable).toHaveBeenNthCalledWith(
950954
1,
@@ -983,7 +987,8 @@ describe('Deploy to ECS', () => {
983987
cluster: 'cluster-789',
984988
service: 'service-456',
985989
taskDefinition: 'task:def:arn',
986-
forceNewDeployment: false
990+
forceNewDeployment: false,
991+
enableECSManagedTags: false
987992
});
988993
expect(waitUntilServicesStable).toHaveBeenNthCalledWith(
989994
1,
@@ -1022,7 +1027,8 @@ describe('Deploy to ECS', () => {
10221027
cluster: 'cluster-789',
10231028
service: 'service-456',
10241029
taskDefinition: 'task:def:arn',
1025-
forceNewDeployment: false
1030+
forceNewDeployment: false,
1031+
enableECSManagedTags: false
10261032
});
10271033
expect(waitUntilServicesStable).toHaveBeenNthCalledWith(
10281034
1,
@@ -1063,7 +1069,8 @@ describe('Deploy to ECS', () => {
10631069
desiredCount: 4,
10641070
service: 'service-456',
10651071
taskDefinition: 'task:def:arn',
1066-
forceNewDeployment: true
1072+
forceNewDeployment: true,
1073+
enableECSManagedTags: false
10671074
});
10681075
});
10691076

@@ -1087,7 +1094,8 @@ describe('Deploy to ECS', () => {
10871094
cluster: 'default',
10881095
service: 'service-456',
10891096
taskDefinition: 'task:def:arn',
1090-
forceNewDeployment: false
1097+
forceNewDeployment: false,
1098+
enableECSManagedTags: false
10911099
});
10921100
});
10931101

@@ -1115,6 +1123,7 @@ describe('Deploy to ECS', () => {
11151123
.mockReturnValueOnce('') // wait-for-minutes
11161124
.mockReturnValueOnce('') // force-new-deployment
11171125
.mockReturnValueOnce('') // desired-count
1126+
.mockReturnValueOnce('') // enable-ecs-managed-tags
11181127
.mockReturnValueOnce('true'); // run-task
11191128

11201129
await run();
@@ -1145,6 +1154,7 @@ describe('Deploy to ECS', () => {
11451154
.mockReturnValueOnce('') // wait-for-minutes
11461155
.mockReturnValueOnce('') // force-new-deployment
11471156
.mockReturnValueOnce('') // desired-count
1157+
.mockReturnValueOnce('') // enable-ecs-managed-tags
11481158
.mockReturnValueOnce('true') // run-task
11491159
.mockReturnValueOnce('false') // wait-for-task-stopped
11501160
.mockReturnValueOnce('someJoe') // run-task-started-by
@@ -1177,8 +1187,9 @@ describe('Deploy to ECS', () => {
11771187
.mockReturnValueOnce('somecluster') // cluster
11781188
.mockReturnValueOnce('true') // wait-for-service-stability
11791189
.mockReturnValueOnce('') // wait-for-minutes
1180-
.mockReturnValueOnce('') // force-new-deployment
1190+
.mockReturnValueOnce('') // force-new-deployment
11811191
.mockReturnValueOnce('') // desired-count
1192+
.mockReturnValueOnce('') // enable-ecs-managed-tags
11821193
.mockReturnValueOnce('true') // run-task
11831194
.mockReturnValueOnce('false') // wait-for-task-stopped
11841195
.mockReturnValueOnce('someJoe') // run-task-started-by
@@ -1200,7 +1211,8 @@ describe('Deploy to ECS', () => {
12001211
cluster: 'somecluster',
12011212
service: 'service-456',
12021213
taskDefinition: 'task:def:arn',
1203-
forceNewDeployment: false
1214+
forceNewDeployment: false,
1215+
enableECSManagedTags: false
12041216
});
12051217
expect(mockRunTask).toHaveBeenCalledWith({
12061218
startedBy: 'someJoe',
@@ -1223,6 +1235,7 @@ describe('Deploy to ECS', () => {
12231235
.mockReturnValueOnce('') // wait-for-minutes
12241236
.mockReturnValueOnce('') // force-new-deployment
12251237
.mockReturnValueOnce('') // desired-count
1238+
.mockReturnValueOnce('') // enable-ecs-managed-tags
12261239
.mockReturnValueOnce('true') // run-task
12271240
.mockReturnValueOnce('true'); // wait-for-task-stopped
12281241

@@ -1246,6 +1259,7 @@ describe('Deploy to ECS', () => {
12461259
.mockReturnValueOnce('') // wait-for-minutes
12471260
.mockReturnValueOnce('') // force-new-deployment
12481261
.mockReturnValueOnce('') // desired-count
1262+
.mockReturnValueOnce('') // enable-ecs-managed-tags
12491263
.mockReturnValueOnce('true') // run-task
12501264
.mockReturnValueOnce('true') // wait-for-task-stopped
12511265
.mockReturnValueOnce('someJoe') // run-task-started-by
@@ -1317,8 +1331,9 @@ describe('Deploy to ECS', () => {
13171331
.mockReturnValueOnce('') // wait-for-minutes
13181332
.mockReturnValueOnce('') // force-new-deployment
13191333
.mockReturnValueOnce('') // desired-count
1334+
.mockReturnValueOnce('') // enable-ecs-managed-tags
13201335
.mockReturnValueOnce('true') // run-task
1321-
.mockReturnValueOnce('false'); // wait-for-task-stopped
1336+
.mockReturnValueOnce('false'); // wait-for-task-stopped
13221337

13231338
mockRunTask.mockImplementation(
13241339
() => Promise.resolve({
@@ -1427,4 +1442,35 @@ describe('Deploy to ECS', () => {
14271442
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'Failed to register task definition in ECS: Could not parse');
14281443
expect(core.setFailed).toHaveBeenNthCalledWith(2, 'Could not parse');
14291444
});
1430-
});
1445+
1446+
test('deployment with enable-ecs-managed-tags', async () => {
1447+
core.getInput = jest
1448+
.fn()
1449+
.mockReturnValueOnce('task-definition.json') // task-definition
1450+
.mockReturnValueOnce('service-456') // service
1451+
.mockReturnValueOnce('cluster-789') // cluster
1452+
.mockReturnValueOnce('false') // wait-for-service-stability
1453+
.mockReturnValueOnce('') // wait-for-minutes
1454+
.mockReturnValueOnce('') // force-new-deployment
1455+
.mockReturnValueOnce('1') // desired count is number
1456+
.mockReturnValueOnce('true'); // enable-ecs-managed-tags
1457+
1458+
await run();
1459+
expect(core.setFailed).toHaveBeenCalledTimes(0);
1460+
1461+
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family' });
1462+
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition-arn', 'task:def:arn');
1463+
expect(mockEcsDescribeServices).toHaveBeenNthCalledWith(1, {
1464+
cluster: 'cluster-789',
1465+
services: ['service-456']
1466+
});
1467+
expect(mockEcsUpdateService).toHaveBeenNthCalledWith(1, {
1468+
cluster: 'cluster-789',
1469+
desiredCount: 1,
1470+
service: 'service-456',
1471+
taskDefinition: 'task:def:arn',
1472+
forceNewDeployment: false,
1473+
enableECSManagedTags: true
1474+
});
1475+
});
1476+
});

0 commit comments

Comments
 (0)