Skip to content

Commit

Permalink
support dockerLabels
Browse files Browse the repository at this point in the history
Signed-off-by: John Seekins <[email protected]>
  • Loading branch information
johnseekins-pathccm committed Oct 11, 2023
1 parent 47e23f9 commit 6faf656
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ input of the second:
environment-variables: |
LOG_LEVEL=info
ENVIRONMENT=prod
docker-labels: |
SERVICE=service
VERSION=version
- name: Modify Amazon ECS task definition with second container
id: render-app-container
Expand Down
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
docker-labels:
description: "Create/Override options inside dockerLabels. Each variable is key=value, you can specify multiple variables with multi-line YAML."
required: false
outputs:
task-definition:
description: 'The path to the rendered task definition file'
Expand Down
19 changes: 19 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async function run() {

const logConfigurationLogDriver = core.getInput("log-configuration-log-driver", { required: false });
const logConfigurationOptions = core.getInput("log-configuration-options", { required: false });
const dockerLabels = core.getInput('docker-labels', { required: false });

// Parse the task definition
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
Expand Down Expand Up @@ -103,6 +104,24 @@ async function run() {
})
}

if (dockerLabels) {
// If dockerLabels object is missing, create it
if (!containerDef.dockerLabels) { containerDef.dockerLabels = {} }

// Get pairs by splitting on newlines
dockerLabels.split('\n').forEach(function (label) {
// Trim whitespace
label = label.trim();
if (label && label.length) {
if (label.indexOf("=") == -1 ) {
throw new Error(`Can't parse logConfiguration option ${label}. Must be in key=value format, one per line`);
}
const [key, value] = label.split("=");
containerDef.dockerLabels[key] = value;
}
})
}

// Write out a new task definition file
var updatedTaskDefFile = tmp.fileSync({
tmpdir: process.env.RUNNER_TEMP,
Expand Down
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async function run() {

const logConfigurationLogDriver = core.getInput("log-configuration-log-driver", { required: false });
const logConfigurationOptions = core.getInput("log-configuration-options", { required: false });
const dockerLabels = core.getInput('docker-labels', { required: false });

// Parse the task definition
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
Expand Down Expand Up @@ -97,6 +98,24 @@ async function run() {
})
}

if (dockerLabels) {
// If dockerLabels object is missing, create it
if (!containerDef.dockerLabels) { containerDef.dockerLabels = {} }

// Get pairs by splitting on newlines
dockerLabels.split('\n').forEach(function (label) {
// Trim whitespace
label = label.trim();
if (label && label.length) {
if (label.indexOf("=") == -1 ) {
throw new Error(`Can't parse logConfiguration option ${label}. Must be in key=value format, one per line`);
}
const [key, value] = label.split("=");
containerDef.dockerLabels[key] = value;
}
})
}

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

test('renders a task definition at an absolute path, and with initial docker labels empty', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('/hello/task-definition.json') // task-definition
.mockReturnValueOnce('web') // container-name
.mockReturnValueOnce('nginx:latest') // image
.mockReturnValueOnce('EXAMPLE=here') // environment-variables
.mockReturnValueOnce('key1=value1\nkey2=value2'); // docker-labels

jest.mock('/hello/task-definition.json', () => ({
family: 'task-def-family',
containerDefinitions: [
{
name: "web",
image: "some-other-image"
}
]
}), { virtual: true });

await run();

expect(tmp.fileSync).toHaveBeenNthCalledWith(1, {
tmpdir: '/home/runner/work/_temp',
prefix: 'task-definition-',
postfix: '.json',
keep: true,
discardDescriptor: true
});

expect(fs.writeFileSync).toHaveBeenNthCalledWith(1, 'new-task-def-file-name',
JSON.stringify({
family: 'task-def-family',
containerDefinitions: [
{
name: "web",
image: "nginx:latest",
environment: [
{
name: "EXAMPLE",
value: "here"
}
],
dockerLabels : {
"key1":"value1",
"key2":"value2"
}
}
]
}, null, 2)
);
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition', 'new-task-def-file-name');
});

test('renders a task definition at an absolute path, and change existed docker labels empty', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('/hello/task-definition.json') // task-definition
.mockReturnValueOnce('web') // container-name
.mockReturnValueOnce('nginx:latest') // image
.mockReturnValueOnce('EXAMPLE=here') // environment-variables
.mockReturnValueOnce('key1=update_value1\nkey2=update_value2\nkey3=value3'); // docker-labels

jest.mock('/hello/task-definition.json', () => ({
family: 'task-def-family',
containerDefinitions: [
{
name: "web",
image: "some-other-image",
dockerLabels : {
"key1":"value1",
"key2":"value2"
}
}
]
}), { virtual: true });

await run();

expect(tmp.fileSync).toHaveBeenNthCalledWith(1, {
tmpdir: '/home/runner/work/_temp',
prefix: 'task-definition-',
postfix: '.json',
keep: true,
discardDescriptor: true
});

expect(fs.writeFileSync).toHaveBeenNthCalledWith(1, 'new-task-def-file-name',
JSON.stringify({
family: 'task-def-family',
containerDefinitions: [
{
name: "web",
image: "nginx:latest",
environment: [
{
name: "EXAMPLE",
value: "here"
}
],
dockerLabels : {
"key1":"update_value1",
"key2":"update_value2",
"key3":"value3"
}
}
]
}, null, 2)
);
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition', 'new-task-def-file-name');
});

test('error returned for non-JSON task definition contents', async () => {
jest.mock('./non-json-task-definition.json', () => ("hello"), { virtual: true });

Expand Down

0 comments on commit 6faf656

Please sign in to comment.