From 6bb9f58e6eff25d8982f635f0f42813687482ea5 Mon Sep 17 00:00:00 2001 From: Evgeniy Belyaev Date: Sun, 20 Feb 2022 12:39:23 +0100 Subject: [PATCH 1/2] Add spport for capacityProviderStrategy run attribute --- README.md | 1 + action.yml | 3 +++ index.js | 21 ++++++++++++++++++--- index.test.js | 21 ++++++++++++++++++++- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f8d792658..09d0f0951 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ Runs an Amazon ECS task on ECS cluster. count: 1 started-by: github-actions-${{ github.actor }} wait-for-finish: true + capacity-provider-strategy: '[{"capacityProvider": "provider", "base": 1, "weight": 1}]' # optional ``` See [action.yml](action.yml) for the full documentation for this action's inputs and outputs. diff --git a/action.yml b/action.yml index 424dde637..f5870ec2f 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,9 @@ inputs: wait-for-minutes: description: 'How long to wait for the task reach stopped state, in minutes (default: 30 minutes, max: 6 hours).' required: false + capacity-provider-strategy: + description: "JSON representation of the capacity provider strategy to use for the task." + required: false outputs: task-definition-arn: description: 'The ARN of the registered ECS task definition' diff --git a/index.js b/index.js index b07b6f216..8a4e07043 100644 --- a/index.js +++ b/index.js @@ -88,6 +88,7 @@ async function run() { const taskDefinitionFile = core.getInput('task-definition', { required: true }); const cluster = core.getInput('cluster', { required: false }); const count = core.getInput('count', { required: true }); + const capacityProviderStrategyString = core.getInput('capacity-provider-strategy', { required: true }); const startedBy = core.getInput('started-by', { required: false }) || agent; const waitForFinish = core.getInput('wait-for-finish', { required: false }) || false; let waitForMinutes = parseInt(core.getInput('wait-for-minutes', { required: false })) || 30; @@ -95,6 +96,18 @@ async function run() { waitForMinutes = MAX_WAIT_MINUTES; } + let capacityProviderStrategy; + if (capacityProviderStrategyString != "") { + try { + capacityProviderStrategy = JSON.parse(capacityProviderStrategyString); + } catch (error) { + core.setFailed("Failed to parse capacity provider strategy definition: " + error.message); + core.debug("Parameter value:"); + core.debug(capacityProviderStrategyString); + throw(error); + } + } + // Register the task definition core.debug('Registering the task definition'); const taskDefPath = path.isAbsolute(taskDefinitionFile) ? @@ -121,6 +134,7 @@ async function run() { cluster: clusterName, taskDefinition: taskDefArn, count: count, + capacityProviderStrategy: capacityProviderStrategy, startedBy: startedBy })}`) @@ -128,6 +142,7 @@ async function run() { cluster: clusterName, taskDefinition: taskDefArn, count: count, + capacityProviderStrategy: capacityProviderStrategy, startedBy: startedBy }).promise(); @@ -161,7 +176,7 @@ async function waitForTasksStopped(ecs, clusterName, taskArns, waitForMinutes) { const maxAttempts = (waitForMinutes * 60) / WAIT_DEFAULT_DELAY_SEC; core.debug('Waiting for tasks to stop'); - + const waitTaskResponse = await ecs.waitFor('tasksStopped', { cluster: clusterName, tasks: taskArns, @@ -172,7 +187,7 @@ async function waitForTasksStopped(ecs, clusterName, taskArns, waitForMinutes) { }).promise(); core.debug(`Run task response ${JSON.stringify(waitTaskResponse)}`) - + core.info(`All tasks have stopped. Watch progress in the Amazon ECS console: https://console.aws.amazon.com/ecs/home?region=${aws.config.region}#/clusters/${clusterName}/tasks`); } @@ -187,7 +202,7 @@ async function tasksExitCode(ecs, clusterName, taskArns) { const reasons = containers.map(container => container.reason) const failuresIdx = []; - + exitCodes.filter((exitCode, index) => { if (exitCode !== 0) { failuresIdx.push(index) diff --git a/index.test.js b/index.test.js index 9eaec9ae2..44fd706a1 100644 --- a/index.test.js +++ b/index.test.js @@ -34,6 +34,7 @@ describe('Deploy to ECS', () => { .mockReturnValueOnce('task-definition.json') // task-definition .mockReturnValueOnce('cluster-789') // cluster .mockReturnValueOnce('1') // count + .mockReturnValueOnce('') // capacity-provider-strategy .mockReturnValueOnce('amazon-ecs-run-task-for-github-actions'); // started-by process.env = Object.assign(process.env, { GITHUB_WORKSPACE: __dirname }); @@ -142,6 +143,7 @@ describe('Deploy to ECS', () => { .mockReturnValueOnce('task-definition.json') // task-definition .mockReturnValueOnce('cluster-789') // cluster .mockReturnValueOnce('1') // count + .mockReturnValueOnce('') // capacity-provider-strategy .mockReturnValueOnce('amazon-ecs-run-task-for-github-actions') // started-by .mockReturnValueOnce('true'); // wait-for-finish @@ -260,7 +262,7 @@ describe('Deploy to ECS', () => { expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'}); }); - test('error is caught if task def registration fails', async () => { + test('error is caught if capacity provider strategy param parsing fails', async () => { mockEcsRegisterTaskDef.mockImplementation(() => { throw new Error("Could not parse"); }); @@ -271,4 +273,21 @@ 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('error is caught if task def registration fails', async () => { + core.getInput = jest + .fn() + .mockReturnValueOnce('task-definition.json') // task-definition + .mockReturnValueOnce('cluster-789') // cluster + .mockReturnValueOnce('1') // count + .mockReturnValueOnce('not-a-json') // capacity-provider-strategy + .mockReturnValueOnce('amazon-ecs-run-task-for-github-actions') // started-by + .mockReturnValueOnce('true'); // wait-for-finish + + await run(); + + expect(core.setFailed).toHaveBeenCalledTimes(2); + expect(core.setFailed).toHaveBeenNthCalledWith(1, 'Failed to parse capacity provider strategy definition: Unexpected token o in JSON at position 1'); + expect(core.setFailed).toHaveBeenNthCalledWith(2, 'Unexpected token o in JSON at position 1'); + }); }); From 82b544deea4d5fc1b7ad22213ef58058ad67184e Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Sun, 20 Feb 2022 11:40:16 +0000 Subject: [PATCH 2/2] chore: Update dist --- dist/index.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index 2e70b39dc..16f7f7f3a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -221,6 +221,7 @@ async function run() { const taskDefinitionFile = core.getInput('task-definition', { required: true }); const cluster = core.getInput('cluster', { required: false }); const count = core.getInput('count', { required: true }); + const capacityProviderStrategyString = core.getInput('capacity-provider-strategy', { required: true }); const startedBy = core.getInput('started-by', { required: false }) || agent; const waitForFinish = core.getInput('wait-for-finish', { required: false }) || false; let waitForMinutes = parseInt(core.getInput('wait-for-minutes', { required: false })) || 30; @@ -228,6 +229,18 @@ async function run() { waitForMinutes = MAX_WAIT_MINUTES; } + let capacityProviderStrategy; + if (capacityProviderStrategyString != "") { + try { + capacityProviderStrategy = JSON.parse(capacityProviderStrategyString); + } catch (error) { + core.setFailed("Failed to parse capacity provider strategy definition: " + error.message); + core.debug("Parameter value:"); + core.debug(capacityProviderStrategyString); + throw(error); + } + } + // Register the task definition core.debug('Registering the task definition'); const taskDefPath = path.isAbsolute(taskDefinitionFile) ? @@ -254,6 +267,7 @@ async function run() { cluster: clusterName, taskDefinition: taskDefArn, count: count, + capacityProviderStrategy: capacityProviderStrategy, startedBy: startedBy })}`) @@ -261,6 +275,7 @@ async function run() { cluster: clusterName, taskDefinition: taskDefArn, count: count, + capacityProviderStrategy: capacityProviderStrategy, startedBy: startedBy }).promise(); @@ -294,7 +309,7 @@ async function waitForTasksStopped(ecs, clusterName, taskArns, waitForMinutes) { const maxAttempts = (waitForMinutes * 60) / WAIT_DEFAULT_DELAY_SEC; core.debug('Waiting for tasks to stop'); - + const waitTaskResponse = await ecs.waitFor('tasksStopped', { cluster: clusterName, tasks: taskArns, @@ -305,7 +320,7 @@ async function waitForTasksStopped(ecs, clusterName, taskArns, waitForMinutes) { }).promise(); core.debug(`Run task response ${JSON.stringify(waitTaskResponse)}`) - + core.info(`All tasks have stopped. Watch progress in the Amazon ECS console: https://console.aws.amazon.com/ecs/home?region=${aws.config.region}#/clusters/${clusterName}/tasks`); } @@ -320,7 +335,7 @@ async function tasksExitCode(ecs, clusterName, taskArns) { const reasons = containers.map(container => container.reason) const failuresIdx = []; - + exitCodes.filter((exitCode, index) => { if (exitCode !== 0) { failuresIdx.push(index)