-
Notifications
You must be signed in to change notification settings - Fork 53
/
Jenkinsfile
257 lines (257 loc) · 10.7 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
pipeline {
agent any
options {
// Running builds concurrently could cause a race condition with
// building the Docker image.
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '5'))
ansiColor('xterm')
}
environment {
// Some branches have a "/" in their name (e.g. feature/new-and-cool)
// Some commands, such as those that deal with directories, don't
// play nice with this naming convention. Define an alias for the
// branch name that can be used in these scenarios.
BRANCH_ALIAS = sh(
script: 'echo $BRANCH_NAME | sed -e "s#/#-#g"',
returnStdout: true
).trim()
DOCKER_BUILDKIT=1
//spawns GITHUB_USR and GITHUB_PSW environment variables
GITHUB_API_KEY=credentials('38b2e4a6-167a-40b2-be6f-d69be42c8190')
GITHUB_CLIENT_ID=credentials('380f58b1-8a33-4a9d-a67b-354a9b0e792e')
GITHUB_CLIENT_SECRET=credentials('71626c21-de59-4450-bfad-5034fd596fb2')
GOOGLE_STT_KEY=credentials('287949f8-2ada-4450-8806-1fe2dd8e4c4d')
STRIPE_KEY=credentials('9980e41f-d418-49af-9d62-341d1246f555')
WOLFRAM_ALPHA_KEY=credentials('f718e0a1-c19c-4c7f-af88-0689738ccaa1')
}
stages {
stage('Lint & Format') {
// Run PyLint and Black to check code quality.
when {
anyOf {
changeRequest target: 'dev'
changeRequest target: 'master'
}
}
steps {
labelledShell label: 'Account API Setup', script: """
docker build \
--build-arg github_api_key=${GITHUB_API_KEY} \
--build-arg api_name=account \
--target api-code-check --no-cache \
-t selene-linter:${BRANCH_ALIAS} .
"""
labelledShell label: 'Account API Check', script: """
docker run selene-linter:${BRANCH_ALIAS} --poetry-dir api/account --pull-request=${BRANCH_NAME}
"""
labelledShell label: 'Single Sign On API Setup', script: """
docker build \
--build-arg github_api_key=${GITHUB_API_KEY} \
--build-arg api_name=sso \
--target api-code-check --no-cache \
-t selene-linter:${BRANCH_ALIAS} .
"""
labelledShell label: 'Single Sign On API Check', script: """
docker run selene-linter:${BRANCH_ALIAS} --poetry-dir api/sso --pull-request=${BRANCH_NAME}
"""
labelledShell label: 'Public API Setup', script: """
docker build \
--build-arg github_api_key=${GITHUB_API_KEY} \
--build-arg api_name=public \
--target api-code-check --no-cache \
--label job=${JOB_NAME} \
-t selene-linter:${BRANCH_ALIAS} .
"""
labelledShell label: 'Public API Check', script: """
docker run selene-linter:${BRANCH_ALIAS} --poetry-dir api/public --pull-request=${BRANCH_NAME}
"""
}
}
stage('Bootstrap DB') {
when {
anyOf {
branch 'dev'
branch 'master'
changeRequest target: 'dev'
changeRequest target: 'master'
}
}
steps {
labelledShell label: 'Building Docker image', script: """
docker build \
--target db-bootstrap \
--build-arg github_api_key=${GITHUB_API_KEY} \
--label job=${JOB_NAME} \
-t selene-db:${BRANCH_ALIAS} .
"""
timeout(time: 5, unit: 'MINUTES')
{
labelledShell label: 'Run database bootstrap script', script: """
docker run \
-v '${HOME}/selene:/tmp/selene' \
--net selene-net selene-db:${BRANCH_ALIAS}
"""
}
}
}
stage('Account API Tests') {
when {
anyOf {
branch 'dev'
branch 'master'
changeRequest target: 'dev'
changeRequest target: 'master'
}
}
steps {
labelledShell label: 'Building Docker image', script: """
docker build \
--build-arg stripe_api_key=${STRIPE_KEY} \
--target account-api-test \
--label job=${JOB_NAME} \
-t selene-account:${BRANCH_ALIAS} .
"""
timeout(time: 5, unit: 'MINUTES')
{
sh 'mkdir -p $HOME/selene/$BRANCH_ALIAS/allure'
labelledShell label: 'Running behave tests', script: """
docker run \
--net selene-net \
-v '$HOME/selene/$BRANCH_ALIAS/allure/:/root/allure' \
--label job=${JOB_NAME} \
selene-account:${BRANCH_ALIAS}
"""
}
}
post {
always {
sh 'docker run \
-v "$HOME/selene/$BRANCH_ALIAS/allure:/root/allure" \
--entrypoint=/bin/bash \
--label build=${JOB_NAME} \
selene-account:${BRANCH_ALIAS} \
-x -c "chown $(id -u $USER):$(id -g $USER) \
-R /root/allure/"'
}
}
}
stage('Single Sign On API Tests') {
when {
anyOf {
branch 'dev'
branch 'master'
changeRequest target: 'dev'
changeRequest target: 'master'
}
}
steps {
labelledShell label: 'Building Docker image', script: """
docker build \
--build-arg github_client_id=${GITHUB_CLIENT_ID} \
--build-arg github_client_secret=${GITHUB_CLIENT_SECRET} \
--target sso-api-test \
--label job=${JOB_NAME} \
-t selene-sso:${BRANCH_ALIAS} .
"""
timeout(time: 2, unit: 'MINUTES')
{
labelledShell label: 'Running behave tests', script: """
docker run \
--net selene-net \
-v '$HOME/selene/$BRANCH_ALIAS/allure/:/root/allure' \
selene-sso:${BRANCH_ALIAS}
"""
}
}
post {
always {
sh 'docker run \
-v "$HOME/selene/$BRANCH_ALIAS/allure:/root/allure" \
--entrypoint=/bin/bash \
--label build=${JOB_NAME} \
selene-sso:${BRANCH_ALIAS} \
-x -c "chown $(id -u $USER):$(id -g $USER) \
-R /root/allure/"'
}
}
}
stage('Public Device API Tests') {
when {
anyOf {
branch 'dev'
branch 'master'
changeRequest target: 'dev'
changeRequest target: 'master'
}
}
steps {
labelledShell label: 'Building Docker image', script: """
docker build \
--build-arg wolfram_alpha_key=${WOLFRAM_ALPHA_KEY} \
--build-arg google_stt_key=${GOOGLE_STT_KEY} \
--target public-api-test \
--label job=${JOB_NAME} \
-t selene-public:${BRANCH_ALIAS} .
"""
timeout(time: 2, unit: 'MINUTES')
{
labelledShell label: 'Running behave tests', script: """
docker run \
--net selene-net \
-v '$HOME/selene/$BRANCH_ALIAS/allure/:/root/allure' \
-v '$HOME/selene/secrets/:/root/secrets' \
selene-public:${BRANCH_ALIAS}
"""
}
}
post {
always {
sh 'docker run \
-v "$HOME/selene/$BRANCH_ALIAS/allure:/root/allure" \
--entrypoint=/bin/bash \
--label build=${JOB_NAME} \
selene-account:${BRANCH_ALIAS} \
-x -c "chown $(id -u $USER):$(id -g $USER) \
-R /root/allure/"'
}
}
}
}
post {
always {
sh 'rm -rf allure-result/*'
sh 'mkdir -p $HOME/selene/$BRANCH_ALIAS/allure/allure-result'
sh 'mv $HOME/selene/$BRANCH_ALIAS/allure/allure-result allure-result'
// This directory should now be empty, rmdir will intentionally fail if not.
sh 'rmdir $HOME/selene/$BRANCH_ALIAS/allure'
script {
allure([
includeProperties: false,
jdk: '',
properties: [],
reportBuildPolicy: 'ALWAYS',
results: [[path: 'allure-result']]
])
}
sh(
label: 'Cleanup lingering docker containers and images.',
script: """
docker container prune --force;
docker image prune --force;
"""
)
}
success {
// Docker images should remain upon failure for troubleshooting purposes. However,
// if the stage is successful, there is no reason to look back at the Docker image. In theory
// broken builds will eventually be fixed so this step should run eventually for every PR
sh(
label: 'Delete Docker Image on Success',
script: '''
docker image prune --all --force --filter label=job=${JOB_NAME};
'''
)
}
}
}