Skip to content

Commit 5ae7be6

Browse files
guikcdkg-aws
andauthored
feat: Enable AWS managed tags (#622)
* feat: enable ecs managed tags --------- Co-authored-by: Kishan Gajjar <[email protected]>
1 parent 6b5f48e commit 5ae7be6

File tree

5 files changed

+72
-27
lines changed

5 files changed

+72
-27
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ The task definition file can be updated prior to deployment with the new contain
104104
wait-for-service-stability: true
105105
```
106106

107+
### Tags
108+
109+
To turn on [Amazon ECS-managed tags](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-using-tags.html#managed-tags) `aws:ecs:serviceName` and `aws:ecs:clusterName` for the tasks in the service or the standalone tasks by setting `enable-ecs-managed-tags`:
110+
111+
```yaml
112+
- name: Deploy Amazon ECS task definition
113+
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
114+
with:
115+
task-definition: task-definition.json
116+
service: my-service
117+
cluster: my-cluster
118+
wait-for-service-stability: true
119+
enable-ecs-managed-tags: true
120+
```
121+
107122
## Credentials and Region
108123

109124
This action relies on the [default behavior of the AWS SDK for Javascript](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html) to determine AWS credentials and region.

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'

dist/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const IGNORED_TASK_DEFINITION_ATTRIBUTES = [
2727
];
2828

2929
// Method to run a stand-alone task with desired inputs
30-
async function runTask(ecs, clusterName, taskDefArn, waitForMinutes) {
30+
async function runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSManagedTags) {
3131
core.info('Running task')
3232

3333
const waitForTask = core.getInput('wait-for-task-stopped', { required: false }) || 'false';
@@ -60,7 +60,8 @@ async function runTask(ecs, clusterName, taskDefArn, waitForMinutes) {
6060
containerOverrides: containerOverrides
6161
},
6262
launchType: launchType,
63-
networkConfiguration: Object.keys(awsvpcConfiguration).length === 0 ? null : { awsvpcConfiguration: awsvpcConfiguration }
63+
networkConfiguration: Object.keys(awsvpcConfiguration).length === 0 ? null : { awsvpcConfiguration: awsvpcConfiguration },
64+
enableECSManagedTags: enableECSManagedTags
6465
});
6566

6667
core.debug(`Run task response ${JSON.stringify(runTaskResponse)}`)
@@ -134,14 +135,15 @@ async function tasksExitCode(ecs, clusterName, taskArns) {
134135
}
135136

136137
// Deploy to a service that uses the 'ECS' deployment controller
137-
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount) {
138+
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags) {
138139
core.debug('Updating the service');
139140

140141
let params = {
141142
cluster: clusterName,
142143
service: service,
143144
taskDefinition: taskDefArn,
144-
forceNewDeployment: forceNewDeployment
145+
forceNewDeployment: forceNewDeployment,
146+
enableECSManagedTags: enableECSManagedTags
145147
};
146148

147149
// Add the desiredCount property only if it is defined and a number.
@@ -397,6 +399,8 @@ async function run() {
397399
const forceNewDeployInput = core.getInput('force-new-deployment', { required: false }) || 'false';
398400
const forceNewDeployment = forceNewDeployInput.toLowerCase() === 'true';
399401
const desiredCount = parseInt((core.getInput('desired-count', {required: false})));
402+
const enableECSManagedTagsInput = core.getInput('enable-ecs-managed-tags', { required: false }) || 'false';
403+
const enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true';
400404

401405
// Register the task definition
402406
core.debug('Registering the task definition');
@@ -424,7 +428,7 @@ async function run() {
424428
core.debug(`shouldRunTask: ${shouldRunTask}`);
425429
if (shouldRunTask) {
426430
core.debug("Running ad-hoc task...");
427-
await runTask(ecs, clusterName, taskDefArn, waitForMinutes);
431+
await runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSManagedTags);
428432
}
429433

430434
// Update the service with the new task definition
@@ -448,7 +452,7 @@ async function run() {
448452
if (!serviceResponse.deploymentController || !serviceResponse.deploymentController.type || serviceResponse.deploymentController.type === 'ECS') {
449453
// Service uses the 'ECS' deployment controller, so we can call UpdateService
450454
core.debug('Updating service...');
451-
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount);
455+
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags);
452456

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

index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const IGNORED_TASK_DEFINITION_ATTRIBUTES = [
2121
];
2222

2323
// Method to run a stand-alone task with desired inputs
24-
async function runTask(ecs, clusterName, taskDefArn, waitForMinutes) {
24+
async function runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSManagedTags) {
2525
core.info('Running task')
2626

2727
const waitForTask = core.getInput('wait-for-task-stopped', { required: false }) || 'false';
@@ -54,7 +54,8 @@ async function runTask(ecs, clusterName, taskDefArn, waitForMinutes) {
5454
containerOverrides: containerOverrides
5555
},
5656
launchType: launchType,
57-
networkConfiguration: Object.keys(awsvpcConfiguration).length === 0 ? null : { awsvpcConfiguration: awsvpcConfiguration }
57+
networkConfiguration: Object.keys(awsvpcConfiguration).length === 0 ? null : { awsvpcConfiguration: awsvpcConfiguration },
58+
enableECSManagedTags: enableECSManagedTags
5859
});
5960

6061
core.debug(`Run task response ${JSON.stringify(runTaskResponse)}`)
@@ -128,14 +129,15 @@ async function tasksExitCode(ecs, clusterName, taskArns) {
128129
}
129130

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

134135
let params = {
135136
cluster: clusterName,
136137
service: service,
137138
taskDefinition: taskDefArn,
138-
forceNewDeployment: forceNewDeployment
139+
forceNewDeployment: forceNewDeployment,
140+
enableECSManagedTags: enableECSManagedTags
139141
};
140142

141143
// Add the desiredCount property only if it is defined and a number.
@@ -391,6 +393,8 @@ async function run() {
391393
const forceNewDeployInput = core.getInput('force-new-deployment', { required: false }) || 'false';
392394
const forceNewDeployment = forceNewDeployInput.toLowerCase() === 'true';
393395
const desiredCount = parseInt((core.getInput('desired-count', {required: false})));
396+
const enableECSManagedTagsInput = core.getInput('enable-ecs-managed-tags', { required: false }) || 'false';
397+
const enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true';
394398

395399
// Register the task definition
396400
core.debug('Registering the task definition');
@@ -418,7 +422,7 @@ async function run() {
418422
core.debug(`shouldRunTask: ${shouldRunTask}`);
419423
if (shouldRunTask) {
420424
core.debug("Running ad-hoc task...");
421-
await runTask(ecs, clusterName, taskDefArn, waitForMinutes);
425+
await runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSManagedTags);
422426
}
423427

424428
// Update the service with the new task definition
@@ -442,7 +446,7 @@ async function run() {
442446
if (!serviceResponse.deploymentController || !serviceResponse.deploymentController.type || serviceResponse.deploymentController.type === 'ECS') {
443447
// Service uses the 'ECS' deployment controller, so we can call UpdateService
444448
core.debug('Updating service...');
445-
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount);
449+
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags);
446450

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

index.test.js

Lines changed: 34 additions & 15 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

@@ -1113,6 +1121,7 @@ describe('Deploy to ECS', () => {
11131121
.mockReturnValueOnce('') // cluster
11141122
.mockReturnValueOnce('') // wait-for-service-stability
11151123
.mockReturnValueOnce('') // wait-for-minutes
1124+
.mockReturnValueOnce('') // enable-ecs-managed-tags
11161125
.mockReturnValueOnce('') // force-new-deployment
11171126
.mockReturnValueOnce('') // desired-count
11181127
.mockReturnValueOnce('true'); // run-task
@@ -1129,7 +1138,8 @@ describe('Deploy to ECS', () => {
11291138
launchType: 'FARGATE',
11301139
taskDefinition: 'task:def:arn',
11311140
overrides: {"containerOverrides": []},
1132-
networkConfiguration: null
1141+
networkConfiguration: null,
1142+
enableECSManagedTags: false
11331143
});
11341144

11351145
expect(core.setOutput).toHaveBeenNthCalledWith(2, 'run-task-arn', ["arn:aws:ecs:fake-region:account_id:task/arn"]);
@@ -1145,6 +1155,7 @@ describe('Deploy to ECS', () => {
11451155
.mockReturnValueOnce('') // wait-for-minutes
11461156
.mockReturnValueOnce('') // force-new-deployment
11471157
.mockReturnValueOnce('') // desired-count
1158+
.mockReturnValueOnce('true') // enable-ecs-managed-tags
11481159
.mockReturnValueOnce('true') // run-task
11491160
.mockReturnValueOnce('false') // wait-for-task-stopped
11501161
.mockReturnValueOnce('someJoe') // run-task-started-by
@@ -1164,7 +1175,8 @@ describe('Deploy to ECS', () => {
11641175
launchType: 'EC2',
11651176
taskDefinition: 'task:def:arn',
11661177
overrides: { containerOverrides: [{ name: 'someapp', command: 'somecmd' }] },
1167-
networkConfiguration: { awsvpcConfiguration: { subnets: ['a', 'b'], securityGroups: ['c', 'd'], assignPublicIp: "DISABLED" } }
1178+
networkConfiguration: { awsvpcConfiguration: { subnets: ['a', 'b'], securityGroups: ['c', 'd'], assignPublicIp: "DISABLED" } },
1179+
enableECSManagedTags: true
11681180
});
11691181
expect(core.setOutput).toHaveBeenNthCalledWith(2, 'run-task-arn', ["arn:aws:ecs:fake-region:account_id:task/arn"]);
11701182
});
@@ -1177,8 +1189,9 @@ describe('Deploy to ECS', () => {
11771189
.mockReturnValueOnce('somecluster') // cluster
11781190
.mockReturnValueOnce('true') // wait-for-service-stability
11791191
.mockReturnValueOnce('') // wait-for-minutes
1180-
.mockReturnValueOnce('') // force-new-deployment
1192+
.mockReturnValueOnce('') // force-new-deployment
11811193
.mockReturnValueOnce('') // desired-count
1194+
.mockReturnValueOnce('') // enable-ecs-managed-tags
11821195
.mockReturnValueOnce('true') // run-task
11831196
.mockReturnValueOnce('false') // wait-for-task-stopped
11841197
.mockReturnValueOnce('someJoe') // run-task-started-by
@@ -1200,15 +1213,17 @@ describe('Deploy to ECS', () => {
12001213
cluster: 'somecluster',
12011214
service: 'service-456',
12021215
taskDefinition: 'task:def:arn',
1203-
forceNewDeployment: false
1216+
forceNewDeployment: false,
1217+
enableECSManagedTags: false
12041218
});
12051219
expect(mockRunTask).toHaveBeenCalledWith({
12061220
startedBy: 'someJoe',
12071221
cluster: 'somecluster',
12081222
taskDefinition: 'task:def:arn',
12091223
launchType: 'EC2',
12101224
overrides: { containerOverrides: [{ name: 'someapp', command: 'somecmd' }] },
1211-
networkConfiguration: { awsvpcConfiguration: { subnets: ['a', 'b'], securityGroups: ['c', 'd'], assignPublicIp: "DISABLED" } }
1225+
networkConfiguration: { awsvpcConfiguration: { subnets: ['a', 'b'], securityGroups: ['c', 'd'], assignPublicIp: "DISABLED" } },
1226+
enableECSManagedTags: false
12121227
});
12131228
expect(core.setOutput).toHaveBeenNthCalledWith(2, 'run-task-arn', ["arn:aws:ecs:fake-region:account_id:task/arn"]);
12141229
});
@@ -1223,6 +1238,7 @@ describe('Deploy to ECS', () => {
12231238
.mockReturnValueOnce('') // wait-for-minutes
12241239
.mockReturnValueOnce('') // force-new-deployment
12251240
.mockReturnValueOnce('') // desired-count
1241+
.mockReturnValueOnce('') // enable-ecs-managed-tags
12261242
.mockReturnValueOnce('true') // run-task
12271243
.mockReturnValueOnce('true'); // wait-for-task-stopped
12281244

@@ -1244,6 +1260,7 @@ describe('Deploy to ECS', () => {
12441260
.mockReturnValueOnce('somecluster') // cluster
12451261
.mockReturnValueOnce('true') // wait-for-service-stability
12461262
.mockReturnValueOnce('') // wait-for-minutes
1263+
.mockReturnValueOnce('') // enable-ecs-managed-tags
12471264
.mockReturnValueOnce('') // force-new-deployment
12481265
.mockReturnValueOnce('') // desired-count
12491266
.mockReturnValueOnce('true') // run-task
@@ -1262,7 +1279,8 @@ describe('Deploy to ECS', () => {
12621279
taskDefinition: 'task:def:arn',
12631280
launchType: 'EC2',
12641281
overrides: { containerOverrides: [] },
1265-
networkConfiguration: null
1282+
networkConfiguration: null,
1283+
enableECSManagedTags: false
12661284
});
12671285
});
12681286

@@ -1317,8 +1335,9 @@ describe('Deploy to ECS', () => {
13171335
.mockReturnValueOnce('') // wait-for-minutes
13181336
.mockReturnValueOnce('') // force-new-deployment
13191337
.mockReturnValueOnce('') // desired-count
1338+
.mockReturnValueOnce('') // enable-ecs-managed-tags
13201339
.mockReturnValueOnce('true') // run-task
1321-
.mockReturnValueOnce('false'); // wait-for-task-stopped
1340+
.mockReturnValueOnce('false'); // wait-for-task-stopped
13221341

13231342
mockRunTask.mockImplementation(
13241343
() => Promise.resolve({
@@ -1427,4 +1446,4 @@ describe('Deploy to ECS', () => {
14271446
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'Failed to register task definition in ECS: Could not parse');
14281447
expect(core.setFailed).toHaveBeenNthCalledWith(2, 'Could not parse');
14291448
});
1430-
});
1449+
});

0 commit comments

Comments
 (0)