-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (106 loc) · 3.02 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
const core = require('@actions/core')
const {
ECSClient,
DescribeTaskDefinitionCommand,
DescribeServicesCommand,
} = require('@aws-sdk/client-ecs')
const { merge, head, omit } = require('lodash')
const tmp = require('tmp')
const fs = require('fs')
const IGNORED_TASK_DEFINITION_ATTRIBUTES = [
'compatibilities',
'taskDefinitionArn',
'requiresAttributes',
'revision',
'status',
'registeredAt',
'deregisteredAt',
'registeredBy'
]
const getTaskDefinition = async ({
taskDefinition,
client,
}) => {
const command = new DescribeTaskDefinitionCommand({
taskDefinition,
client
})
try {
const { taskDefinition } = await client.send(command)
return taskDefinition
} catch (error) {
core.setFailed(error.message)
}
}
const getECSService = async ({
cluster,
service,
client
}) => {
const command = new DescribeServicesCommand({
services: [service],
cluster
})
const { services } = await client.send(command)
if (services.length < 1) {
throw new ReferenceError('Service not found')
}
return services
}
async function run() {
const aws_region = core.getInput('region')
const cluster = core.getInput('cluster-name')
const service = core.getInput('service-name')
const task = core.getInput('task-name')
core.info(`Start client with region ${aws_region}`)
const client = new ECSClient({ region: aws_region })
try {
if (service !== '') {
const services = await getECSService({
cluster,
service,
client
})
const { taskDefinition } = head(services)
core.info(`Task definition from service ${taskDefinition}`)
const taskDef = await getTaskDefinition({
taskDefinition,
client,
})
const replacements = core.getInput('replacements') || '{}'
const taskDefMerged = merge(taskDef, JSON.parse(replacements))
const newTaskDef = omit(taskDefMerged, IGNORED_TASK_DEFINITION_ATTRIBUTES)
// create a a file for task def
const taskDefFile = tmp.fileSync({
tmpdir: process.env.RUNNER_TEMP,
prefix: 'task-definition-',
postfix: '.json',
keep: true,
discardDescriptor: true
})
fs.writeFileSync(taskDefFile.name, JSON.stringify(newTaskDef))
core.setOutput('taskDef', taskDefFile.name)
} else {
const taskDef = await getTaskDefinition({
taskDefinition: task,
client,
})
const replacements = core.getInput('replacements') || '{}'
const taskDefMerged = merge(taskDef, JSON.parse(replacements))
const newTaskDef = omit(taskDefMerged, IGNORED_TASK_DEFINITION_ATTRIBUTES)
console.dir(newTaskDef)
const taskDefFile = tmp.fileSync({
tmpdir: process.env.RUNNER_TEMP,
prefix: 'task-definition-',
postfix: '.json',
keep: true,
discardDescriptor: true
})
fs.writeFileSync(taskDefFile.name, JSON.stringify(newTaskDef))
core.setOutput('taskDef', taskDefFile.name)
}
} catch (error) {
core.setFailed(error.message)
}
}
run()