-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJenkinsfile
302 lines (262 loc) · 10.3 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* groovylint-disable NestedBlockDepth */
def nodeInstallationName = 'NodeJS 18.12.0'
/* groovylint-disable DuplicateStringLiteral */
/* groovylint-disable-next-line CompileStatic */
pipeline {
agent any
options {
buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '5', numToKeepStr: '5')
disableConcurrentBuilds()
}
stages {
stage('ci') {
agent {
label 'jenkins-build-agent'
}
tools { nodejs nodeInstallationName }
environment {
NODE_ENV = 'test'
NODE_PORT = 3131
ARANGO_DB_HOST_DOMAIN = 'arangotest.digiteched.com'
ARANGO_DB_HOST_SCHEME = 'https'
ARANGO_DB_HOST_PORT = 443
ARANGO_DB_ROOT_PASSWORD = credentials('arango-test-root-password')
ARANGO_DB_USER = 'dbuser'
ARANGO_DB_USER_PASSWORD = credentials('arango-test-db-user-password')
ARANGO_DB_NAME = 'staging'
AUTH0_ISSUER_URL = 'https://foo.auth0.com/'
AUTH0_AUDIENCE = 'https://bar.mydomain.ca'
GLOBAL_PREFIX = 'api'
SHOULD_ENABLE_LEGACY_GAMES_ENDPOINT = 'false'
}
when {
branch 'PR-*'
}
steps {
configFileProvider([configFile(fileId:'42feff14-78da-45fc-a8ee-5f98213a313f', \
targetLocation: 'apps/coscrad-frontend/src/auth_config.json')]) {
echo 'PR opened or updated...'
echo "NODE ENV: ${NODE_ENV}"
echo 'node version:'
sh 'node -v'
echo 'npm version'
sh 'npm -v'
echo 'Installing dependencies'
sh 'npm ci --legacy-peer-deps'
echo 'Running lint on all COSCRAD projects'
sh 'npm run lint:coscrad'
/**
* Note that the sample content config is actually valid for
* our staging build.
**/
echo 'copying sample content config for test build'
/* groovylint-disable-next-line LineLength */
sh 'cp apps/coscrad-frontend/src/configurable-front-matter/data/content.config.SAMPLE.ts apps/coscrad-frontend/src/configurable-front-matter/data/content.config.ts'
echo 'Building COSCRAD'
echo 'with node version'
sh 'node --version'
sh 'npm run build:coscrad:prod'
echo 'testing coscrad-frontend'
sh 'npx nx test coscrad-frontend --skip-nx-cache'
echo 'testing api (coscrad back-end)'
/**
* While the test falls back to `process.env` whenever there is
* no value from `test.env` we have added an empty `test.env`
* as a hack because by some quirk the mock config implementation
* fails when no file is found.
**/
echo 'creating empty test.env file'
/* groovylint-disable-next-line LineLength */
sh 'touch apps/api/src/app/config/test.env'
sh 'npx nx test api --skip-nx-cache'
}
}
}
stage('build and deploy to staging') {
agent {
label 'jenkins-build-agent'
}
tools { nodejs nodeInstallationName }
environment {
NODE_ENV = 'staging'
NODE_PORT = 3131
ARANGO_DB_HOST_DOMAIN = 'arangotest.digiteched.com'
ARANGO_DB_HOST_SCHEME = 'https'
ARANGO_DB_HOST_PORT = 443
ARANGO_DB_ROOT_PASSWORD = credentials('arango-test-root-password')
ARANGO_DB_USER = 'dbuser'
ARANGO_DB_USER_PASSWORD = credentials('arango-test-db-user-password')
ARANGO_DB_NAME = 'staging'
AUTH0_ISSUER_URL = 'https://foo.auth0.com/'
AUTH0_AUDIENCE = 'https://bar.mydomain.ca'
GLOBAL_PREFIX = 'api'
SHOULD_ENABLE_LEGACY_GAMES_ENDPOINT = 'false'
}
when {
branch 'integration'
}
stages {
stage('install dependencies') {
steps {
echo 'Running staging build'
echo "NODE ENV: ${NODE_ENV}"
echo 'Installing dependencies'
sh 'npm ci --legacy-peer-deps'
}
}
stage('build front-end for COSCRAD sandbox') {
steps {
runFrontendBuild('COSCRAD')
}
post {
success {
deployFrontend('COSCRAD')
}
}
}
stage('build front-end for Haida sandbox') {
steps {
runFrontendBuild('Haida')
}
post {
success {
deployFrontend('Haida')
}
}
}
stage('build back-end') {
steps {
sh 'npx nx run build api --prod'
}
post {
success {
deployBackend()
}
}
}
stage('build cli') {
steps {
sh 'npx nx run api:build:cli'
}
post {
success {
deployCli()
}
}
}
}
}
}
}
/**
* TODO We might be able to have a single function that switches on `target` and
* sets several global variables to be used in various steps.
*/
String getContentConfigFilename(String target) {
if (target == 'COSCRAD') { return 'content.config.SAMPLE.ts' }
if (target == 'Haida') { return 'content.config.STAGING.ts' }
error "unsupported deployment target: ${target}"
// This is to satisfy the linter. Maybe we should be explicitly throwing in the line above?
return ''
}
/**
* # Available Targets
- COSCRAD
- Haida
**/
void copyContentConfig(String target) {
String contentConfigDirectory = 'apps/coscrad-frontend/src/configurable-front-matter/data/'
/**
* Note that the sample content config is actually valid for
* our staging build.
**/
echo "attempting to copy sample content config for test build for target ${target}"
/* groovylint-disable-next-line LineLength */
sh "cp ${contentConfigDirectory}${getContentConfigFilename(target)} ${contentConfigDirectory}content.config.ts"
return
}
void runFrontendBuild(String target) {
configFileProvider([configFile(fileId:'staging.auth.config', \
targetLocation: 'apps/coscrad-frontend/src/auth_config.json')]) {
echo "Building COSCRAD front-end for target project: ${target}"
echo 'with node version'
sh 'node --version'
copyContentConfig(target)
sh 'npx nx build coscrad-frontend --prod --skip-nx-cache'
echo 'build contents'
sh 'ls dist/apps'
}
}
String getDeploymentDirectoryForFrontendBuild(String target) {
if (target == 'COSCRAD') {
return 'html'
}
if (target == 'Haida') {
return 'haida'
}
error "No deployment directory found for unknown build target: ${target}"
return ''
}
void publishArtifacts(String sshConfigName, \
String postTransferCommand, \
String remoteDirectory, \
String sourceFilePattern) {
sshPublisher(
publishers: [
sshPublisherDesc(
configName: sshConfigName,
transfers: [
sshTransfer(
cleanRemote: false,
excludes: '',
execCommand: postTransferCommand,
execTimeout: 120000,
flatten: false, makeEmptyDirs:
false, noDefaultExcludes: false,
patternSeparator: '[, ]+',
remoteDirectory: remoteDirectory,
remoteDirectorySDF: false,
removePrefix: '',
sourceFiles: sourceFilePattern
)],
usePromotionTimestamp: false,
useWorkspaceInPromotion: false,
verbose: false
)])
}
void archiveArtifacts(String sourceFilePattern) {
archiveArtifacts artifacts: sourceFilePattern, followSymlinks: false
}
void deployFrontend(String target) {
String basePath = '/var/www/'
String deploymentDirectory = getDeploymentDirectoryForFrontendBuild(target)
String fullDeploymentPath = "${basePath}${deploymentDirectory}"
String command = "rm -rf ${fullDeploymentPath} \
&& mv build/dist/apps/coscrad-frontend \
${fullDeploymentPath} && rm -rf build "
String sourceFilePattern = 'dist/apps/coscrad-frontend/**'
archiveArtifacts(sourceFilePattern)
publishArtifacts('[email protected]', command, 'build', sourceFilePattern)
}
void deployBackend() {
String backendArtifactsPattern = 'dist/apps/api/**, node_modules/**'
String postDeployCommand = 'rm -rf archive ; \
mv build archive; \
touch archive/dist/apps/api/staging.env; \
PATH=$PATH://home/coscradmin/.nvm/versions/node/v18.16.0/bin pm2 restart main; \
echo API restarted'
archiveArtifacts(backendArtifactsPattern)
publishArtifacts('[email protected]',
postDeployCommand, \
'build', \
backendArtifactsPattern)
}
void deployCli() {
String backendArtifactsPattern = 'dist/apps/coscrad-cli/**'
String postDeployCommand = 'echo CLI build copied'
archiveArtifacts(backendArtifactsPattern)
publishArtifacts('[email protected]',
postDeployCommand, \
'cli', \
backendArtifactsPattern)
}