forked from cfpb/consumerfinance.gov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
149 lines (138 loc) · 4.78 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
// Jenkins multibranch pipeline geared towards building
// and deploying cf.gov production-like Docker stack.
//
// This pipeline uses Jenkins Shared Libraries for several
// pipeline steps. For details on how those work, see
// GHE repo: app-ops/app-ops-jenkins-shared-libraries
pipeline {
agent {
label 'docker'
}
environment {
IMAGE_REPO = 'cfpb/cfgov-python'
IMAGE_TAG = "${JOB_BASE_NAME}-${BUILD_NUMBER}"
STACK_PREFIX = 'cfgov'
NOTIFICATION_CHANNEL = 'cfgov-deployments'
}
parameters {
booleanParam(
name: 'DEPLOY',
defaultValue: true,
description: 'Deploy the stack?'
)
booleanParam(
name: 'REFRESH_DB',
defaultValue: false,
description: 'Refresh the database?'
)
}
options {
ansiColor('xterm')
parallelsAlwaysFailFast()
disableConcurrentBuilds()
timestamps()
}
stages {
stage('Init') {
steps {
script {
env.STACK_NAME = dockerStack.sanitizeStackName("${env.STACK_PREFIX}-${JOB_BASE_NAME}")
env.CFGOV_HOSTNAME = dockerStack.getHostingDomain(env.STACK_NAME)
env.IMAGE_NAME_LOCAL = "${env.IMAGE_REPO}:${env.IMAGE_TAG}"
}
sh 'env | sort'
}
}
stage('Checkout') {
steps {
dir('static.in/cfgov-fonts') {
script {
git ghe.getRepoUrl('CFGOV/cfgov-fonts')
}
}
}
}
stage('Build Image') {
environment {
DOCKER_BUILDKIT = '1'
}
steps {
script {
docker.build(env.IMAGE_NAME_LOCAL, '--build-arg scl_python_version=rh-python36 --target cfgov-prod .')
}
}
}
stage('Scan Image') {
steps {
scanImage(env.IMAGE_REPO, env.IMAGE_TAG)
}
}
stage('Push Image') {
// Push image only on main branch or deploy is set to true
when {
anyOf {
branch 'main'
expression { return params.DEPLOY }
}
}
steps {
script {
docker.withRegistry(dockerRegistry.url, dockerRegistry.credentialsId) {
image = docker.image(env.IMAGE_NAME_LOCAL)
image.push()
// Sets fully-qualified image name
env.CFGOV_PYTHON_IMAGE = image.imageName()
}
}
}
}
stage('Deploy Stack') {
// Deploys only on main branch or deploy is set to true
when {
anyOf {
branch 'main'
expression { return params.DEPLOY }
}
}
steps {
script {
timeout(time: 15, unit: 'MINUTES') {
dockerStack.deploy(env.STACK_NAME, 'docker-stack.yml')
}
}
echo "Site available at: https://${CFGOV_HOSTNAME}"
}
}
stage('Run Functional Tests') {
when {
anyOf {
branch 'main'
expression { return params.DEPLOY }
}
}
steps {
script {
timeout(time: 15, unit: 'MINUTES') {
// sh "docker-compose -f docker-compose.e2e.yml run e2e -e CYPRESS_baseUrl=https://${CFGOV_HOSTNAME}"
sh "docker run -v ${WORKSPACE}/test/cypress:/app/test/cypress -v ${WORKSPACE}/cypress.json:/app/cypress.json -w /app -e CYPRESS_baseUrl=https://${CFGOV_HOSTNAME} -e CI=1 cypress/included:4.10.0 npx cypress run -b chrome --headless"
}
}
}
}
}
post {
success {
script {
if (env.GIT_BRANCH != 'main') {
notify("${NOTIFICATION_CHANNEL}", ":white_check_mark: Branch $env.GIT_BRANCH PR $env.CHANGE_URL deployed by $env.CHANGE_AUTHOR_EMAIL via $env.BUILD_URL and available at https://$env.CFGOV_HOSTNAME.")
}
else {
notify("${NOTIFICATION_CHANNEL}", ":white_check_mark: Branch $env.GIT_BRANCH deployed via $env.BUILD_URL and available at https://$env.CFGOV_HOSTNAME.")
}
}
}
unsuccessful {
notify("${NOTIFICATION_CHANNEL}", ":x: PR ${env.CHANGE_URL} by ${env.CHANGE_AUTHOR} failed. See: ${env.BUILD_URL}.")
}
}
}