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 Feb 6, 2023
1 parent cb3289a commit 9078387
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ inputs:
description: 'The path to the ECS task definition JSON file'
required: true
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
33 changes: 21 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1406,17 +1406,26 @@ async function run() {
}
const taskDefContents = require(taskDefPath);

// 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 == containerName;
});
if (!containerDef) {
throw new Error('Invalid task definition: Could not find container definition with matching name');
}
containerDef.image = imageURI;
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');
}

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 (environmentVariables) {

Expand Down Expand Up @@ -1454,7 +1463,7 @@ async function run() {
}
})
}

});

// Write out a new task definition file
var updatedTaskDefFile = tmp.fileSync({
Expand Down
31 changes: 20 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@ async function run() {
}
const taskDefContents = require(taskDefPath);

// 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 (environmentVariables) {

Expand Down Expand Up @@ -69,7 +78,7 @@ async function run() {
}
})
}

});

// Write out a new task definition file
var updatedTaskDefFile = tmp.fileSync({
Expand Down
12 changes: 12 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ describe('Render task definition', () => {
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition', 'new-task-def-file-name');
});

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
Expand Down

0 comments on commit 9078387

Please sign in to comment.