Skip to content

Commit

Permalink
Fix: Ignore task definition fields that are Describe outputs, but not…
Browse files Browse the repository at this point in the history
… Register inputs

Fixes #22
  • Loading branch information
clareliguori committed Jan 16, 2020
1 parent 33bfed3 commit 70d7e5a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
25 changes: 24 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ const CODE_DEPLOY_MAX_WAIT_MINUTES = 360; // 6 hours
const CODE_DEPLOY_MIN_WAIT_MINUTES = 30;
const CODE_DEPLOY_WAIT_DEFAULT_DELAY_SEC = 15;

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

// Deploy to a service that uses the 'ECS' deployment controller
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService) {
core.debug('Updating the service');
Expand Down Expand Up @@ -191,6 +200,20 @@ function cleanNullKeys(obj) {
return JSON.parse(JSON.stringify(obj, undefinedOrNullReplacer));
}

function removeIgnoredAttributes(taskDef) {
for (var attribute of IGNORED_TASK_DEFINITION_ATTRIBUTES) {
if (taskDef[attribute]) {
core.warning(`Ignoring property '${attribute}' in the task definition file. ` +
'This property is returned by the Amazon ECS DescribeTaskDefinition API and may be shown in the ECS console, ' +
'but it is not a valid field when registering a new task definition. ' +
'This field can be safely removed from your task definition file.');
delete taskDef[attribute];
}
}

return taskDef;
}

// Deploy to a service that uses the 'CODE_DEPLOY' deployment controller
async function createCodeDeployDeployment(codedeploy, clusterName, service, taskDefArn, waitForService) {
core.debug('Updating AppSpec file with new task definition ARN');
Expand Down Expand Up @@ -292,7 +315,7 @@ async function run() {
taskDefinitionFile :
path.join(process.env.GITHUB_WORKSPACE, taskDefinitionFile);
const fileContents = fs.readFileSync(taskDefPath, 'utf8');
const taskDefContents = cleanNullKeys(yaml.parse(fileContents));
const taskDefContents = removeIgnoredAttributes(cleanNullKeys(yaml.parse(fileContents)));
const registerResponse = await ecs.registerTaskDefinition(taskDefContents).promise();
const taskDefArn = registerResponse.taskDefinition.taskDefinitionArn;
core.setOutput('task-definition-arn', taskDefArn);
Expand Down
25 changes: 24 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ const CODE_DEPLOY_MAX_WAIT_MINUTES = 360; // 6 hours
const CODE_DEPLOY_MIN_WAIT_MINUTES = 30;
const CODE_DEPLOY_WAIT_DEFAULT_DELAY_SEC = 15;

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

// Deploy to a service that uses the 'ECS' deployment controller
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService) {
core.debug('Updating the service');
Expand Down Expand Up @@ -64,6 +73,20 @@ function cleanNullKeys(obj) {
return JSON.parse(JSON.stringify(obj, undefinedOrNullReplacer));
}

function removeIgnoredAttributes(taskDef) {
for (var attribute of IGNORED_TASK_DEFINITION_ATTRIBUTES) {
if (taskDef[attribute]) {
core.warning(`Ignoring property '${attribute}' in the task definition file. ` +
'This property is returned by the Amazon ECS DescribeTaskDefinition API and may be shown in the ECS console, ' +
'but it is not a valid field when registering a new task definition. ' +
'This field can be safely removed from your task definition file.');
delete taskDef[attribute];
}
}

return taskDef;
}

// Deploy to a service that uses the 'CODE_DEPLOY' deployment controller
async function createCodeDeployDeployment(codedeploy, clusterName, service, taskDefArn, waitForService) {
core.debug('Updating AppSpec file with new task definition ARN');
Expand Down Expand Up @@ -165,7 +188,7 @@ async function run() {
taskDefinitionFile :
path.join(process.env.GITHUB_WORKSPACE, taskDefinitionFile);
const fileContents = fs.readFileSync(taskDefPath, 'utf8');
const taskDefContents = cleanNullKeys(yaml.parse(fileContents));
const taskDefContents = removeIgnoredAttributes(cleanNullKeys(yaml.parse(fileContents)));
const registerResponse = await ecs.registerTaskDefinition(taskDefContents).promise();
const taskDefArn = registerResponse.taskDefinition.taskDefinitionArn;
core.setOutput('task-definition-arn', taskDefArn);
Expand Down
14 changes: 14 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,20 @@ describe('Deploy to ECS', () => {
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'});
});

test('cleans invalid keys out of the task definition contents', async () => {
fs.readFileSync.mockImplementation((pathInput, encoding) => {
if (encoding != 'utf8') {
throw new Error(`Wrong encoding ${encoding}`);
}

return '{ "compatibilities": ["EC2"], "taskDefinitionArn": "arn:aws...:task-def-family:1", "family": "task-def-family", "revision": 1, "status": "ACTIVE" }';
});

await run();
expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'});
});

test('registers the task definition contents and creates a CodeDeploy deployment', async () => {
core.getInput = jest
.fn()
Expand Down

0 comments on commit 70d7e5a

Please sign in to comment.