Skip to content

Commit 70d7e5a

Browse files
committed
Fix: Ignore task definition fields that are Describe outputs, but not Register inputs
Fixes #22
1 parent 33bfed3 commit 70d7e5a

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

dist/index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ const CODE_DEPLOY_MAX_WAIT_MINUTES = 360; // 6 hours
137137
const CODE_DEPLOY_MIN_WAIT_MINUTES = 30;
138138
const CODE_DEPLOY_WAIT_DEFAULT_DELAY_SEC = 15;
139139

140+
// Attributes that are returned by DescribeTaskDefinition, but are not valid RegisterTaskDefinition inputs
141+
const IGNORED_TASK_DEFINITION_ATTRIBUTES = [
142+
'compatibilities',
143+
'taskDefinitionArn',
144+
'requiresAttributes',
145+
'revision',
146+
'status'
147+
];
148+
140149
// Deploy to a service that uses the 'ECS' deployment controller
141150
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService) {
142151
core.debug('Updating the service');
@@ -191,6 +200,20 @@ function cleanNullKeys(obj) {
191200
return JSON.parse(JSON.stringify(obj, undefinedOrNullReplacer));
192201
}
193202

203+
function removeIgnoredAttributes(taskDef) {
204+
for (var attribute of IGNORED_TASK_DEFINITION_ATTRIBUTES) {
205+
if (taskDef[attribute]) {
206+
core.warning(`Ignoring property '${attribute}' in the task definition file. ` +
207+
'This property is returned by the Amazon ECS DescribeTaskDefinition API and may be shown in the ECS console, ' +
208+
'but it is not a valid field when registering a new task definition. ' +
209+
'This field can be safely removed from your task definition file.');
210+
delete taskDef[attribute];
211+
}
212+
}
213+
214+
return taskDef;
215+
}
216+
194217
// Deploy to a service that uses the 'CODE_DEPLOY' deployment controller
195218
async function createCodeDeployDeployment(codedeploy, clusterName, service, taskDefArn, waitForService) {
196219
core.debug('Updating AppSpec file with new task definition ARN');
@@ -292,7 +315,7 @@ async function run() {
292315
taskDefinitionFile :
293316
path.join(process.env.GITHUB_WORKSPACE, taskDefinitionFile);
294317
const fileContents = fs.readFileSync(taskDefPath, 'utf8');
295-
const taskDefContents = cleanNullKeys(yaml.parse(fileContents));
318+
const taskDefContents = removeIgnoredAttributes(cleanNullKeys(yaml.parse(fileContents)));
296319
const registerResponse = await ecs.registerTaskDefinition(taskDefContents).promise();
297320
const taskDefArn = registerResponse.taskDefinition.taskDefinitionArn;
298321
core.setOutput('task-definition-arn', taskDefArn);

index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ const CODE_DEPLOY_MAX_WAIT_MINUTES = 360; // 6 hours
1010
const CODE_DEPLOY_MIN_WAIT_MINUTES = 30;
1111
const CODE_DEPLOY_WAIT_DEFAULT_DELAY_SEC = 15;
1212

13+
// Attributes that are returned by DescribeTaskDefinition, but are not valid RegisterTaskDefinition inputs
14+
const IGNORED_TASK_DEFINITION_ATTRIBUTES = [
15+
'compatibilities',
16+
'taskDefinitionArn',
17+
'requiresAttributes',
18+
'revision',
19+
'status'
20+
];
21+
1322
// Deploy to a service that uses the 'ECS' deployment controller
1423
async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForService) {
1524
core.debug('Updating the service');
@@ -64,6 +73,20 @@ function cleanNullKeys(obj) {
6473
return JSON.parse(JSON.stringify(obj, undefinedOrNullReplacer));
6574
}
6675

76+
function removeIgnoredAttributes(taskDef) {
77+
for (var attribute of IGNORED_TASK_DEFINITION_ATTRIBUTES) {
78+
if (taskDef[attribute]) {
79+
core.warning(`Ignoring property '${attribute}' in the task definition file. ` +
80+
'This property is returned by the Amazon ECS DescribeTaskDefinition API and may be shown in the ECS console, ' +
81+
'but it is not a valid field when registering a new task definition. ' +
82+
'This field can be safely removed from your task definition file.');
83+
delete taskDef[attribute];
84+
}
85+
}
86+
87+
return taskDef;
88+
}
89+
6790
// Deploy to a service that uses the 'CODE_DEPLOY' deployment controller
6891
async function createCodeDeployDeployment(codedeploy, clusterName, service, taskDefArn, waitForService) {
6992
core.debug('Updating AppSpec file with new task definition ARN');
@@ -165,7 +188,7 @@ async function run() {
165188
taskDefinitionFile :
166189
path.join(process.env.GITHUB_WORKSPACE, taskDefinitionFile);
167190
const fileContents = fs.readFileSync(taskDefPath, 'utf8');
168-
const taskDefContents = cleanNullKeys(yaml.parse(fileContents));
191+
const taskDefContents = removeIgnoredAttributes(cleanNullKeys(yaml.parse(fileContents)));
169192
const registerResponse = await ecs.registerTaskDefinition(taskDefContents).promise();
170193
const taskDefArn = registerResponse.taskDefinition.taskDefinitionArn;
171194
core.setOutput('task-definition-arn', taskDefArn);

index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,20 @@ describe('Deploy to ECS', () => {
170170
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'});
171171
});
172172

173+
test('cleans invalid keys out of the task definition contents', async () => {
174+
fs.readFileSync.mockImplementation((pathInput, encoding) => {
175+
if (encoding != 'utf8') {
176+
throw new Error(`Wrong encoding ${encoding}`);
177+
}
178+
179+
return '{ "compatibilities": ["EC2"], "taskDefinitionArn": "arn:aws...:task-def-family:1", "family": "task-def-family", "revision": 1, "status": "ACTIVE" }';
180+
});
181+
182+
await run();
183+
expect(core.setFailed).toHaveBeenCalledTimes(0);
184+
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'});
185+
});
186+
173187
test('registers the task definition contents and creates a CodeDeploy deployment', async () => {
174188
core.getInput = jest
175189
.fn()

0 commit comments

Comments
 (0)