Skip to content

Commit 1b137d4

Browse files
authored
feat: Tags for services and ad-hoc run (#629)
* feat: Get tags from services and set ad-hoc run-task * tags input get in runTask * Add run-task-tags option in existing test
1 parent e9678cb commit 1b137d4

File tree

5 files changed

+127
-24
lines changed

5 files changed

+127
-24
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ To turn on [Amazon ECS-managed tags](https://docs.aws.amazon.com/AmazonECS/lates
119119
enable-ecs-managed-tags: true
120120
```
121121

122+
You can propagate your custom tags from your existing service using `propagate-tags`:
123+
124+
```yaml
125+
- name: Deploy Amazon ECS task definition
126+
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
127+
with:
128+
task-definition: task-definition.json
129+
service: my-service
130+
cluster: my-cluster
131+
wait-for-service-stability: true
132+
propagate-tags: SERVICE
133+
```
134+
122135
## Credentials and Region
123136

124137
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.
@@ -288,6 +301,27 @@ In the following example, the service would not be updated until the ad-hoc task
288301
Overrides and VPC networking options are available as well. See [action.yml](action.yml) for more details. The `FARGATE`
289302
launch type requires `awsvpc` network mode in your task definition and you must specify a network configuration.
290303

304+
### Tags
305+
306+
To tag your tasks:
307+
308+
* to turn on Amazon ECS-managed tags (`aws:ecs:clusterName`), use `enable-ecs-managed-tags`
309+
* for custom tags, use `run-task-tags`
310+
311+
```yaml
312+
- name: Deploy to Amazon ECS
313+
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
314+
with:
315+
task-definition: task-definition.json
316+
service: my-service
317+
cluster: my-cluster
318+
wait-for-service-stability: true
319+
run-task: true
320+
enable-ecs-managed-tags: true
321+
run-task-tags: '[{"key": "project", "value": "myproject"}]'
322+
wait-for-task-stopped: true
323+
```
324+
291325
## Troubleshooting
292326

293327
This action emits debug logs to help troubleshoot deployment failures. To see the debug logs, create a secret named `ACTIONS_STEP_DEBUG` with value `true` in your repository.

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,18 @@ inputs:
6161
run-task-started-by:
6262
description: "A name to use for the startedBy tag when running a task outside of a service. Will default to 'GitHub-Actions'."
6363
required: false
64+
run-task-tags:
65+
description: 'A JSON array of tags'
66+
required: false
6467
wait-for-task-stopped:
6568
description: 'Whether to wait for the task to stop when running it outside of a service. Will default to not wait.'
6669
required: false
6770
enable-ecs-managed-tags:
6871
description: "Determines whether to turn on Amazon ECS managed tags 'aws:ecs:serviceName' and 'aws:ecs:clusterName' for the tasks in the service."
6972
required: false
73+
propagate-tags:
74+
description: "Determines to propagate the tags from the 'SERVICE' to the task. Will default to 'NONE'"
75+
required: false
7076
outputs:
7177
task-definition-arn:
7278
description: 'The ARN of the registered ECS task definition'

dist/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ async function runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSMa
3737
const securityGroupIds = core.getInput('run-task-security-groups', { required: false }) || '';
3838
const containerOverrides = JSON.parse(core.getInput('run-task-container-overrides', { required: false }) || '[]');
3939
const assignPublicIP = core.getInput('run-task-assign-public-IP', { required: false }) || 'DISABLED';
40+
const tags = JSON.parse(core.getInput('run-task-tags', { required: false }) || '[]');
4041

4142
let awsvpcConfiguration = {}
4243

@@ -61,7 +62,8 @@ async function runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSMa
6162
},
6263
launchType: launchType,
6364
networkConfiguration: Object.keys(awsvpcConfiguration).length === 0 ? null : { awsvpcConfiguration: awsvpcConfiguration },
64-
enableECSManagedTags: enableECSManagedTags
65+
enableECSManagedTags: enableECSManagedTags,
66+
tags: tags
6567
});
6668

6769
core.debug(`Run task response ${JSON.stringify(runTaskResponse)}`)
@@ -135,15 +137,16 @@ async function tasksExitCode(ecs, clusterName, taskArns) {
135137
}
136138

137139
// Deploy to a service that uses the 'ECS' deployment controller
138-
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags) {
140+
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags, propagateTags) {
139141
core.debug('Updating the service');
140142

141143
let params = {
142144
cluster: clusterName,
143145
service: service,
144146
taskDefinition: taskDefArn,
145147
forceNewDeployment: forceNewDeployment,
146-
enableECSManagedTags: enableECSManagedTags
148+
enableECSManagedTags: enableECSManagedTags,
149+
propagateTags: propagateTags
147150
};
148151

149152
// Add the desiredCount property only if it is defined and a number.
@@ -401,7 +404,8 @@ async function run() {
401404
const desiredCount = parseInt((core.getInput('desired-count', {required: false})));
402405
const enableECSManagedTagsInput = core.getInput('enable-ecs-managed-tags', { required: false }) || 'false';
403406
const enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true';
404-
407+
const propagateTags = core.getInput('propagate-tags', { required: false }) || 'NONE';
408+
405409
// Register the task definition
406410
core.debug('Registering the task definition');
407411
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
@@ -452,7 +456,7 @@ async function run() {
452456
if (!serviceResponse.deploymentController || !serviceResponse.deploymentController.type || serviceResponse.deploymentController.type === 'ECS') {
453457
// Service uses the 'ECS' deployment controller, so we can call UpdateService
454458
core.debug('Updating service...');
455-
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags);
459+
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags, propagateTags);
456460

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

index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ async function runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSMa
3131
const securityGroupIds = core.getInput('run-task-security-groups', { required: false }) || '';
3232
const containerOverrides = JSON.parse(core.getInput('run-task-container-overrides', { required: false }) || '[]');
3333
const assignPublicIP = core.getInput('run-task-assign-public-IP', { required: false }) || 'DISABLED';
34+
const tags = JSON.parse(core.getInput('run-task-tags', { required: false }) || '[]');
3435

3536
let awsvpcConfiguration = {}
3637

@@ -55,7 +56,8 @@ async function runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSMa
5556
},
5657
launchType: launchType,
5758
networkConfiguration: Object.keys(awsvpcConfiguration).length === 0 ? null : { awsvpcConfiguration: awsvpcConfiguration },
58-
enableECSManagedTags: enableECSManagedTags
59+
enableECSManagedTags: enableECSManagedTags,
60+
tags: tags
5961
});
6062

6163
core.debug(`Run task response ${JSON.stringify(runTaskResponse)}`)
@@ -129,15 +131,16 @@ async function tasksExitCode(ecs, clusterName, taskArns) {
129131
}
130132

131133
// Deploy to a service that uses the 'ECS' deployment controller
132-
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags) {
134+
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags, propagateTags) {
133135
core.debug('Updating the service');
134136

135137
let params = {
136138
cluster: clusterName,
137139
service: service,
138140
taskDefinition: taskDefArn,
139141
forceNewDeployment: forceNewDeployment,
140-
enableECSManagedTags: enableECSManagedTags
142+
enableECSManagedTags: enableECSManagedTags,
143+
propagateTags: propagateTags
141144
};
142145

143146
// Add the desiredCount property only if it is defined and a number.
@@ -395,7 +398,8 @@ async function run() {
395398
const desiredCount = parseInt((core.getInput('desired-count', {required: false})));
396399
const enableECSManagedTagsInput = core.getInput('enable-ecs-managed-tags', { required: false }) || 'false';
397400
const enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true';
398-
401+
const propagateTags = core.getInput('propagate-tags', { required: false }) || 'NONE';
402+
399403
// Register the task definition
400404
core.debug('Registering the task definition');
401405
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
@@ -446,7 +450,7 @@ async function run() {
446450
if (!serviceResponse.deploymentController || !serviceResponse.deploymentController.type || serviceResponse.deploymentController.type === 'ECS') {
447451
// Service uses the 'ECS' deployment controller, so we can call UpdateService
448452
core.debug('Updating service...');
449-
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags);
453+
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags, propagateTags);
450454

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

0 commit comments

Comments
 (0)