generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
111 lines (98 loc) · 2.93 KB
/
main.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
import * as core from '@actions/core'
import { LambdaFunction, findLambdas } from './findLambdas'
import { DeployApplicationInput, DeployInput, Resource } from './types'
import { deploy } from './deploy'
const LAMBDA_ALIAS = 'live'
/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
*/
export async function run(): Promise<void> {
try {
// const applicationName: string = 'betting-api'
// const deploymentConfigName: string = 'CodeDeployDefault.LambdaAllAtOnce'
// const description: string = 'Deploy betting-api'
// const tagKey: string = 'Application'
// const tagValues: string[] = ['betting-api']
const applicationName: string = core.getInput('applicationName')
const deploymentConfigName: string = core.getInput('deploymentConfigName')
const description: string = core.getInput('description')
const tagKey: string = core.getInput('tagKey')
const tagValues: string[] = core
.getInput('tagValues')
.split(',')
.map(value => value.trim())
await deployApplication({
applicationName,
deploymentConfigName,
description,
tagKey,
tagValues
})
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
}
}
const deployApplication = async (
data: DeployApplicationInput
): Promise<void> => {
const allLambdas = await findLambdas({
tags: {
[data.tagKey]: data.tagValues
},
alias: LAMBDA_ALIAS
})
const lambdas = allLambdas.filter(
lambda => lambda.latestVersion !== lambda.aliasVersion
)
const resources = lambdas.map(lambda => {
return {
[lambda.name]: fromLambdaFunctionToResource(lambda)
}
})
if (resources.length === 0) {
core.notice('No lambdas to deploy')
return
}
for (const resource of resources) {
const input: DeployInput = {
applicationName: data.applicationName,
deploymentGroupName: Object.keys(resource)[0],
deploymentConfigName: data.deploymentConfigName,
description: data.description,
revision: {
revisionType: 'AppSpecContent',
appSpecContent: {
content: JSON.stringify({
version: '0.0.1',
Resources: [resource]
})
}
}
}
await delay(1000)
await deploy(input)
}
core.info('Deployments triggered:')
for (const lambda of lambdas) {
core.info(
` - ${lambda.name}: ${lambda.aliasVersion} -> ${lambda.latestVersion}`
)
}
return
}
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
export const fromLambdaFunctionToResource = (
lambdaFunction: LambdaFunction
): Resource => {
return {
Type: 'AWS::Lambda::Function',
Properties: {
Name: lambdaFunction.name,
Alias: LAMBDA_ALIAS,
CurrentVersion: lambdaFunction.aliasVersion,
TargetVersion: lambdaFunction.latestVersion
}
}
}