Skip to content

Commit d7db970

Browse files
committed
Clean null-value keys from the task definition
Fixes #6 Fixes #2
1 parent 31b03ed commit d7db970

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

dist/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ function findAppSpecKey(obj, keyName) {
248248
throw new Error(`AppSpec file must include property '${keyName}'`);
249249
}
250250

251+
function undefinedOrNullReplacer(_, value) {
252+
if (value === null || value === undefined) {
253+
return undefined;
254+
}
255+
256+
return value;
257+
}
258+
259+
function cleanNullKeys(obj) {
260+
return JSON.parse(JSON.stringify(obj, undefinedOrNullReplacer));
261+
}
262+
251263
// Deploy to a service that uses the 'CODE_DEPLOY' deployment controller
252264
async function createCodeDeployDeployment(codedeploy, clusterName, service, taskDefArn, waitForService) {
253265
core.debug('Updating AppSpec file with new task definition ARN');
@@ -349,7 +361,7 @@ async function run() {
349361
taskDefinitionFile :
350362
path.join(process.env.GITHUB_WORKSPACE, taskDefinitionFile);
351363
const fileContents = fs.readFileSync(taskDefPath, 'utf8');
352-
const taskDefContents = yaml.parse(fileContents);
364+
const taskDefContents = cleanNullKeys(yaml.parse(fileContents));
353365
const registerResponse = await ecs.registerTaskDefinition(taskDefContents).promise();
354366
const taskDefArn = registerResponse.taskDefinition.taskDefinitionArn;
355367
core.setOutput('task-definition-arn', taskDefArn);

index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ function findAppSpecKey(obj, keyName) {
5252
throw new Error(`AppSpec file must include property '${keyName}'`);
5353
}
5454

55+
function undefinedOrNullReplacer(_, value) {
56+
if (value === null || value === undefined) {
57+
return undefined;
58+
}
59+
60+
return value;
61+
}
62+
63+
function cleanNullKeys(obj) {
64+
return JSON.parse(JSON.stringify(obj, undefinedOrNullReplacer));
65+
}
66+
5567
// Deploy to a service that uses the 'CODE_DEPLOY' deployment controller
5668
async function createCodeDeployDeployment(codedeploy, clusterName, service, taskDefArn, waitForService) {
5769
core.debug('Updating AppSpec file with new task definition ARN');
@@ -153,7 +165,7 @@ async function run() {
153165
taskDefinitionFile :
154166
path.join(process.env.GITHUB_WORKSPACE, taskDefinitionFile);
155167
const fileContents = fs.readFileSync(taskDefPath, 'utf8');
156-
const taskDefContents = yaml.parse(fileContents);
168+
const taskDefContents = cleanNullKeys(yaml.parse(fileContents));
157169
const registerResponse = await ecs.registerTaskDefinition(taskDefContents).promise();
158170
const taskDefArn = registerResponse.taskDefinition.taskDefinitionArn;
159171
core.setOutput('task-definition-arn', taskDefArn);

index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@ describe('Deploy to ECS', () => {
156156
expect(mockEcsWaiter).toHaveBeenCalledTimes(0);
157157
});
158158

159+
test('cleans null keys out of the task definition contents', async () => {
160+
fs.readFileSync.mockImplementation((pathInput, encoding) => {
161+
if (encoding != 'utf8') {
162+
throw new Error(`Wrong encoding ${encoding}`);
163+
}
164+
165+
return '{ "ipcMode": null, "family": "task-def-family" }';
166+
});
167+
168+
await run();
169+
expect(core.setFailed).toHaveBeenCalledTimes(0);
170+
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'});
171+
});
172+
159173
test('registers the task definition contents and creates a CodeDeploy deployment', async () => {
160174
core.getInput = jest
161175
.fn()

0 commit comments

Comments
 (0)