-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathJenkinsfile
More file actions
323 lines (286 loc) · 10.2 KB
/
Jenkinsfile
File metadata and controls
323 lines (286 loc) · 10.2 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import org.kohsuke.github.GitHub
import groovy.transform.Field
@Field
def ref, shortRef, prNum
@Field
def envNames = ['dev', 'staging', 'production']
@Field
def review_s3_bucket_name = 'review-developer-va-gov'
def devBranch = 'master'
def stagingBranch = 'master'
def prodBranch = 'master'
env.CONCURRENCY = 10
def onDeployableBranch = {
(env.BRANCH_NAME == devBranch ||
env.BRANCH_NAME == stagingBranch ||
env.BRANCH_NAME == prodBranch)
}
def supercededByConcurrentBuild = {
// abort the job if we're not on deployable branch (usually master) and there's a newer build going now
(!onDeployableBranch() &&
!env.CHANGE_TARGET &&
currentBuild.nextBuild)
}
def buildDetails = { vars ->
"""
BUILDTYPE=${vars['buildtype']}
NODE_ENV=production
BRANCH_NAME=${env.BRANCH_NAME}
CHANGE_TARGET=${env.CHANGE_TARGET}
BUILD_ID=${env.BUILD_ID}
BUILD_NUMBER=${env.BUILD_NUMBER}
REF=${vars['ref']}
""".stripIndent()
}
def notify = { ->
if (onDeployableBranch()) {
message = "developer-portal ${env.BRANCH_NAME} branch CI failed. |${env.RUN_DISPLAY_URL}".stripMargin()
slackSend message: message,
color: 'danger',
failOnError: true
}
}
// Post a comment on the current pull request
def pullRequestComment(String comment) {
withEnv(["comment=${comment}", "prNum=${prNum}"]) {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'va-bot', usernameVariable: 'USERNAME', passwordVariable: 'TOKEN']]) {
sh '''
curl -u "${USERNAME}:${TOKEN}" "https://api.github.com/repos/department-of-veterans-affairs/developer-portal/issues/${prNum}/comments" --data "{\\"body\\":\\"${comment}\\"}"
'''
}
}
}
def getPullRequestNumber() {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'va-bot', usernameVariable: 'USERNAME', passwordVariable: 'TOKEN']]) {
return sh(script: '''
# URL decode branch name
branch=$(python -c 'import sys, urllib; print urllib.unquote(sys.argv[1])' ${JOB_BASE_NAME})
# Get PR number from branch name. May fail if there are multiple PRs from the same branch
curl -u "${USERNAME}:${TOKEN}" "https://api.github.com/repos/department-of-veterans-affairs/developer-portal/pulls" | jq ".[] | select(.head.ref==\\"${branch}\\") | .number"
''', returnStdout: true).trim()
}
}
def commentAfterDeploy() {
def linksSnippet = envNames.collect{ envName ->
"https://s3-us-gov-west-1.amazonaws.com/${reviewBucketPath()}/${envName}/index.html"
}.join(" <br> ")
pullRequestComment(
"These changes have been deployed to an S3 bucket. A build for each environment is available: <br><br> ${linksSnippet} <br><br> Due to S3 website hosting limitations in govcloud you need to first navigate to index.html explicitly."
)
}
def reviewBucketPath() {
return "${review_s3_bucket_name}/${shortRef}"
}
node('vetsgov-general-purpose') {
properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', daysToKeepStr: '60']]]);
def dockerImage, args, imageTag
// Checkout source, create output directories, build container
stage('Setup') {
try {
prNum = getPullRequestNumber()
echo("The pull request number captured from the github API: ${prNum}")
deleteDir()
checkout([
$class: 'GitSCM',
branches: [[name: "origin/${env.BRANCH_NAME}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'CleanBeforeCheckout'],
[$class: 'CleanCheckout'],
[$class: 'GitLFSPull']
],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: 'va-vfs-bot',
url: 'https://github.com/department-of-veterans-affairs/developer-portal'
]]
])
ref = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
shortRef = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
if (prNum) {
envNames.each{ envName ->
// Set public url so review deploys handle not being at the site root
// Set sentry DSN to send review deploy errors to the review sentry project
writeFile(
file: "./.env.${envName}.local",
text: """\
PUBLIC_URL=/${reviewBucketPath()}/${envName}
REACT_APP_SENTRY_DSN=https://dc7d5ebec20e474c80f8150c399d2955@dev-developer.va.gov/sentry/26
""".stripIndent()
)
}
}
sh "mkdir -p build"
imageTag = java.net.URLDecoder.decode(env.BUILD_TAG).replaceAll("[^A-Za-z0-9\\-\\_]", "-")
dockerImage = docker.build("developer-portal:${imageTag}")
args = "-v ${pwd()}:/application -v /application/node_modules"
} catch (error) {
notify()
throw error
}
}
stage('Image Prohibition') {
if (!onDeployableBranch()) {
sh "./prohibit_image_files.sh origin/master HEAD"
}
}
stage('Security') {
try {
dockerImage.inside(args) {
sh "cd /application && npm config set audit-level critical && npm audit"
}
} catch (error) {
if (!onDeployableBranch()) {
// Only PR branches should be blocked by npm audit failures. Packages
// that receive new vulnerability notices after master already depends
// on them should be handled separately from our post-merge
// auto-deploys.
throw error
}
}
}
stage('TSLint') {
try {
dockerImage.inside(args) {
sh 'cd /application && npm run-script lint:ci'
}
} catch (error) {
notify()
dir(pwd()) {
step([$class: 'JUnitResultArchiver', testResults: 'lint-results.xml'])
}
throw error
}
}
stage('Unit test') {
try {
dockerImage.inside(args) {
sh 'cd /application && npm run-script test:unit:ci'
}
} catch (error) {
notify()
dir(pwd()) {
step([$class: 'JUnitResultArchiver', testResults: 'test-report.xml'])
}
throw error
}
}
stage('e2e test') {
try {
dockerImage.inside(args) {
sh 'cd /application && npm run-script test:e2e:ci'
}
} catch (error) {
notify()
dir(pwd()) {
step([$class: 'JUnitResultArchiver', testResults: 'test-report.xml'])
}
throw error
}
}
stage('Accessibility Test'){
try {
dockerImage.inside(args) {
sh 'cd /application && npm run-script test:accessibility:ci'
}
} catch (error) {
notify()
dir(pwd()) {
step([$class: 'JUnitResultArchiver', testResults: 'test-report.xml'])
}
throw error
}
}
stage('Visual Regression Test') {
if (supercededByConcurrentBuild()) { return }
try {
dockerImage.inside(args) {
sh 'cd /application && npm run test:visual'
}
} catch (error) {
dir('test/image_snapshots/diff_output') {
withEnv(["ref=${ref}",'bucket=developer-portal-screenshots']) {
// Upload diffs to S3
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'vetsgov-website-builds-s3-upload', usernameVariable: 'AWS_ACCESS_KEY', passwordVariable: 'AWS_SECRET_KEY']]) {
sh 'aws --region us-gov-west-1 s3 sync --no-progress . "s3://${bucket}/${ref}/"'
}
// Create github comment
files = sh(script: 'ls', returnStdout: true).tokenize()
links = files.collect {
"[${it - 'visual-regression-test-ts-visual-regression-test-'}](https://s3-us-gov-west-1.amazonaws.com/${bucket}/${ref}/${it})"
}.join(' <br>')
docsLink = 'https://github.com/department-of-veterans-affairs/developer-portal/blob/master/docs/testing.md#visual-regression-testing'
comment = "Visual regression testing failed. Review these diffs and then [update the snapshots](${docsLink}). <br><br> ${links}"
pullRequestComment(comment)
}
}
notify()
throw error
}
}
// Perform a build for each build type
stage('Build') {
if (supercededByConcurrentBuild()) { return }
try {
def builds = [:]
envNames.each{ envName ->
builds[envName] = {
dockerImage.inside(args) {
sh "cd /application && NODE_ENV=production BUILD_ENV=${envName} npm run-script build ${envName}"
sh "cd /application && echo \"${buildDetails('buildtype': envName, 'ref': ref)}\" > build/${envName}/BUILD.txt"
}
}
}
parallel builds
} catch (error) {
notify()
// For content team PRs, add comment in GH so they don't need direct Jenkins access to find broken links
throw error
}
}
stage('Archive') {
if (supercededByConcurrentBuild()) { return }
if (!onDeployableBranch()) { return }
// This could only happen in the rare case that a PR is opened from master -> another branch,
// which is unlikely, but potentially disastrous.
if (prNum) { return }
try {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'vetsgov-website-builds-s3-upload',
usernameVariable: 'AWS_ACCESS_KEY', passwordVariable: 'AWS_SECRET_KEY']]) {
envNames.each{ envName ->
sh "tar -C build/${envName} -cf build/${envName}.tar.bz2 ."
sh "aws --region us-gov-west-1 s3 cp --no-progress --acl public-read build/${envName}.tar.bz2 s3://developer-portal-builds-s3-upload/${ref}/${envName}.tar.bz2"
}
}
} catch (error) {
notify()
throw error
}
}
stage('Deploy') {
if (supercededByConcurrentBuild()) { return }
try {
if (prNum) {
// Deploy to review bucket
sh "aws --region us-gov-west-1 s3 sync --no-progress --acl public-read ./build/ s3://${reviewBucketPath()}/"
commentAfterDeploy()
} else {
if (env.BRANCH_NAME == devBranch) {
build job: 'deploys/developer-portal-dev', parameters: [
booleanParam(name: 'notify_slack', value: true),
stringParam(name: 'ref', value: ref),
], wait: false
}
if (env.BRANCH_NAME == stagingBranch) {
build job: 'deploys/developer-portal-staging', parameters: [
booleanParam(name: 'notify_slack', value: true),
stringParam(name: 'ref', value: ref),
], wait: false
}
}
} catch (error) {
notify()
throw error
}
}
}