From 2b7155de06e6f48fb36251858f9f364a5644d462 Mon Sep 17 00:00:00 2001 From: wim-web <45655999+wim-web@users.noreply.github.com> Date: Tue, 26 Nov 2024 19:30:53 +0900 Subject: [PATCH] set enableECSManagedTags in updateEcsService and runTask when input is exist --- dist/index.js | 14 +++--- index.js | 14 +++--- index.test.js | 134 ++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 145 insertions(+), 17 deletions(-) diff --git a/dist/index.js b/dist/index.js index d5e0bbd0..e404e5d5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -139,7 +139,7 @@ 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, enableECSManagedTagsInput, propagateTags) { +async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags, propagateTags) { core.debug('Updating the service'); let params = { @@ -147,13 +147,10 @@ async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForSe service: service, taskDefinition: taskDefArn, forceNewDeployment: forceNewDeployment, + enableECSManagedTags: enableECSManagedTags, propagateTags: propagateTags }; - if (enableECSManagedTagsInput !== "") { - params.enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true'; - } - // Add the desiredCount property only if it is defined and a number. if (!isNaN(desiredCount) && desiredCount !== undefined) { params.desiredCount = desiredCount; @@ -408,7 +405,10 @@ async function run() { const forceNewDeployment = forceNewDeployInput.toLowerCase() === 'true'; const desiredCount = parseInt((core.getInput('desired-count', {required: false}))); const enableECSManagedTagsInput = core.getInput('enable-ecs-managed-tags', { required: false }) || ''; - const enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true'; + let enableECSManagedTags = null; + if (enableECSManagedTagsInput !== '') { + enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true'; + } const propagateTags = core.getInput('propagate-tags', { required: false }) || 'NONE'; // Register the task definition @@ -461,7 +461,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, enableECSManagedTagsInput, propagateTags); + await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags, propagateTags); } else if (serviceResponse.deploymentController.type === 'CODE_DEPLOY') { // Service uses CodeDeploy, so we should start a CodeDeploy deployment diff --git a/index.js b/index.js index 7287abaf..b24760e2 100644 --- a/index.js +++ b/index.js @@ -133,7 +133,7 @@ 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, enableECSManagedTagsInput, propagateTags) { +async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags, propagateTags) { core.debug('Updating the service'); let params = { @@ -141,13 +141,10 @@ async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForSe service: service, taskDefinition: taskDefArn, forceNewDeployment: forceNewDeployment, + enableECSManagedTags: enableECSManagedTags, propagateTags: propagateTags }; - if (enableECSManagedTagsInput !== "") { - params.enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true'; - } - // Add the desiredCount property only if it is defined and a number. if (!isNaN(desiredCount) && desiredCount !== undefined) { params.desiredCount = desiredCount; @@ -402,7 +399,10 @@ async function run() { const forceNewDeployment = forceNewDeployInput.toLowerCase() === 'true'; const desiredCount = parseInt((core.getInput('desired-count', {required: false}))); const enableECSManagedTagsInput = core.getInput('enable-ecs-managed-tags', { required: false }) || ''; - const enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true'; + let enableECSManagedTags = null; + if (enableECSManagedTagsInput !== '') { + enableECSManagedTags = enableECSManagedTagsInput.toLowerCase() === 'true'; + } const propagateTags = core.getInput('propagate-tags', { required: false }) || 'NONE'; // Register the task definition @@ -455,7 +455,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, enableECSManagedTagsInput, propagateTags); + await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount, enableECSManagedTags, propagateTags); } else if (serviceResponse.deploymentController.type === 'CODE_DEPLOY') { // Service uses CodeDeploy, so we should start a CodeDeploy deployment diff --git a/index.test.js b/index.test.js index 2f9efe41..493c520c 100644 --- a/index.test.js +++ b/index.test.js @@ -186,6 +186,7 @@ describe('Deploy to ECS', () => { service: 'service-456', taskDefinition: 'task:def:arn', forceNewDeployment: false, + enableECSManagedTags: null, propagateTags: 'NONE' }); expect(waitUntilServicesStable).toHaveBeenCalledTimes(0); @@ -218,6 +219,7 @@ describe('Deploy to ECS', () => { service: 'service-456', taskDefinition: 'task:def:arn', forceNewDeployment: false, + enableECSManagedTags: null, propagateTags: 'NONE' }); expect(waitUntilServicesStable).toHaveBeenCalledTimes(0); @@ -952,6 +954,7 @@ describe('Deploy to ECS', () => { service: 'service-456', taskDefinition: 'task:def:arn', forceNewDeployment: false, + enableECSManagedTags: null, propagateTags: 'NONE' }); expect(waitUntilServicesStable).toHaveBeenNthCalledWith( @@ -992,6 +995,7 @@ describe('Deploy to ECS', () => { service: 'service-456', taskDefinition: 'task:def:arn', forceNewDeployment: false, + enableECSManagedTags: null, propagateTags: 'NONE' }); expect(waitUntilServicesStable).toHaveBeenNthCalledWith( @@ -1032,6 +1036,7 @@ describe('Deploy to ECS', () => { service: 'service-456', taskDefinition: 'task:def:arn', forceNewDeployment: false, + enableECSManagedTags: null, propagateTags: 'NONE' }); expect(waitUntilServicesStable).toHaveBeenNthCalledWith( @@ -1074,6 +1079,7 @@ describe('Deploy to ECS', () => { service: 'service-456', taskDefinition: 'task:def:arn', forceNewDeployment: true, + enableECSManagedTags: null, propagateTags: 'NONE' }); }); @@ -1099,6 +1105,7 @@ describe('Deploy to ECS', () => { service: 'service-456', taskDefinition: 'task:def:arn', forceNewDeployment: false, + enableECSManagedTags: null, propagateTags: 'NONE' }); }); @@ -1145,7 +1152,7 @@ describe('Deploy to ECS', () => { taskDefinition: 'task:def:arn', overrides: {"containerOverrides": []}, networkConfiguration: null, - enableECSManagedTags: false, + enableECSManagedTags: null, tags: [] }); @@ -1269,6 +1276,7 @@ describe('Deploy to ECS', () => { service: 'service-456', taskDefinition: 'task:def:arn', forceNewDeployment: false, + enableECSManagedTags: null, propagateTags: 'NONE', }); expect(mockRunTask).toHaveBeenCalledWith({ @@ -1279,7 +1287,7 @@ describe('Deploy to ECS', () => { launchType: 'EC2', overrides: { containerOverrides: [{ name: 'someapp', command: 'somecmd' }] }, networkConfiguration: { awsvpcConfiguration: { subnets: ['a', 'b'], securityGroups: ['c', 'd'], assignPublicIp: "DISABLED" } }, - enableECSManagedTags: false, + enableECSManagedTags: null, tags: [] }); expect(core.setOutput).toHaveBeenNthCalledWith(2, 'run-task-arn', ["arn:aws:ecs:fake-region:account_id:task/arn"]); @@ -1340,6 +1348,62 @@ describe('Deploy to ECS', () => { launchType: 'EC2', overrides: { containerOverrides: [] }, networkConfiguration: null, + enableECSManagedTags: null, + tags: [] + }); + }); + + test('run task with setting true to enableECSManagedTags', async () => { + core.getInput = jest + .fn() + .mockReturnValueOnce('task-definition.json') // task-definition + .mockReturnValueOnce('') // service + .mockReturnValueOnce('somecluster') // cluster + .mockReturnValueOnce('') // wait-for-service-stability + .mockReturnValueOnce('') // wait-for-minutes + .mockReturnValueOnce('') // force-new-deployment + .mockReturnValueOnce('') // desired-count + .mockReturnValueOnce('true') // enable-ecs-managed-tags + .mockReturnValueOnce('') // propagate-tags + .mockReturnValueOnce('true'); // run-task + + await run(); + expect(mockRunTask).toHaveBeenCalledWith({ + startedBy: 'GitHub-Actions', + cluster: 'somecluster', + taskDefinition: 'task:def:arn', + capacityProviderStrategy: null, + launchType: 'FARGATE', + overrides: { containerOverrides: [] }, + networkConfiguration: null, + enableECSManagedTags: true, + tags: [] + }); + }); + + test('run task with setting false to enableECSManagedTags', async () => { + core.getInput = jest + .fn() + .mockReturnValueOnce('task-definition.json') // task-definition + .mockReturnValueOnce('') // service + .mockReturnValueOnce('somecluster') // cluster + .mockReturnValueOnce('') // wait-for-service-stability + .mockReturnValueOnce('') // wait-for-minutes + .mockReturnValueOnce('') // force-new-deployment + .mockReturnValueOnce('') // desired-count + .mockReturnValueOnce('false') // enable-ecs-managed-tags + .mockReturnValueOnce('') // propagate-tags + .mockReturnValueOnce('true'); // run-task + + await run(); + expect(mockRunTask).toHaveBeenCalledWith({ + startedBy: 'GitHub-Actions', + cluster: 'somecluster', + taskDefinition: 'task:def:arn', + capacityProviderStrategy: null, + launchType: 'FARGATE', + overrides: { containerOverrides: [] }, + networkConfiguration: null, enableECSManagedTags: false, tags: [] }); @@ -1511,7 +1575,39 @@ describe('Deploy to ECS', () => { expect(core.setFailed).toHaveBeenNthCalledWith(2, 'Could not parse'); }); - test('propagate service tags from service and enable ecs managed tags', async () => { + test('propagate service tags from service', 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('') // desired-count + .mockReturnValueOnce('') // enable-ecs-managed-tags + .mockReturnValueOnce('SERVICE'); // propagate-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', + service: 'service-456', + taskDefinition: 'task:def:arn', + forceNewDeployment: false, + enableECSManagedTags: null, + propagateTags: 'SERVICE' + }); + }); + + test('update service with setting true to enableECSManagedTags', async () => { core.getInput = jest .fn() .mockReturnValueOnce('task-definition.json') // task-definition @@ -1542,4 +1638,36 @@ describe('Deploy to ECS', () => { propagateTags: 'SERVICE' }); }); + + test('update service with setting false to enableECSManagedTags', 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('') // desired-count + .mockReturnValueOnce('false') // enable-ecs-managed-tags + .mockReturnValueOnce('SERVICE'); // propagate-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', + service: 'service-456', + taskDefinition: 'task:def:arn', + forceNewDeployment: false, + enableECSManagedTags: false, + propagateTags: 'SERVICE' + }); + }); });