forked from petbattle/pet-battle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
379 lines (337 loc) Β· 16.2 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
pipeline {
agent {
label "master"
}
environment {
// GLobal Vars
// PIPELINES_NAMESPACE = "${OPENSHIFT_BUILD_NAMESPACE}"
NAME = "pet-battle"
ARGOCD_CONFIG_REPO = "github.com/springdo/ubiquitous-journey.git"
ARGOCD_CONFIG_REPO_PATH = "example-deployment/values-applications.yaml"
ARGOCD_CONFIG_REPO_BRANCH = "ds-env"
// Job name contains the branch eg ds-app-feature%2Fjenkins-123
JOB_NAME = "${JOB_NAME}".replace("%2F", "-").replace("/", "-")
GIT_SSL_NO_VERIFY = true
// Credentials bound in OpenShift
GIT_CREDS = credentials("${OPENSHIFT_BUILD_NAMESPACE}-git-auth")
NEXUS_CREDS = credentials("${OPENSHIFT_BUILD_NAMESPACE}-nexus-password")
ARGOCD_CREDS = credentials("${OPENSHIFT_BUILD_NAMESPACE}-argocd-token")
// Nexus Artifact repo
NEXUS_REPO_NAME="labs-static"
NEXUS_REPO_HELM = "helm-charts"
}
// The options directive is for configuration that applies to the whole job.
options {
buildDiscarder(logRotator(numToKeepStr: '50', artifactNumToKeepStr: '1'))
timeout(time: 15, unit: 'MINUTES')
ansiColor('xterm')
timestamps()
}
stages {
stage('Perpare Environment') {
failFast true
parallel {
stage("Release Build") {
options {
skipDefaultCheckout(true)
}
agent {
node {
label "master"
}
}
when {
expression { GIT_BRANCH.startsWith("master") }
}
steps {
script {
env.TARGET_NAMESPACE = "springdo"
// External image push registry info
env.IMAGE_REPOSITORY = "quay.io"
env.QUAY_PUSH_SECRET = "springdo-petbattlepipeline-secret"
// app name for master is just pet-battle or something
env.APP_NAME = "${NAME}".replace("/", "-").toLowerCase()
}
}
}
stage("Sandbox Build") {
options {
skipDefaultCheckout(true)
}
agent {
node {
label "master"
}
}
when {
expression { GIT_BRANCH.startsWith("dev") || GIT_BRANCH.startsWith("feature") || GIT_BRANCH.startsWith("fix") }
}
steps {
script {
env.TARGET_NAMESPACE = "ds-dev"
// Sandbox registry deets
env.IMAGE_REPOSITORY = 'image-registry.openshift-image-registry.svc:5000'
// ammend the name to create 'sandbox' deploys based on current branch
env.APP_NAME = "${GIT_BRANCH}-${NAME}".replace("/", "-").toLowerCase()
env.NODE_ENV = "test"
}
}
}
stage("Pull Request Build") {
options {
skipDefaultCheckout(true)
}
agent {
node {
label "master"
}
}
when {
expression { GIT_BRANCH.startsWith("PR-") }
}
steps {
script {
env.TARGET_NAMESPACE = "ds-dev"
env.IMAGE_REPOSITORY = 'image-registry.openshift-image-registry.svc:5000'
env.APP_NAME = "${GIT_BRANCH}-${NAME}".replace("/", "-").toLowerCase()
}
}
}
}
}
stage("Build (Compile App)") {
agent {
node {
label "jenkins-slave-npm"
}
}
steps {
script {
env.VERSION = sh(returnStdout: true, script: "npm run version --silent").trim()
env.PACKAGE = "${APP_NAME}-${VERSION}.tar.gz"
}
sh 'printenv'
echo '### Install deps ###'
// sh 'npm install'
sh 'npm --registry http://${SONATYPE_NEXUS_SERVICE_SERVICE_HOST}:${SONATYPE_NEXUS_SERVICE_SERVICE_PORT}/repository/labs-npm ci'
echo '### Running linter ###'
// sh 'npm run lint'
// helm lint? probs not here as no helm in jenkins slave
echo '### Running tests ###'
// sh 'npm run test'
echo '### Running build ###'
sh '''
npm run build
'''
echo '### Packaging App for Nexus ###'
sh '''
tar -zcvf ${PACKAGE} dist Dockerfile nginx.conf
curl -v -f -u ${NEXUS_CREDS} --upload-file ${PACKAGE} http://${SONATYPE_NEXUS_SERVICE_SERVICE_HOST}:${SONATYPE_NEXUS_SERVICE_SERVICE_PORT}/repository/${NEXUS_REPO_NAME}/${APP_NAME}/${PACKAGE}
'''
}
// Post can be used both on individual stages and for the entire build.
}
stage("Bake (OpenShift Build)") {
options {
skipDefaultCheckout(true)
}
agent {
node {
label "master"
}
}
steps {
sh 'printenv'
echo '### Get Binary from Nexus and shove it in a box ###'
sh '''
rm -rf package-contents*
curl -v -f -u ${NEXUS_CREDS} http://${SONATYPE_NEXUS_SERVICE_SERVICE_HOST}:${SONATYPE_NEXUS_SERVICE_SERVICE_PORT}/repository/${NEXUS_REPO_NAME}/${APP_NAME}/${PACKAGE} -o ${PACKAGE}
BUILD_ARGS=" --build-arg git_commit=${GIT_COMMIT} --build-arg git_url=${GIT_URL} --build-arg build_url=${RUN_DISPLAY_URL} --build-arg build_tag=${BUILD_TAG}"
echo ${BUILD_ARGS}
# oc get bc ${APP_NAME} || rc=$?
# dirty hack so i don't have to oc patch the bc for the new version when pushing to quay ...
oc delete bc ${APP_NAME} || rc=$?
if [[ $TARGET_NAMESPACE == *"dev"* ]]; then
echo "π Creating a sandbox build for inside the cluster π"
oc new-build --binary --name=${APP_NAME} -l app=${APP_NAME} ${BUILD_ARGS} --strategy=docker
oc start-build ${APP_NAME} --from-archive=${PACKAGE} ${BUILD_ARGS} --follow
# used for internal sandbox build ....
oc tag ${OPENSHIFT_BUILD_NAMESPACE}/${APP_NAME}:latest ${TARGET_NAMESPACE}/${APP_NAME}:${VERSION}
else
echo "π Creating a potential build that could go all the way so pushing externally π"
oc new-build --binary --name=${APP_NAME} -l app=${APP_NAME} ${BUILD_ARGS} --strategy=docker --push-secret=${QUAY_PUSH_SECRET} --to-docker --to="${IMAGE_REPOSITORY}/${TARGET_NAMESPACE}/${APP_NAME}:${VERSION}"
oc start-build ${APP_NAME} --from-archive=${PACKAGE} ${BUILD_ARGS} --follow
fi
'''
}
}
stage("Helm Package App (master)") {
agent {
node {
label "jenkins-slave-helm"
}
}
steps {
sh 'printenv'
sh '''
helm lint chart
'''
sh '''
# might be overkill...
yq w -i chart/Chart.yaml 'appVersion' ${VERSION}
yq w -i chart/Chart.yaml 'version' ${VERSION}
yq w -i chart/Chart.yaml 'name' ${APP_NAME}
# probs point to the image inside ocp cluster or perhaps an external repo?
yq w -i chart/values.yaml 'image_repository' ${IMAGE_REPOSITORY}
yq w -i chart/values.yaml 'image_name' ${APP_NAME}
yq w -i chart/values.yaml 'image_namespace' ${TARGET_NAMESPACE}
# latest built image
yq w -i chart/values.yaml 'app_tag' ${VERSION}
'''
sh '''
# package and release helm chart?
helm package chart/ --app-version ${VERSION} --version ${VERSION}
curl -v -f -u ${NEXUS_CREDS} http://${SONATYPE_NEXUS_SERVICE_SERVICE_HOST}:${SONATYPE_NEXUS_SERVICE_SERVICE_PORT}/repository/${NEXUS_REPO_HELM}/ --upload-file ${APP_NAME}-${VERSION}.tgz
'''
}
}
stage("Deploy App") {
failFast true
parallel {
stage("sandbox - helm3 publish and install"){
options {
skipDefaultCheckout(true)
}
agent {
node {
label "jenkins-slave-helm"
}
}
when {
expression { GIT_BRANCH.startsWith("dev") || GIT_BRANCH.startsWith("feature") || GIT_BRANCH.startsWith("fix") }
}
steps {
// TODO - if SANDBOX, create release in rando ns
sh '''
helm upgrade --install ${APP_NAME} \
--namespace=${TARGET_NAMESPACE} \
http://${SONATYPE_NEXUS_SERVICE_SERVICE_HOST}:${SONATYPE_NEXUS_SERVICE_SERVICE_PORT}/repository/${NEXUS_REPO_HELM}/${APP_NAME}-${VERSION}.tgz
'''
}
}
stage("test env - ArgoCD sync") {
options {
skipDefaultCheckout(true)
}
agent {
node {
label "jenkins-slave-argocd"
}
}
when {
expression { GIT_BRANCH ==~ /(.*master)/ }
}
steps {
echo '### Commit new image tag to git ###'
sh '''
# TODO ARGOCD create app?
# TODO - fix all this after chat with @eformat
git clone https://${ARGOCD_CONFIG_REPO} config-repo
cd config-repo
git checkout ${ARGOCD_CONFIG_REPO_BRANCH}
# TODO - @eformat we probs need to think about the app of apps approach or better logic here
# as using array[0] is π§»
yq w -i ${ARGOCD_CONFIG_REPO_PATH} "applications.name==test-${NAME}.source_ref" ${VERSION}
git config --global user.email "[email protected]"
git config --global user.name "Jenkins"
git config --global push.default simple
git add ${ARGOCD_CONFIG_REPO_PATH}
git commit -m "π AUTOMATED COMMIT - Deployment new app version ${VERSION} π" || rc=$?
git remote set-url origin https://${GIT_CREDS_USR}:${GIT_CREDS_PSW}@${ARGOCD_CONFIG_REPO}
git push -u origin ${ARGOCD_CONFIG_REPO_BRANCH}
'''
echo '### Ask ArgoCD to Sync the changes and roll it out ###'
sh '''
# 1. Check if app of apps exists, if not create?
# 1.1 Check sync not currently in progress . if so, kill it
# 2. sync argocd to change pushed in previous step
ARGOCD_INFO="--auth-token ${ARGOCD_CREDS_PSW} --server ${ARGOCD_SERVER_SERVICE_HOST}:${ARGOCD_SERVER_SERVICE_PORT_HTTP} --insecure"
argocd app sync catz ${ARGOCD_INFO}
argocd app wait catz ${ARGOCD_INFO}
# todo sync child app
# argocd app sync test-${NAME} ${ARGOCD_INFO}
# argocd app wait test-${NAME} ${ARGOCD_INFO}
'''
}
}
}
}
stage("End to End Test") {
agent {
node {
label "master"
}
}
when {
expression { GIT_BRANCH ==~ /(.*master)/ }
}
steps {
sh '''
echo "TODO - Run tests"
'''
}
}
stage("Promote app to Staging") {
agent {
node {
label "jenkins-slave-argocd"
}
}
when {
expression { GIT_BRANCH ==~ /(.*master)/ }
}
steps {
sh '''
# TODO ARGOCD create app?
# TODO - fix all this after chat with @eformat
git clone https://${ARGOCD_CONFIG_REPO} config-repo
cd config-repo
git checkout ${ARGOCD_CONFIG_REPO_BRANCH}
# TODO - @eformat we probs need to think about the app of apps approach or better logic here
# as using array[0] is π§»
yq w -i ${ARGOCD_CONFIG_REPO_PATH} "applications.name==${APP_NAME}.source_ref" ${VERSION}
git config --global user.email "[email protected]"
git config --global user.name "Jenkins"
git config --global push.default simple
git add ${ARGOCD_CONFIG_REPO_PATH}
# grabbing the error code incase there is nothing to commit and allow jenkins proceed
git commit -m "π AUTOMATED COMMIT - Deployment new app version ${VERSION} π" || rc=$?
git remote set-url origin https://${GIT_CREDS_USR}:${GIT_CREDS_PSW}@${ARGOCD_CONFIG_REPO}
git push -u origin ${ARGOCD_CONFIG_REPO_BRANCH}
'''
echo '### Ask ArgoCD to Sync the changes and roll it out ###'
sh '''
# 1. Check if app of apps exists, if not create?
# 1.1 Check sync not currently in progress . if so, kill it
# 2. sync argocd to change pushed in previous step
ARGOCD_INFO="--auth-token ${ARGOCD_CREDS_PSW} --server ${ARGOCD_SERVER_SERVICE_HOST}:${ARGOCD_SERVER_SERVICE_PORT_HTTP} --insecure"
argocd app sync catz ${ARGOCD_INFO}
argocd app wait catz ${ARGOCD_INFO}
# todo sync child app
# argocd app sync ${NAME} ${ARGOCD_INFO}
# argocd app wait ${NAME} ${ARGOCD_INFO}
'''
sh '''
echo "merge versions back to the original GIT repo as they should be persisted?"
git checkout ${GIT_BRANCH}
yq w -i chart/Chart.yaml 'appVersion' ${VERSION}
yq w -i chart/Chart.yaml 'version' ${VERSION}
git add chart/Chart.yaml
git commit -m "π AUTOMATED COMMIT - Deployment of new app version ${VERSION} π" || rc=$?
git remote set-url origin https://${GIT_CREDS_USR}:${GIT_CREDS_PSW}@github.com/springdo/pet-battle.git
git push
'''
}
}
}
}