-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
139 lines (132 loc) · 4.49 KB
/
Jenkinsfile
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
pipeline {
agent any
stages {
stage('Build API') {
steps {
dir('holiday-calendar-lambda') {
bat 'sam build'
}
}
}
stage('Deploy API') {
steps {
withAWS(credentials: 'AWS Credentials') {
dir('holiday-calendar-lambda') {
bat 'sam deploy --config-env prod --no-fail-on-empty-changeset'
}
}
}
}
stage('Configure UI') {
steps {
script {
apiUrl = getApiUrl()
replaceApiUrl apiUrl
}
}
}
stage('Install UI dependencies') {
steps {
dir('holiday-calendar-web') {
bat 'npm install'
}
}
}
stage('Build UI') {
steps {
dir('holiday-calendar-web') {
bat 'npm run build:prod'
}
}
}
stage('Create UI package') {
steps {
dir('holiday-calendar-web') {
bat 'xcopy /Y "deployment\\Procfile" dist'
zip zipFile: 'dist/holiday-calendar-web.zip', dir: 'dist', overwrite: true
}
}
}
stage('Upload UI package') {
steps {
dir('holiday-calendar-web') {
withAWS(credentials: 'AWS Credentials') {
bat 'aws s3 cp dist/holiday-calendar-web.zip s3://elasticbeanstalk-holiday-calendar-ap-southeast-1'
}
}
}
}
stage('Create UI Elastic Beanstalk environment') {
steps {
dir('holiday-calendar-web/deployment') {
withAWS(credentials: 'AWS Credentials', region: 'ap-southeast-1') {
script {
webStackName = 'holiday-calendar-web'
if (isStackExisting(webStackName)) {
echo "Elastic Beanstalk stack ${webStackName} already exists. Deploying new source code."
deployNewCode()
} else {
echo 'Create new Elastic Beanstalk stack.'
createWebStack(webStackName)
}
}
}
}
}
}
}
}
def getApiUrl() {
withAWS(credentials: 'AWS Credentials', region: 'ap-southeast-1') {
script = '''
@echo off
aws cloudformation describe-stacks ^
--stack-name holiday-calendar-lambda ^
--query "Stacks[0].Outputs[?OutputKey==\'HolidayCalendarApi\'].OutputValue" ^
--output text
'''
apiUrl = bat(script: script, returnStdout: true)
return apiUrl.replaceAll('\\r?\\n$', '')
}
}
def replaceApiUrl(apiUrl) {
environmentFile = 'holiday-calendar-web/src/environments/environment.ts'
content = readFile(environmentFile)
newContent = content.replace('<prod-api-url-place-holder>', apiUrl)
echo "Override environment.ts with: ${newContent}"
writeFile(file: environmentFile, text: newContent)
}
def isStackExisting(stackName) {
script = """
@echo off
aws cloudformation list-stacks ^
--query \"StackSummaries[?StackName == '${stackName}' && StackStatus == 'CREATE_COMPLETE']\"
"""
output = bat(script: script, returnStdout: true)
return output.contains(stackName)
}
def createWebStack(webStackName) {
bat """
aws cloudformation create-stack ^
--stack-name ${webStackName} ^
--template-body file://template.yaml ^
--capabilities CAPABILITY_NAMED_IAM
"""
}
def deployNewCode() {
version = createNewVersion()
bat """
aws elasticbeanstalk create-application-version ^
--application-name holiday-calendar-web ^
--version-label ${version} ^
--source-bundle S3Bucket=elasticbeanstalk-holiday-calendar-ap-southeast-1,S3Key=holiday-calendar-web.zip
"""
bat """
aws elasticbeanstalk update-environment ^
--environment-name holiday-calendar-web ^
--version-label ${version}
"""
}
def createNewVersion() {
new Date().format('yyyyMMdd-HHmmss-SSS')
}