diff --git a/action.yml b/action.yml index bffb1cba..1a3ac7cd 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/dist/index.js b/dist/index.js index 4cd74d74..e441e4c2 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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(' ') @@ -224,6 +233,7 @@ async function run() { } }) } + }); // Write out a new task definition file var updatedTaskDefFile = tmp.fileSync({ diff --git a/index.js b/index.js index 95467f9b..2c25527f 100644 --- a/index.js +++ b/index.js @@ -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(' ') @@ -218,6 +227,7 @@ async function run() { } }) } + }); // Write out a new task definition file var updatedTaskDefFile = tmp.fileSync({ diff --git a/index.test.js b/index.test.js index 18634c71..39018171 100644 --- a/index.test.js +++ b/index.test.js @@ -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()