Skip to content

Commit e5f78d3

Browse files
feat: Add desired tasks (#505)
* add desired count * add desired count * add desired count * fix test for desired count * small change * update dist/index.js * address feedback @iamhopaul123 * update dist/index.js --------- Co-authored-by: Adithya Kolla <[email protected]>
1 parent ee7a5be commit e5f78d3

File tree

4 files changed

+58
-32
lines changed

4 files changed

+58
-32
lines changed

action.yml

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ inputs:
77
task-definition:
88
description: 'The path to the ECS task definition file to register'
99
required: true
10+
desired-count:
11+
description: 'The number of instantiations of the task to place and keep running in your service.'
12+
required: false
1013
service:
1114
description: 'The name of the ECS service to deploy to. The action will only register the task definition if no service is given.'
1215
required: false

dist/index.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ const IGNORED_TASK_DEFINITION_ATTRIBUTES = [
2727
];
2828

2929
// Deploy to a service that uses the 'ECS' deployment controller
30-
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment) {
30+
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount) {
3131
core.debug('Updating the service');
32-
await ecs.updateService({
32+
let params = {
3333
cluster: clusterName,
3434
service: service,
3535
taskDefinition: taskDefArn,
3636
forceNewDeployment: forceNewDeployment
37-
}).promise();
37+
};
38+
// Add the desiredCount property only if it is defined and a number.
39+
if (!isNaN(desiredCount) && desiredCount !== undefined) {
40+
params.desiredCount = desiredCount;
41+
}
42+
await ecs.updateService(params).promise();
3843

3944
const consoleHostname = aws.config.region.startsWith('cn') ? 'console.amazonaws.cn' : 'console.aws.amazon.com';
4045

@@ -275,6 +280,9 @@ async function run() {
275280
const forceNewDeployInput = core.getInput('force-new-deployment', { required: false }) || 'false';
276281
const forceNewDeployment = forceNewDeployInput.toLowerCase() === 'true';
277282

283+
const desiredCount = parseInt((core.getInput('desired-count', {required: false})));
284+
285+
278286
// Register the task definition
279287
core.debug('Registering the task definition');
280288
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
@@ -316,7 +324,7 @@ async function run() {
316324

317325
if (!serviceResponse.deploymentController || !serviceResponse.deploymentController.type || serviceResponse.deploymentController.type === 'ECS') {
318326
// Service uses the 'ECS' deployment controller, so we can call UpdateService
319-
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment);
327+
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount);
320328
} else if (serviceResponse.deploymentController.type === 'CODE_DEPLOY') {
321329
// Service uses CodeDeploy, so we should start a CodeDeploy deployment
322330
await createCodeDeployDeployment(codedeploy, clusterName, service, taskDefArn, waitForService, waitForMinutes);

index.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ const IGNORED_TASK_DEFINITION_ATTRIBUTES = [
2121
];
2222

2323
// Deploy to a service that uses the 'ECS' deployment controller
24-
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment) {
24+
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount) {
2525
core.debug('Updating the service');
26-
await ecs.updateService({
26+
let params = {
2727
cluster: clusterName,
2828
service: service,
2929
taskDefinition: taskDefArn,
3030
forceNewDeployment: forceNewDeployment
31-
}).promise();
31+
};
32+
// Add the desiredCount property only if it is defined and a number.
33+
if (!isNaN(desiredCount) && desiredCount !== undefined) {
34+
params.desiredCount = desiredCount;
35+
}
36+
await ecs.updateService(params).promise();
3237

3338
const consoleHostname = aws.config.region.startsWith('cn') ? 'console.amazonaws.cn' : 'console.aws.amazon.com';
3439

@@ -269,6 +274,9 @@ async function run() {
269274
const forceNewDeployInput = core.getInput('force-new-deployment', { required: false }) || 'false';
270275
const forceNewDeployment = forceNewDeployInput.toLowerCase() === 'true';
271276

277+
const desiredCount = parseInt((core.getInput('desired-count', {required: false})));
278+
279+
272280
// Register the task definition
273281
core.debug('Registering the task definition');
274282
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
@@ -310,7 +318,7 @@ async function run() {
310318

311319
if (!serviceResponse.deploymentController || !serviceResponse.deploymentController.type || serviceResponse.deploymentController.type === 'ECS') {
312320
// Service uses the 'ECS' deployment controller, so we can call UpdateService
313-
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment);
321+
await updateEcsService(ecs, clusterName, service, taskDefArn, waitForService, waitForMinutes, forceNewDeployment, desiredCount);
314322
} else if (serviceResponse.deploymentController.type === 'CODE_DEPLOY') {
315323
// Service uses CodeDeploy, so we should start a CodeDeploy deployment
316324
await createCodeDeployDeployment(codedeploy, clusterName, service, taskDefArn, waitForService, waitForMinutes);

index.test.js

+31-24
Original file line numberDiff line numberDiff line change
@@ -687,14 +687,15 @@ describe('Deploy to ECS', () => {
687687
core.getInput = jest
688688
.fn()
689689
.mockReturnValueOnce('task-definition.json') // task-definition
690-
.mockReturnValueOnce('service-456') // service
691-
.mockReturnValueOnce('cluster-789') // cluster
692-
.mockReturnValueOnce('false') // wait-for-service-stability
693-
.mockReturnValueOnce('') // wait-for-minutes
694-
.mockReturnValueOnce('') // force-new-deployment
695-
.mockReturnValueOnce('/hello/appspec.json') // codedeploy-appspec
696-
.mockReturnValueOnce('MyApplication') // codedeploy-application
697-
.mockReturnValueOnce('MyDeploymentGroup'); // codedeploy-deployment-group
690+
.mockReturnValueOnce('service-456') // service
691+
.mockReturnValueOnce('cluster-789') // cluster
692+
.mockReturnValueOnce('false') // wait-for-service-stability
693+
.mockReturnValueOnce('') // wait-for-minutes
694+
.mockReturnValueOnce('') // force-new-deployment
695+
.mockReturnValueOnce('') // desired count
696+
.mockReturnValueOnce('/hello/appspec.json') // codedeploy-appspec
697+
.mockReturnValueOnce('MyApplication') // codedeploy-application
698+
.mockReturnValueOnce('MyDeploymentGroup') // codedeploy-deployment-group
698699

699700
fs.readFileSync.mockReturnValue(`
700701
{
@@ -887,7 +888,7 @@ describe('Deploy to ECS', () => {
887888
expect(core.info).toBeCalledWith("Deployment started. Watch this deployment's progress in the AWS CodeDeploy console: https://console.aws.amazon.com/codesuite/codedeploy/deployments/deployment-1?region=fake-region");
888889
});
889890

890-
test('registers the task definition contents at an absolute path', async () => {
891+
test('registers the task definition contents at an absolute path', async () => {
891892
core.getInput = jest.fn().mockReturnValueOnce('/hello/task-definition.json');
892893
fs.readFileSync.mockImplementation((pathInput, encoding) => {
893894
if (encoding != 'utf8') {
@@ -908,13 +909,14 @@ describe('Deploy to ECS', () => {
908909
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition-arn', 'task:def:arn');
909910
});
910911

911-
test('waits for the service to be stable', async () => {
912+
test('waits for the service to be stable', async () => {
912913
core.getInput = jest
913914
.fn()
914915
.mockReturnValueOnce('task-definition.json') // task-definition
915-
.mockReturnValueOnce('service-456') // service
916-
.mockReturnValueOnce('cluster-789') // cluster
917-
.mockReturnValueOnce('TRUE'); // wait-for-service-stability
916+
.mockReturnValueOnce('service-456') // service
917+
.mockReturnValueOnce('cluster-789') // cluster
918+
.mockReturnValueOnce('TRUE') // wait-for-service-stability
919+
.mockReturnValueOnce(''); // desired count
918920

919921
await run();
920922
expect(core.setFailed).toHaveBeenCalledTimes(0);
@@ -945,10 +947,11 @@ describe('Deploy to ECS', () => {
945947
core.getInput = jest
946948
.fn()
947949
.mockReturnValueOnce('task-definition.json') // task-definition
948-
.mockReturnValueOnce('service-456') // service
949-
.mockReturnValueOnce('cluster-789') // cluster
950-
.mockReturnValueOnce('TRUE') // wait-for-service-stability
951-
.mockReturnValueOnce('60'); // wait-for-minutes
950+
.mockReturnValueOnce('service-456') // service
951+
.mockReturnValueOnce('cluster-789') // cluster
952+
.mockReturnValueOnce('TRUE') // wait-for-service-stability
953+
.mockReturnValueOnce('60') // wait-for-minutes
954+
.mockReturnValueOnce(''); // desired count
952955

953956
await run();
954957
expect(core.setFailed).toHaveBeenCalledTimes(0);
@@ -979,10 +982,11 @@ describe('Deploy to ECS', () => {
979982
core.getInput = jest
980983
.fn()
981984
.mockReturnValueOnce('task-definition.json') // task-definition
982-
.mockReturnValueOnce('service-456') // service
983-
.mockReturnValueOnce('cluster-789') // cluster
984-
.mockReturnValueOnce('TRUE') // wait-for-service-stability
985-
.mockReturnValueOnce('1000'); // wait-for-minutes
985+
.mockReturnValueOnce('service-456') // service
986+
.mockReturnValueOnce('cluster-789') // cluster
987+
.mockReturnValueOnce('TRUE') // wait-for-service-stability
988+
.mockReturnValueOnce('1000') // wait-for-minutes
989+
.mockReturnValueOnce('abc'); // desired count is NaN
986990

987991
await run();
988992
expect(core.setFailed).toHaveBeenCalledTimes(0);
@@ -1012,12 +1016,13 @@ describe('Deploy to ECS', () => {
10121016
test('force new deployment', async () => {
10131017
core.getInput = jest
10141018
.fn()
1015-
.mockReturnValueOnce('task-definition.json') // task-definition
1019+
.mockReturnValueOnce('task-definition.json') // task-definition
10161020
.mockReturnValueOnce('service-456') // service
10171021
.mockReturnValueOnce('cluster-789') // cluster
10181022
.mockReturnValueOnce('false') // wait-for-service-stability
10191023
.mockReturnValueOnce('') // wait-for-minutes
1020-
.mockReturnValueOnce('true'); // force-new-deployment
1024+
.mockReturnValueOnce('true') // force-new-deployment
1025+
.mockReturnValueOnce('4'); // desired count is number
10211026

10221027
await run();
10231028
expect(core.setFailed).toHaveBeenCalledTimes(0);
@@ -1030,6 +1035,7 @@ describe('Deploy to ECS', () => {
10301035
});
10311036
expect(mockEcsUpdateService).toHaveBeenNthCalledWith(1, {
10321037
cluster: 'cluster-789',
1038+
desiredCount: 4,
10331039
service: 'service-456',
10341040
taskDefinition: 'task:def:arn',
10351041
forceNewDeployment: true
@@ -1040,7 +1046,8 @@ describe('Deploy to ECS', () => {
10401046
core.getInput = jest
10411047
.fn()
10421048
.mockReturnValueOnce('task-definition.json') // task-definition
1043-
.mockReturnValueOnce('service-456'); // service
1049+
.mockReturnValueOnce('service-456') // service
1050+
.mockReturnValueOnce(''); // desired count
10441051

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

0 commit comments

Comments
 (0)