-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (129 loc) · 4.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const path = require('path');
const core = require('@actions/core');
const aws = require('aws-sdk');
const yaml = require('yaml');
const fs = require('fs');
// 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 isEmptyValue(value) {
if (value === null || value === undefined || value === '') {
return true;
}
if (Array.isArray(value)) {
for (var element of value) {
if (!isEmptyValue(element)) {
// the array has at least one non-empty element
return false;
}
}
// the array has no non-empty elements
return true;
}
if (typeof value === 'object') {
for (var childValue of Object.values(value)) {
if (!isEmptyValue(childValue)) {
// the object has at least one non-empty property
return false;
}
}
// the object has no non-empty property
return true;
}
return false;
}
function emptyValueReplacer(_, value) {
if (isEmptyValue(value)) {
return undefined;
}
if (Array.isArray(value)) {
return value.filter(e => !isEmptyValue(e));
}
return value;
}
function cleanNullKeys(obj) {
return JSON.parse(JSON.stringify(obj, emptyValueReplacer));
}
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;
}
function maintainValidObjects(taskDef) {
if (validateProxyConfigurations(taskDef)) {
taskDef.proxyConfiguration.properties.forEach((property, index, arr) => {
if (!('value' in property)) {
arr[index].value = '';
}
if (!('name' in property)) {
arr[index].name = '';
}
});
}
if(taskDef && taskDef.containerDefinitions){
taskDef.containerDefinitions.forEach((container) => {
if(container.environment){
container.environment.forEach((property, index, arr) => {
if (!('value' in property)) {
arr[index].value = '';
}
});
}
});
}
return taskDef;
}
function validateProxyConfigurations(taskDef){
return 'proxyConfiguration' in taskDef && taskDef.proxyConfiguration.type && taskDef.proxyConfiguration.type == 'APPMESH' && taskDef.proxyConfiguration.properties && taskDef.proxyConfiguration.properties.length > 0;
}
async function run() {
try {
const ecs = new aws.ECS({
customUserAgent: 'amazon-ecs-deploy-task-definition-for-github-actions'
});
// Get inputs
const taskDefinitionFile = core.getInput('task-definition', { required: true });
// Register the task definition
core.debug('Registering the task definition');
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
taskDefinitionFile :
path.join(process.env.GITHUB_WORKSPACE, taskDefinitionFile);
const fileContents = fs.readFileSync(taskDefPath, 'utf8');
const taskDefContents = maintainValidObjects(removeIgnoredAttributes(cleanNullKeys(yaml.parse(fileContents))));
let registerResponse;
try {
registerResponse = await ecs.registerTaskDefinition(taskDefContents).promise();
} catch (error) {
core.setFailed("Failed to register task definition in ECS: " + error.message);
core.debug("Task definition contents:");
core.debug(JSON.stringify(taskDefContents, undefined, 4));
throw(error);
}
const taskDefArn = registerResponse.taskDefinition.taskDefinitionArn;
core.setOutput('task-definition-arn', taskDefArn);
}
catch (error) {
core.setFailed(error.message);
core.debug(error.stack);
}
}
module.exports = run;
/* istanbul ignore next */
if (require.main === module) {
run();
}