forked from aws-actions/amazon-ecs-render-task-definition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (90 loc) · 3.54 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
const path = require('path');
const core = require('@actions/core');
const tmp = require('tmp');
const fs = require('fs');
async function run() {
try {
// Get inputs
const taskDefinitionFile = core.getInput('task-definition', { required: true });
const containerName = core.getInput('container-name', { required: true });
const imageURI = core.getInput('image', { required: true });
const taskFamily = core.getInput('task-family', { required: true });
const entryPoint = core.getInput('entry-point', { required: false }) || '';
const logGroup = core.getInput('log-group', { required: false }) || '';
const memory = core.getInput('memory', { required: false }) || '';
const cpu = core.getInput('cpu', { required: false }) || '';
const storageSize = core.getInput('ephemeral-storage-size-in-gib', { required: false }) || '';
const environmentVariablesFile = core.getInput('environment-file', { required: false }) || '';
const secretsFile = core.getInput('secrets-file', { required: false }) || '';
// Parse the task definition
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
taskDefinitionFile :
path.join(process.env.GITHUB_WORKSPACE, taskDefinitionFile);
if (!fs.existsSync(taskDefPath)) {
throw new Error(`Task definition file does not exist: ${taskDefinitionFile}`);
}
const taskDefContents = require(taskDefPath);
taskDefContents.family = taskFamily;
// Insert the image URI
if (!Array.isArray(taskDefContents.containerDefinitions)) {
throw new Error('Invalid task definition format: containerDefinitions section is not present or is not an array');
}
const containerDef = taskDefContents.containerDefinitions.find(function(element) {
return element.name == containerName;
});
if (!containerDef) {
throw new Error('Invalid task definition: Could not find container definition with matching name');
}
containerDef.image = imageURI;
if (entryPoint) {
containerDef.entryPoint = entryPoint.split(' ');
}
if (logGroup) {
containerDef.logConfiguration.options['awslogs-group'] = logGroup;
} else {
containerDef.logConfiguration.options['awslogs-group'] = `/ecs/${taskFamily}`;
}
if (environmentVariablesFile) {
const envFilePath = path.isAbsolute(environmentVariablesFile) ?
environmentVariablesFile :
path.join(process.env.GITHUB_WORKSPACE, environmentVariablesFile);
containerDef.environment = require(envFilePath);
}
if (secretsFile) {
const secretsFilePath = path.isAbsolute(secretsFile) ?
secretsFile :
path.join(process.env.GITHUB_WORKSPACE, secretsFile);
containerDef.secrets = require(secretsFilePath);
}
if (memory) {
taskDefContents.memory = memory;
}
if (cpu) {
taskDefContents.cpu = cpu;
}
if (storageSize) {
taskDefContents.ephemeralStorage = {
sizeInGiB: parseInt(storageSize, 10)
};
}
// Write out a new task definition file
var updatedTaskDefFile = tmp.fileSync({
tmpdir: process.env.RUNNER_TEMP,
prefix: 'task-definition-',
postfix: '.json',
keep: true,
discardDescriptor: true
});
const newTaskDefContents = JSON.stringify(taskDefContents, null, 2);
fs.writeFileSync(updatedTaskDefFile.name, newTaskDefContents);
core.setOutput('task-definition', updatedTaskDefFile.name);
}
catch (error) {
core.setFailed(error.message);
}
}
module.exports = run;
/* istanbul ignore next */
if (require.main === module) {
run();
}