-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathdeploy.ts
230 lines (205 loc) · 5.69 KB
/
deploy.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import * as core from '@actions/core'
import {
CloudFormationClient,
Stack,
DescribeChangeSetCommand,
DeleteChangeSetCommand,
waitUntilChangeSetCreateComplete,
waitUntilStackUpdateComplete,
waitUntilStackCreateComplete,
CreateChangeSetCommand,
ExecuteChangeSetCommand,
DescribeStacksCommand,
CreateStackCommand,
CloudFormationServiceException
} from '@aws-sdk/client-cloudformation'
import { CreateChangeSetInput, CreateStackInputWithName } from './main'
export async function cleanupChangeSet(
cfn: CloudFormationClient,
stack: Stack,
params: CreateChangeSetInput,
noEmptyChangeSet: boolean,
noDeleteFailedChangeSet: boolean
): Promise<string | undefined> {
const knownErrorMessages = [
`No updates are to be performed`,
`The submitted information didn't contain changes`
]
const changeSetStatus = await cfn.send(
new DescribeChangeSetCommand({
ChangeSetName: params.ChangeSetName,
StackName: params.StackName
})
)
if (changeSetStatus.Status === 'FAILED') {
core.debug('Deleting failed Change Set')
if (!noDeleteFailedChangeSet) {
cfn.send(
new DeleteChangeSetCommand({
ChangeSetName: params.ChangeSetName,
StackName: params.StackName
})
)
}
if (
noEmptyChangeSet &&
knownErrorMessages.some(err =>
changeSetStatus.StatusReason?.includes(err)
)
) {
return stack.StackId
}
throw new Error(
`Failed to create Change Set: ${changeSetStatus.StatusReason}`
)
}
}
export async function updateStack(
cfn: CloudFormationClient,
stack: Stack,
params: CreateChangeSetInput,
noEmptyChangeSet: boolean,
noExecuteChangeSet: boolean,
noDeleteFailedChangeSet: boolean
): Promise<string | undefined> {
core.debug('Creating CloudFormation Change Set')
await cfn.send(new CreateChangeSetCommand(params))
try {
core.debug('Waiting for CloudFormation Change Set creation')
await waitUntilChangeSetCreateComplete(
{ client: cfn, maxWaitTime: 1800, minDelay: 10 },
{
ChangeSetName: params.ChangeSetName,
StackName: params.StackName
}
)
} catch (err) {
return cleanupChangeSet(
cfn,
stack,
params,
noEmptyChangeSet,
noDeleteFailedChangeSet
)
}
if (noExecuteChangeSet) {
core.debug('Not executing the change set')
return stack.StackId
}
core.debug('Executing CloudFormation change set')
await cfn.send(
new ExecuteChangeSetCommand({
ChangeSetName: params.ChangeSetName,
StackName: params.StackName
})
)
core.debug('Updating CloudFormation stack')
await waitUntilStackUpdateComplete(
{ client: cfn, maxWaitTime: 43200, minDelay: 10 },
{
StackName: params.StackName
}
)
return stack.StackId
}
async function getStack(
cfn: CloudFormationClient,
stackNameOrId: string
): Promise<Stack | undefined> {
try {
const stacks = await cfn.send(
new DescribeStacksCommand({
StackName: stackNameOrId
})
)
if (stacks.Stacks?.[0]) {
return stacks.Stacks[0]
}
throw new Error(
`Stack ${stackNameOrId} not found, but CloudFormation did not throw an exception. This is an unexpected situation, has the SDK changed unexpectedly?`
)
} catch (e) {
if (
e instanceof CloudFormationServiceException &&
e.$metadata.httpStatusCode === 400 &&
e.name === 'ValidationError'
) {
return undefined
}
throw e
}
}
export async function deployStack(
cfn: CloudFormationClient,
params: CreateStackInputWithName,
changeSetName: string,
noEmptyChangeSet: boolean,
noExecuteChangeSet: boolean,
noDeleteFailedChangeSet: boolean
): Promise<string | undefined> {
const stack = await getStack(cfn, params.StackName)
if (!stack) {
core.debug(`Creating CloudFormation Stack`)
const stack = await cfn.send(
new CreateStackCommand({
StackName: params.StackName,
TemplateBody: params.TemplateBody,
TemplateURL: params.TemplateURL,
Parameters: params.Parameters,
Capabilities: params.Capabilities,
ResourceTypes: params.ResourceTypes,
RoleARN: params.RoleARN,
RollbackConfiguration: params.RollbackConfiguration,
NotificationARNs: params.NotificationARNs,
DisableRollback: params.DisableRollback,
Tags: params.Tags,
TimeoutInMinutes: params.TimeoutInMinutes,
EnableTerminationProtection: params.EnableTerminationProtection
})
)
await waitUntilStackCreateComplete(
{ client: cfn, maxWaitTime: 43200, minDelay: 10 },
{
StackName: params.StackName
}
)
return stack.StackId
}
return await updateStack(
cfn,
stack,
{
ChangeSetName: changeSetName,
...{
StackName: params.StackName,
TemplateBody: params.TemplateBody,
TemplateURL: params.TemplateURL,
Parameters: params.Parameters,
Capabilities: params.Capabilities,
ResourceTypes: params.ResourceTypes,
RoleARN: params.RoleARN,
RollbackConfiguration: params.RollbackConfiguration,
NotificationARNs: params.NotificationARNs,
Tags: params.Tags
}
},
noEmptyChangeSet,
noExecuteChangeSet,
noDeleteFailedChangeSet
)
}
export async function getStackOutputs(
cfn: CloudFormationClient,
stackId: string
): Promise<Map<string, string>> {
const outputs = new Map<string, string>()
const stack = await getStack(cfn, stackId)
if (stack && stack.Outputs) {
for (const output of stack.Outputs) {
if (output.OutputKey && output.OutputValue) {
outputs.set(output.OutputKey, output.OutputValue)
}
}
}
return outputs
}