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

closes #135 #265

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ inputs:
log-configuration-options:
description: "Create/Override options inside logConfiguration. Each variable is of the form key=value, you can specify multiple variables with multi-line YAML strings."
required: false
# tags:
PaddeK marked this conversation as resolved.
Show resolved Hide resolved
# description: "Tags to add to the task definition. Each Tag is of the form KEY=value, you can specify multiple key–value pairs with multi-line YAML strings."
# required: false
outputs:
task-definition:
description: 'The path to the rendered task definition file'
Expand Down
38 changes: 38 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async function run() {
const imageURI = core.getInput('image', { required: true });

const environmentVariables = core.getInput('environment-variables', { required: false });
const tags = core.getInput('tags', {required: false});

const logConfigurationLogDriver = core.getInput("log-configuration-log-driver", { required: false });
const logConfigurationOptions = core.getInput("log-configuration-options", { required: false });
Expand Down Expand Up @@ -73,6 +74,43 @@ async function run() {
})
}

if (tags) {

// If tags array is missing, create it
if (!Array.isArray(containerDef.tags)) {
containerDef.tags = [];
}

// Get pairs by splitting on newlines
tags.split('\n').forEach(function (line) {
// Trim whitespace
const trimmedLine = line.trim();
// Skip if empty
if (trimmedLine.length === 0) { return; }
// Split on =
const separatorIdx = trimmedLine.indexOf("=");
// If there's nowhere to split
if (separatorIdx === -1) {
throw new Error(`Cannot parse the tag '${trimmedLine}'. Tag key–value pairs must be of the form KEY=value.`);
}
// Build object
const tag = {
key: trimmedLine.substring(0, separatorIdx),
value: trimmedLine.substring(separatorIdx + 1),
};

// Search container definition tags for one matching name
const tagDef = containerDef.tags.find((e) => e.key == tag.key);
if (tagDef) {
// If found, update
tagDef.value = tag.value;
} else {
// Else, create
containerDef.tags.push(tag);
}
})
}

if (logConfigurationLogDriver) {
if (!containerDef.logConfiguration) { containerDef.logConfiguration = {} }
const validDrivers = ["json-file", "syslog", "journald", "logentries", "gelf", "fluentd", "awslogs", "splunk", "awsfirelens"];
Expand Down
33 changes: 31 additions & 2 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe('Render task definition', () => {
.mockReturnValueOnce('task-definition.json') // task-definition
.mockReturnValueOnce('web') // container-name
.mockReturnValueOnce('nginx:latest') // image
.mockReturnValueOnce('FOO=bar\nHELLO=world'); // environment-variables
.mockReturnValueOnce('FOO=bar\nHELLO=world') // environment-variables
.mockReturnValueOnce('tag=value\nanother=tag'); // tags

process.env = Object.assign(process.env, { GITHUB_WORKSPACE: __dirname });
process.env = Object.assign(process.env, { RUNNER_TEMP: '/home/runner/work/_temp' });
Expand Down Expand Up @@ -92,6 +93,16 @@ describe('Render task definition', () => {
name: "HELLO",
value: "world"
}
],
tags: [
{
key: "tag",
value: "value"
},
{
key: "another",
value: "tag"
}
]
},
{
Expand All @@ -110,7 +121,8 @@ describe('Render task definition', () => {
.mockReturnValueOnce('/hello/task-definition.json') // task-definition
.mockReturnValueOnce('web') // container-name
.mockReturnValueOnce('nginx:latest') // image
.mockReturnValueOnce('EXAMPLE=here'); // environment-variables
.mockReturnValueOnce('EXAMPLE=here') // environment-variables
.mockReturnValueOnce('tag=value'); // tags
jest.mock('/hello/task-definition.json', () => ({
family: 'task-def-family',
containerDefinitions: [
Expand Down Expand Up @@ -142,6 +154,12 @@ describe('Render task definition', () => {
name: "EXAMPLE",
value: "here"
}
],
tags: [
{
key: "tag",
value: "value"
}
]
}
]
Expand All @@ -157,6 +175,7 @@ describe('Render task definition', () => {
.mockReturnValueOnce('web')
.mockReturnValueOnce('nginx:latest')
.mockReturnValueOnce('FOO=bar\nHELLO=world')
.mockReturnValueOnce('tag=value\nanother=tag')
.mockReturnValueOnce('awslogs')
.mockReturnValueOnce(`awslogs-create-group=true\nawslogs-group=/ecs/web\nawslogs-region=us-east-1\nawslogs-stream-prefix=ecs`);

Expand Down Expand Up @@ -192,6 +211,16 @@ describe('Render task definition', () => {
value: "world"
}
],
tags: [
{
key: "tag",
value: "value"
},
{
key: "another",
value: "tag"
}
],
logConfiguration: {
logDriver: "awslogs",
options: {
Expand Down