Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add possibility to add more than one container in container-name #229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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