Skip to content

Commit

Permalink
set enableECSManagedTags in updateEcsService and runTask when input i…
Browse files Browse the repository at this point in the history
…s exist
  • Loading branch information
wim-web committed Nov 26, 2024
1 parent 4640cea commit 2b7155d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 17 deletions.
14 changes: 7 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,18 @@ 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 = {
cluster: clusterName,
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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,18 @@ 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 = {
cluster: clusterName,
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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
134 changes: 131 additions & 3 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -952,6 +954,7 @@ describe('Deploy to ECS', () => {
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false,
enableECSManagedTags: null,
propagateTags: 'NONE'
});
expect(waitUntilServicesStable).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -992,6 +995,7 @@ describe('Deploy to ECS', () => {
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false,
enableECSManagedTags: null,
propagateTags: 'NONE'
});
expect(waitUntilServicesStable).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -1032,6 +1036,7 @@ describe('Deploy to ECS', () => {
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false,
enableECSManagedTags: null,
propagateTags: 'NONE'
});
expect(waitUntilServicesStable).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -1074,6 +1079,7 @@ describe('Deploy to ECS', () => {
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: true,
enableECSManagedTags: null,
propagateTags: 'NONE'
});
});
Expand All @@ -1099,6 +1105,7 @@ describe('Deploy to ECS', () => {
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false,
enableECSManagedTags: null,
propagateTags: 'NONE'
});
});
Expand Down Expand Up @@ -1145,7 +1152,7 @@ describe('Deploy to ECS', () => {
taskDefinition: 'task:def:arn',
overrides: {"containerOverrides": []},
networkConfiguration: null,
enableECSManagedTags: false,
enableECSManagedTags: null,
tags: []
});

Expand Down Expand Up @@ -1269,6 +1276,7 @@ describe('Deploy to ECS', () => {
service: 'service-456',
taskDefinition: 'task:def:arn',
forceNewDeployment: false,
enableECSManagedTags: null,
propagateTags: 'NONE',
});
expect(mockRunTask).toHaveBeenCalledWith({
Expand All @@ -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"]);
Expand Down Expand Up @@ -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: []
});
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
});
});
});

0 comments on commit 2b7155d

Please sign in to comment.