Skip to content

Commit 43672e2

Browse files
committed
feat: Get tags from services and set ad-hoc run-task
1 parent 5ae7be6 commit 43672e2

File tree

7 files changed

+159
-42
lines changed

7 files changed

+159
-42
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: 11 additions & 7 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, enableECSManagedTags) {
30+
async function runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSManagedTags, runTaskTags) {
3131
core.info('Running task')
3232

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

6768
core.debug(`Run task response ${JSON.stringify(runTaskResponse)}`)
@@ -135,15 +136,16 @@ async function tasksExitCode(ecs, clusterName, taskArns) {
135136
}
136137

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

141142
let params = {
142143
cluster: clusterName,
143144
service: service,
144145
taskDefinition: taskDefArn,
145146
forceNewDeployment: forceNewDeployment,
146-
enableECSManagedTags: enableECSManagedTags
147+
enableECSManagedTags: enableECSManagedTags,
148+
propagateTags: propagateTags
147149
};
148150

149151
// Add the desiredCount property only if it is defined and a number.
@@ -401,7 +403,9 @@ async function run() {
401403
const desiredCount = parseInt((core.getInput('desired-count', {required: false})));
402404
const enableECSManagedTagsInput = core.getInput('enable-ecs-managed-tags', { required: false }) || 'false';
403405
const enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true';
404-
406+
const propagateTags = core.getInput('propagate-tags', { required: false }) || 'NONE';
407+
const runTaskTags = JSON.parse(core.getInput('run-task-tags', { required: false }) || '[]');
408+
405409
// Register the task definition
406410
core.debug('Registering the task definition');
407411
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
@@ -428,7 +432,7 @@ async function run() {
428432
core.debug(`shouldRunTask: ${shouldRunTask}`);
429433
if (shouldRunTask) {
430434
core.debug("Running ad-hoc task...");
431-
await runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSManagedTags);
435+
await runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSManagedTags, runTaskTags);
432436
}
433437

434438
// Update the service with the new task definition
@@ -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: 11 additions & 7 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, enableECSManagedTags) {
24+
async function runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSManagedTags, runTaskTags) {
2525
core.info('Running task')
2626

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

6162
core.debug(`Run task response ${JSON.stringify(runTaskResponse)}`)
@@ -129,15 +130,16 @@ async function tasksExitCode(ecs, clusterName, taskArns) {
129130
}
130131

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

135136
let params = {
136137
cluster: clusterName,
137138
service: service,
138139
taskDefinition: taskDefArn,
139140
forceNewDeployment: forceNewDeployment,
140-
enableECSManagedTags: enableECSManagedTags
141+
enableECSManagedTags: enableECSManagedTags,
142+
propagateTags: propagateTags
141143
};
142144

143145
// Add the desiredCount property only if it is defined and a number.
@@ -395,7 +397,9 @@ async function run() {
395397
const desiredCount = parseInt((core.getInput('desired-count', {required: false})));
396398
const enableECSManagedTagsInput = core.getInput('enable-ecs-managed-tags', { required: false }) || 'false';
397399
const enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true';
398-
400+
const propagateTags = core.getInput('propagate-tags', { required: false }) || 'NONE';
401+
const runTaskTags = JSON.parse(core.getInput('run-task-tags', { required: false }) || '[]');
402+
399403
// Register the task definition
400404
core.debug('Registering the task definition');
401405
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
@@ -422,7 +426,7 @@ async function run() {
422426
core.debug(`shouldRunTask: ${shouldRunTask}`);
423427
if (shouldRunTask) {
424428
core.debug("Running ad-hoc task...");
425-
await runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSManagedTags);
429+
await runTask(ecs, clusterName, taskDefArn, waitForMinutes, enableECSManagedTags, runTaskTags);
426430
}
427431

428432
// Update the service with the new task definition
@@ -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)