Skip to content

Commit

Permalink
feat: add possibility to add more than one container in container-name
Browse files Browse the repository at this point in the history
  • Loading branch information
felipem1210 committed Dec 4, 2024
1 parent 92c0c91 commit 5ca03ff
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ inputs:
description: 'The revision of the task definition being used'
required: false
container-name:
description: 'The name of the container defined in the containerDefinitions section of the ECS task definition'
description: 'The name of the container or containers defined in the containerDefinitions section of the ECS task definition. If more than one container, add the names comma separated.'
required: true
image:
description: 'The URI of the container image to insert into the ECS task definition'
Expand Down
30 changes: 20 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,26 @@ async function run() {
throw new Error("Either task definition, task definition arn or task definition family must be provided");
}

// Insert the image URI
if (!Array.isArray(taskDefContents.containerDefinitions)) {
throw new Error('Invalid task definition format: containerDefinitions section is not present or is not an array');
const containersNames = containerName.split(',');
// Check if containerNames length is major than 1
// Regex to check if a string is comma separated
const pattern = /^([^,]+,)*[^,]+$/g;
if (!containerName.match(pattern)) {
throw new Error('Invalid format for container name. Please use a single value or comma separated values');
}
const containerDef = taskDefContents.containerDefinitions.find(function (element) {
return element.name == containerName;
});
if (!containerDef) {
throw new Error('Invalid task definition: Could not find container definition with matching name');
}
containerDef.image = imageURI;

containersNames.forEach(contName => {
// Insert the image URI
if (!Array.isArray(taskDefContents.containerDefinitions)) {
throw new Error('Invalid task definition format: containerDefinitions section is not present or is not an array');
}
const containerDef = taskDefContents.containerDefinitions.find(function(element) {
return element.name == contName;
});
if (!containerDef) {
throw new Error('Invalid task definition: Could not find container definition with matching name');
}
containerDef.image = imageURI;

if (command) {
containerDef.command = command.split(' ')
Expand Down Expand Up @@ -224,6 +233,7 @@ async function run() {
}
})
}
});

// Write out a new task definition file
var updatedTaskDefFile = tmp.fileSync({
Expand Down
30 changes: 20 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,26 @@ async function run() {
throw new Error("Either task definition, task definition arn or task definition family must be provided");
}

// Insert the image URI
if (!Array.isArray(taskDefContents.containerDefinitions)) {
throw new Error('Invalid task definition format: containerDefinitions section is not present or is not an array');
const containersNames = containerName.split(',');
// Check if containerNames length is major than 1
// Regex to check if a string is comma separated
const pattern = /^([^,]+,)*[^,]+$/g;
if (!containerName.match(pattern)) {
throw new Error('Invalid format for container name. Please use a single value or comma separated values');
}
const containerDef = taskDefContents.containerDefinitions.find(function (element) {
return element.name == containerName;
});
if (!containerDef) {
throw new Error('Invalid task definition: Could not find container definition with matching name');
}
containerDef.image = imageURI;

containersNames.forEach(contName => {
// Insert the image URI
if (!Array.isArray(taskDefContents.containerDefinitions)) {
throw new Error('Invalid task definition format: containerDefinitions section is not present or is not an array');
}
const containerDef = taskDefContents.containerDefinitions.find(function(element) {
return element.name == contName;
});
if (!containerDef) {
throw new Error('Invalid task definition: Could not find container definition with matching name');
}
containerDef.image = imageURI;

if (command) {
containerDef.command = command.split(' ')
Expand Down Expand Up @@ -218,6 +227,7 @@ async function run() {
}
})
}
});

// Write out a new task definition file
var updatedTaskDefFile = tmp.fileSync({
Expand Down
25 changes: 25 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,31 @@ describe('Render task definition', () => {
expect(core.setFailed).toBeCalledWith('Task definition file does not exist: does-not-exist-task-definition.json');
});

test('error returned if container-name input is not well formated', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('/hello/task-definition.json')
.mockReturnValueOnce('web,')
.mockReturnValueOnce('nginx:latest');

await run();

expect(core.setFailed).toBeCalledWith('Invalid format for container name. Please use a single value or comma separated values');
});

test('error returned for missing task definition file', async () => {
fs.existsSync.mockReturnValue(false);
core.getInput = jest
.fn()
.mockReturnValueOnce('does-not-exist-task-definition.json')
.mockReturnValueOnce('web')
.mockReturnValueOnce('nginx:latest');

await run();

expect(core.setFailed).toBeCalledWith('Task definition file does not exist: does-not-exist-task-definition.json');
});

test('error thown for missing task definition, task definition arn and task definition family ', async () => {
core.getInput = jest
.fn()
Expand Down

0 comments on commit 5ca03ff

Please sign in to comment.