Skip to content

feat: Enable AWS managed tags #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ inputs:
wait-for-task-stopped:
description: 'Whether to wait for the task to stop when running it outside of a service. Will default to not wait.'
required: false
enable-ecs-managed-tags:
description: "Determines whether to turn on Amazon ECS managed tags 'aws:ecs:serviceName' and 'aws:ecs:clusterName' for the tasks in the service."
required: false
outputs:
task-definition-arn:
description: 'The ARN of the registered ECS task definition'
Expand Down
9 changes: 6 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,15 @@ async function tasksExitCode(ecs, clusterName, taskArns) {
}

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

let params = {
cluster: clusterName,
service: service,
taskDefinition: taskDefArn,
forceNewDeployment: forceNewDeployment
forceNewDeployment: forceNewDeployment,
enableECSManagedTags: enableECSManagedTags
};

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

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

} else if (serviceResponse.deploymentController.type === 'CODE_DEPLOY') {
// Service uses CodeDeploy, so we should start a CodeDeploy deployment
Expand Down
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,15 @@ async function tasksExitCode(ecs, clusterName, taskArns) {
}

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

let params = {
cluster: clusterName,
service: service,
taskDefinition: taskDefArn,
forceNewDeployment: forceNewDeployment
forceNewDeployment: forceNewDeployment,
enableECSManagedTags: enableECSManagedTags
};

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

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

} else if (serviceResponse.deploymentController.type === 'CODE_DEPLOY') {
// Service uses CodeDeploy, so we should start a CodeDeploy deployment
Expand Down
68 changes: 57 additions & 11 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ describe('Deploy to ECS', () => {
cluster: 'cluster-789',
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false
forceNewDeployment: false,
enableECSManagedTags: false
});
expect(waitUntilServicesStable).toHaveBeenCalledTimes(0);
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");
Expand Down Expand Up @@ -213,7 +214,8 @@ describe('Deploy to ECS', () => {
cluster: 'cluster-789',
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false
forceNewDeployment: false,
enableECSManagedTags: false
});
expect(waitUntilServicesStable).toHaveBeenCalledTimes(0);
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");
Expand Down Expand Up @@ -708,6 +710,7 @@ describe('Deploy to ECS', () => {
.mockReturnValueOnce('') // force-new-deployment
.mockReturnValueOnce('') // run-task
.mockReturnValueOnce('') // desired count
.mockReturnValueOnce('') // enable-ecs-managed-tags
.mockReturnValueOnce('/hello/appspec.json') // codedeploy-appspec
.mockReturnValueOnce('MyApplication') // codedeploy-application
.mockReturnValueOnce('MyDeploymentGroup'); // codedeploy-deployment-group
Expand Down Expand Up @@ -944,7 +947,8 @@ describe('Deploy to ECS', () => {
cluster: 'cluster-789',
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false
forceNewDeployment: false,
enableECSManagedTags: false
});
expect(waitUntilServicesStable).toHaveBeenNthCalledWith(
1,
Expand Down Expand Up @@ -983,7 +987,8 @@ describe('Deploy to ECS', () => {
cluster: 'cluster-789',
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false
forceNewDeployment: false,
enableECSManagedTags: false
});
expect(waitUntilServicesStable).toHaveBeenNthCalledWith(
1,
Expand Down Expand Up @@ -1022,7 +1027,8 @@ describe('Deploy to ECS', () => {
cluster: 'cluster-789',
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false
forceNewDeployment: false,
enableECSManagedTags: false
});
expect(waitUntilServicesStable).toHaveBeenNthCalledWith(
1,
Expand Down Expand Up @@ -1063,7 +1069,8 @@ describe('Deploy to ECS', () => {
desiredCount: 4,
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: true
forceNewDeployment: true,
enableECSManagedTags: false
});
});

Expand All @@ -1087,7 +1094,8 @@ describe('Deploy to ECS', () => {
cluster: 'default',
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false
forceNewDeployment: false,
enableECSManagedTags: false
});
});

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

await run();
Expand Down Expand Up @@ -1145,6 +1154,7 @@ describe('Deploy to ECS', () => {
.mockReturnValueOnce('') // wait-for-minutes
.mockReturnValueOnce('') // force-new-deployment
.mockReturnValueOnce('') // desired-count
.mockReturnValueOnce('') // enable-ecs-managed-tags
.mockReturnValueOnce('true') // run-task
.mockReturnValueOnce('false') // wait-for-task-stopped
.mockReturnValueOnce('someJoe') // run-task-started-by
Expand Down Expand Up @@ -1177,8 +1187,9 @@ describe('Deploy to ECS', () => {
.mockReturnValueOnce('somecluster') // cluster
.mockReturnValueOnce('true') // wait-for-service-stability
.mockReturnValueOnce('') // wait-for-minutes
.mockReturnValueOnce('') // force-new-deployment
.mockReturnValueOnce('') // force-new-deployment
.mockReturnValueOnce('') // desired-count
.mockReturnValueOnce('') // enable-ecs-managed-tags
.mockReturnValueOnce('true') // run-task
.mockReturnValueOnce('false') // wait-for-task-stopped
.mockReturnValueOnce('someJoe') // run-task-started-by
Expand All @@ -1200,7 +1211,8 @@ describe('Deploy to ECS', () => {
cluster: 'somecluster',
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false
forceNewDeployment: false,
enableECSManagedTags: false
});
expect(mockRunTask).toHaveBeenCalledWith({
startedBy: 'someJoe',
Expand All @@ -1223,6 +1235,7 @@ describe('Deploy to ECS', () => {
.mockReturnValueOnce('') // wait-for-minutes
.mockReturnValueOnce('') // force-new-deployment
.mockReturnValueOnce('') // desired-count
.mockReturnValueOnce('') // enable-ecs-managed-tags
.mockReturnValueOnce('true') // run-task
.mockReturnValueOnce('true'); // wait-for-task-stopped

Expand All @@ -1246,6 +1259,7 @@ describe('Deploy to ECS', () => {
.mockReturnValueOnce('') // wait-for-minutes
.mockReturnValueOnce('') // force-new-deployment
.mockReturnValueOnce('') // desired-count
.mockReturnValueOnce('') // enable-ecs-managed-tags
.mockReturnValueOnce('true') // run-task
.mockReturnValueOnce('true') // wait-for-task-stopped
.mockReturnValueOnce('someJoe') // run-task-started-by
Expand Down Expand Up @@ -1317,8 +1331,9 @@ describe('Deploy to ECS', () => {
.mockReturnValueOnce('') // wait-for-minutes
.mockReturnValueOnce('') // force-new-deployment
.mockReturnValueOnce('') // desired-count
.mockReturnValueOnce('') // enable-ecs-managed-tags
.mockReturnValueOnce('true') // run-task
.mockReturnValueOnce('false'); // wait-for-task-stopped
.mockReturnValueOnce('false'); // wait-for-task-stopped

mockRunTask.mockImplementation(
() => Promise.resolve({
Expand Down Expand Up @@ -1427,4 +1442,35 @@ describe('Deploy to ECS', () => {
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'Failed to register task definition in ECS: Could not parse');
expect(core.setFailed).toHaveBeenNthCalledWith(2, 'Could not parse');
});
});

test('deployment with enable-ecs-managed-tags', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('task-definition.json') // task-definition
.mockReturnValueOnce('service-456') // service
.mockReturnValueOnce('cluster-789') // cluster
.mockReturnValueOnce('false') // wait-for-service-stability
.mockReturnValueOnce('') // wait-for-minutes
.mockReturnValueOnce('') // force-new-deployment
.mockReturnValueOnce('1') // desired count is number
.mockReturnValueOnce('true'); // enable-ecs-managed-tags

await run();
expect(core.setFailed).toHaveBeenCalledTimes(0);

expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family' });
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition-arn', 'task:def:arn');
expect(mockEcsDescribeServices).toHaveBeenNthCalledWith(1, {
cluster: 'cluster-789',
services: ['service-456']
});
expect(mockEcsUpdateService).toHaveBeenNthCalledWith(1, {
cluster: 'cluster-789',
desiredCount: 1,
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false,
enableECSManagedTags: true
});
});
});
Loading