-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
286 lines (262 loc) Β· 11.8 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
pipeline {
agent {
label "master"
}
environment {
// GLobal Vars
NAME = "learning-experience-platform"
// Config repo managed by ArgoCD details
ARGOCD_CONFIG_REPO = "github.com/who-lxp/lxp-config.git"
ARGOCD_CONFIG_REPO_PATH = "lxp-deployment/values-test.yaml"
ARGOCD_CONFIG_REPO_BRANCH = "master"
// 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")
QUAY_PUSH_SECRET = "who-lxp-imagepusher-secret"
// Nexus Artifact repos
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')
}
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 {
// External image push registry info
env.TARGET_NAMESPACE = "who-lxp"
env.IMAGE_REPOSITORY = "quay.io"
// app name for master is just learning-experience-platform 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 = "labs-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()
}
}
}
}
}
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'
echo '### Running tests ###'
sh 'npm run test:ci'
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.
post {
always {
// archiveArtifacts "**"
junit 'reports/unit/junit.xml'
// publish html
publishHTML target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'reports/coverage/lcov-report',
reportFiles: 'index.html',
reportName: 'FE Code Coverage'
]
}
}
}
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}
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 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 || rc=$?
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") {
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} # APP= feature-123-learning-experience-platform
# 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 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 --force --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 '''
git clone https://${ARGOCD_CONFIG_REPO} config-repo
cd config-repo
git checkout ${ARGOCD_CONFIG_REPO_BRANCH}
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}
'''
}
}
}
}
stage("Trigger System Tests") {
options {
skipDefaultCheckout(true)
}
agent {
node {
label "master"
}
}
when {
expression { GIT_BRANCH ==~ /(.*master)/ }
}
steps {
sh '''
echo "TODO - Run tests"
'''
build job: 'system-tests/master', parameters: [[$class: 'StringParameterValue', name: 'APP_NAME', value: "${APP_NAME}" ],[$class: 'StringParameterValue', name: 'VERSION', value: "${VERSION}"]], wait: false
}
}
}
}