Skip to content
Merged
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
38 changes: 34 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ const tmp = __nccwpck_require__(1288);
const fs = __nccwpck_require__(9896);
const {ECS} = __nccwpck_require__(212);

// Attributes that are returned by DescribeTaskDefinition, but are not valid RegisterTaskDefinition inputs
const IGNORED_TASK_DEFINITION_ATTRIBUTES = [
'compatibilities',
'taskDefinitionArn',
'requiresAttributes',
'revision',
'status',
'registeredAt',
'deregisteredAt',
'registeredBy'
];

function removeIgnoredAttributes(taskDef) {
// Creates a completely new object with its own reference
const cleanTaskDef = JSON.parse(JSON.stringify(taskDef));

// Modifications are made to the new object
IGNORED_TASK_DEFINITION_ATTRIBUTES.forEach(attr => {
delete cleanTaskDef[attr];
});

return cleanTaskDef; // Returns a completely new object
}

async function run() {
try {
const ecs = new ECS({
Expand Down Expand Up @@ -51,13 +75,13 @@ async function run() {
} else if (taskDefinitionArn || taskDefinitionFamily || taskDefinitionRevision) {
if (taskDefinitionArn) {
core.info("The task definition arn will be used to fetch task definition");
params = {taskDefinition: taskDefinitionArn};
params = {taskDefinition: taskDefinitionArn, include: ['TAGS']};
} else if (taskDefinitionFamily && taskDefinitionRevision) {
core.info("The specified revision of the task definition family will be used to fetch task definition");
params = {taskDefinition: `${taskDefinitionFamily}:${taskDefinitionRevision}` };
params = {taskDefinition: `${taskDefinitionFamily}:${taskDefinitionRevision}`, include: ['TAGS'] };
} else if (taskDefinitionFamily) {
core.info("The latest revision of the task definition family will be used to fetch task definition");
params = {taskDefinition: taskDefinitionFamily};
params = {taskDefinition: taskDefinitionFamily, include: ['TAGS']};
} else if (taskDefinitionRevision) {
core.setFailed("You can't fetch task definition with just revision: Either use task definition file, arn or family name");
} else {
Expand All @@ -71,6 +95,8 @@ async function run() {
throw(error);
}
taskDefContents = describeTaskDefResponse.taskDefinition;
// merge tags into taskDefinition
taskDefContents.tags = describeTaskDefResponse.tags;
core.debug("Task definition contents:");
core.debug(JSON.stringify(taskDefContents, undefined, 4));
} else {
Expand Down Expand Up @@ -233,7 +259,11 @@ async function run() {
keep: true,
discardDescriptor: true
});
const newTaskDefContents = JSON.stringify(taskDefContents, null, 2);
// remove ignored attributes just before writting it
const cleanedTaskDef = removeIgnoredAttributes(taskDefContents);
const newTaskDefContents = JSON.stringify(cleanedTaskDef, null, 2);
core.debug('Content being written to file:');
core.debug(newTaskDefContents);
fs.writeFileSync(updatedTaskDefFile.name, newTaskDefContents);
core.setOutput('task-definition', updatedTaskDefFile.name);
}
Expand Down
38 changes: 34 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ const tmp = require('tmp');
const fs = require('fs');
const {ECS} = require('@aws-sdk/client-ecs');

// Attributes that are returned by DescribeTaskDefinition, but are not valid RegisterTaskDefinition inputs
const IGNORED_TASK_DEFINITION_ATTRIBUTES = [
'compatibilities',
'taskDefinitionArn',
'requiresAttributes',
'revision',
'status',
'registeredAt',
'deregisteredAt',
'registeredBy'
];

function removeIgnoredAttributes(taskDef) {
// Creates a completely new object with its own reference
const cleanTaskDef = JSON.parse(JSON.stringify(taskDef));

// Modifications are made to the new object
IGNORED_TASK_DEFINITION_ATTRIBUTES.forEach(attr => {
delete cleanTaskDef[attr];
});

return cleanTaskDef; // Returns a completely new object
}

async function run() {
try {
const ecs = new ECS({
Expand Down Expand Up @@ -45,13 +69,13 @@ async function run() {
} else if (taskDefinitionArn || taskDefinitionFamily || taskDefinitionRevision) {
if (taskDefinitionArn) {
core.info("The task definition arn will be used to fetch task definition");
params = {taskDefinition: taskDefinitionArn};
params = {taskDefinition: taskDefinitionArn, include: ['TAGS']};
} else if (taskDefinitionFamily && taskDefinitionRevision) {
core.info("The specified revision of the task definition family will be used to fetch task definition");
params = {taskDefinition: `${taskDefinitionFamily}:${taskDefinitionRevision}` };
params = {taskDefinition: `${taskDefinitionFamily}:${taskDefinitionRevision}`, include: ['TAGS'] };
} else if (taskDefinitionFamily) {
core.info("The latest revision of the task definition family will be used to fetch task definition");
params = {taskDefinition: taskDefinitionFamily};
params = {taskDefinition: taskDefinitionFamily, include: ['TAGS']};
} else if (taskDefinitionRevision) {
core.setFailed("You can't fetch task definition with just revision: Either use task definition file, arn or family name");
} else {
Expand All @@ -65,6 +89,8 @@ async function run() {
throw(error);
}
taskDefContents = describeTaskDefResponse.taskDefinition;
// merge tags into taskDefinition
taskDefContents.tags = describeTaskDefResponse.tags;
core.debug("Task definition contents:");
core.debug(JSON.stringify(taskDefContents, undefined, 4));
} else {
Expand Down Expand Up @@ -227,7 +253,11 @@ async function run() {
keep: true,
discardDescriptor: true
});
const newTaskDefContents = JSON.stringify(taskDefContents, null, 2);
// remove ignored attributes just before writting it
const cleanedTaskDef = removeIgnoredAttributes(taskDefContents);
const newTaskDefContents = JSON.stringify(cleanedTaskDef, null, 2);
core.debug('Content being written to file:');
core.debug(newTaskDefContents);
fs.writeFileSync(updatedTaskDefFile.name, newTaskDefContents);
core.setOutput('task-definition', updatedTaskDefFile.name);
}
Expand Down
86 changes: 75 additions & 11 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ describe('Render task definition', () => {

jest.mock('./task-definition.json', () => ({
family: 'task-def-family',
revision: 10,
registeredBy: 'arn:aws:sts::012345678901:assumed-role/Role/myrole',
compatibilities: [
"EC2",
"FARGATE"
],
requiresAttributes: [
{
"name": "com.amazonaws.ecs.capability.task-iam-role"
}
],
registeredAt: '1970-01-01T00:00:00.00000+00:00',
deregisteredAt: '1970-01-01T00:01:00.00000+00:00',

containerDefinitions: [
{
name: "web",
Expand Down Expand Up @@ -101,14 +115,32 @@ describe('Render task definition', () => {
name: "sidecar",
image: "hello"
}
],
tags: [
{
key: "project",
value: "mytaskdef"
}
]
}), { virtual: true });

mockEcsDescribeTaskDef.mockImplementation(() => Promise.resolve({
taskDefinition: {
taskDefinitionArn: "task-definition-arn",
taskDefinitionFamily: "task-definition-family",
taskDefinitionRevision: '',
family: "task-definition-family",
revision: 10,
registeredBy: 'arn:aws:sts::012345678901:assumed-role/Role/myrole',
compatibilities: [
"EC2",
"FARGATE"
],
requiresAttributes: [
{
"name": "com.amazonaws.ecs.capability.task-iam-role"
}
],
registeredAt: '1970-01-01T00:00:00.00000+00:00',
deregisteredAt: '1970-01-01T00:01:00.00000+00:00',

containerDefinitions: [
{
Expand Down Expand Up @@ -136,8 +168,13 @@ describe('Render task definition', () => {
image: "hello"
}
]
}

},
tags: [
{
key: "project",
value: "mytaskdef"
}
]
}));
ECS.mockImplementation(() => mockEcsClient);
});
Expand Down Expand Up @@ -199,6 +236,12 @@ describe('Render task definition', () => {
name: "sidecar",
image: "hello"
}
],
tags: [
{
key: "project",
value: "mytaskdef"
}
]
}, null, 2)
);
Expand Down Expand Up @@ -349,6 +392,12 @@ describe('Render task definition', () => {
name: "sidecar",
image: "hello"
}
],
tags: [
{
key: "project",
value: "mytaskdef"
}
]
}, null, 2)
);
Expand All @@ -360,8 +409,8 @@ describe('Render task definition', () => {
mockEcsDescribeTaskDef.mockImplementation(() => Promise.resolve({
taskDefinition: {
taskDefinitionArn: "task-definition-arn",
taskDefinitionFamily: "task-definition-family",
taskDefinitionRevision: 10,
family: "task-definition-family",
revision: 10,

containerDefinitions: [
{
Expand Down Expand Up @@ -503,7 +552,8 @@ describe('Render task definition', () => {

expect(mockEcsClient.describeTaskDefinition).toHaveBeenCalledTimes(1);
expect(mockEcsDescribeTaskDef).toHaveBeenCalledWith({
taskDefinition: "task-definition-family:10"
taskDefinition: "task-definition-family:10",
include: ["TAGS"],
});
});

Expand All @@ -527,7 +577,8 @@ describe('Render task definition', () => {

expect(mockEcsClient.describeTaskDefinition).toHaveBeenCalledTimes(1);
expect(mockEcsDescribeTaskDef).toHaveBeenCalledWith({
taskDefinition: "task-definition-family"
taskDefinition: "task-definition-family",
include: ["TAGS"],
});
expect(core.info).toBeCalledWith("The latest revision of the task definition family will be used to fetch task definition");
});
Expand All @@ -552,8 +603,8 @@ describe('Render task definition', () => {

expect(mockEcsClient.describeTaskDefinition).toHaveBeenCalledTimes(1);
expect(mockEcsDescribeTaskDef).toHaveBeenCalledWith({
taskDefinition: "task-definition-arn"

taskDefinition: "task-definition-arn",
include: ["TAGS"],
});
expect(core.info).toBeCalledWith("The task definition arn will be used to fetch task definition");
});
Expand Down Expand Up @@ -624,7 +675,8 @@ describe('Render task definition', () => {

expect(mockEcsClient.describeTaskDefinition).toHaveBeenCalledTimes(1);
expect(mockEcsDescribeTaskDef).toHaveBeenCalledWith({
taskDefinition: "task-definition-arn"
taskDefinition: "task-definition-arn",
include: ["TAGS"],
});
});

Expand Down Expand Up @@ -734,6 +786,12 @@ describe('Render task definition', () => {
name: "sidecar",
image: "hello"
}
],
tags: [
{
key: "project",
value: "mytaskdef"
}
]
}, null, 2)
);
Expand Down Expand Up @@ -910,6 +968,12 @@ describe('Render task definition', () => {
name: "sidecar",
image: "hello"
}
],
tags: [
{
key: "project",
value: "mytaskdef"
}
]
}, null, 2)
);
Expand Down
Loading