forked from petbattle/pet-battle
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Jenkinsfile
243 lines (214 loc) Β· 9.01 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
pipeline {
agent {
label "master"
}
environment {
// GLobal Vars shared across all jenkins agents
// Job name contains the branch eg pet-battle-feature%2Fjenkins-123
// ensure the name is k8s compliant
// JOB_NAME = "${JOB_NAME}".replace("%2F", "-").replace("/", "-")
// NAME = "${JOB_NAME}".split("/")[0]
GIT_SSL_NO_VERIFY = true
// ArgoCD Config Repo
// set this as an ENV_VAR on Jenkins to make this easier?
// ARGOCD_CONFIG_REPO = "github.com/petbattle/ubiquitous-journey.git"
ARGOCD_CONFIG_REPO_PATH = "pet-battle/test/values.yaml"
ARGOCD_CONFIG_REPO_BRANCH = "main"
// Credentials bound in OpenShift
GIT_CREDS = credentials("${OPENSHIFT_BUILD_NAMESPACE}-git-auth")
NEXUS_CREDS = credentials("${OPENSHIFT_BUILD_NAMESPACE}-nexus-password")
// 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')
}
stages {
stage('ποΈ Prepare Environment') {
failFast true
parallel {
stage("π Release Build") {
options {
skipDefaultCheckout(true)
}
agent { label "master" }
when {
expression { GIT_BRANCH.startsWith("master") || GIT_BRANCH.startsWith("main") }
}
steps {
script {
// ensure the name is k8s compliant
env.TEAM_NAME = "${GITLAB_GROUP_NAME}"
env.NAME = "${JOB_NAME}".split("/")[0]
env.APP_NAME = "${NAME}".replace("/", "-").toLowerCase()
env.DESTINATION_NAMESPACE = "${TEAM_NAME}-test"
env.IMAGE_NAMESPACE = "${DESTINATION_NAMESPACE}"
env.IMAGE_REPOSITORY = 'image-registry.openshift-image-registry.svc:5000'
// env.ARGOCD_CONFIG_REPO = "${ARGOCD_CONFIG_REPO}"
env.ARGOCD_CONFIG_REPO = "${GITLAB_HOST}/${GITLAB_GROUP_NAME}/tech-exercise.git"
}
sh 'printenv'
}
}
stage("π Sandbox Build") {
options {
skipDefaultCheckout(true)
}
agent { label "master" }
when {
expression { return !(GIT_BRANCH.startsWith("master") || GIT_BRANCH.startsWith("main") )}
}
steps {
script {
env.TEAM_NAME = "${TEAM_NAME}"
env.DESTINATION_NAMESPACE = "${TEAM_NAME}-dev"
env.IMAGE_NAMESPACE = "${DESTINATION_NAMESPACE}"
env.IMAGE_REPOSITORY = 'image-registry.openshift-image-registry.svc:5000'
// ammend the name to create 'sandbox' deploys based on current branch
env.NAME = "${JOB_NAME}".split("/")[0]
env.APP_NAME = "${GIT_BRANCH}-${NAME}".replace("/", "-").toLowerCase()
env.NODE_ENV = "test"
env.DEV_BUILD = true
}
}
}
}
}
// π₯π¨ PIPELINE EXERCISE GOES HERE
stage("π§ Bake (OpenShift Build)") {
options {
skipDefaultCheckout(true)
}
agent { label "master" }
steps {
sh 'printenv'
echo '### Get Binary from Nexus and clean up ###'
sh '''
rm -rf package-contents*
curl -v -f -u ${NEXUS_CREDS} http://nexus:8081/repository/${NEXUS_REPO_NAME}/${APP_NAME}/${PACKAGE} -o ${PACKAGE}
# clean up
oc delete bc/${APP_NAME} is/${APP_NAME} || rc=$?
'''
echo '### Run OpenShift Build ###'
sh '''
echo "π Creating a sandbox build for inside the cluster π"
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} --build-arg JOB_NAME=${JOB_NAME} --build-arg GIT_BRANCH=${GIT_BRANCH} "
oc new-build --binary --name=${APP_NAME} -l app=${APP_NAME} -l app.kubernetes.io/name=${APP_NAME} ${BUILD_ARGS} --strategy=docker || rc=$?
oc start-build ${APP_NAME} --from-archive=${PACKAGE} ${BUILD_ARGS} --follow --wait
oc tag ${OPENSHIFT_BUILD_NAMESPACE}/${APP_NAME}:latest ${DESTINATION_NAMESPACE}/${APP_NAME}:${VERSION}
'''
}
}
// π IMAGE SCANNING EXAMPLE GOES HERE
stage("ποΈ Deploy - Helm Package") {
agent { label "jenkins-agent-helm" }
steps {
echo '### Lint Helm Chart ###'
sh 'helm lint chart '
// Kube-linter step
echo '### Kube Lint ###'
echo '### Patch Helm Chart ###'
script {
env.CHART_VERSION = sh(returnStdout: true, script: "yq eval .version chart/Chart.yaml").trim()
}
sh '''
# might be overkill...
yq eval -i .appVersion=\\"${VERSION}\\" "chart/Chart.yaml"
# over write the chart name for features / sandbox dev
yq eval -i .name=\\"${APP_NAME}\\" "chart/Chart.yaml"
# probs point to the image inside ocp cluster or perhaps an external repo?
yq eval -i .image_repository=\\"${IMAGE_REPOSITORY}\\" "chart/values.yaml"
yq eval -i .image_name=\\"${APP_NAME}\\" "chart/values.yaml"
yq eval -i .image_namespace=\\"${IMAGE_NAMESPACE}\\" "chart/values.yaml"
# latest built image
yq eval -i .image_version=\\"${VERSION}\\" "chart/values.yaml"
'''
echo '### Publish Helm Chart ###'
sh '''
# package and release helm chart - could only do this if release candidate only
helm package --dependency-update chart/ --app-version ${VERSION}
curl -v -f -u ${NEXUS_CREDS} http://nexus:8081/repository/${NEXUS_REPO_HELM}/ --upload-file ${APP_NAME}-*.tgz
'''
}
}
stage("ποΈ Deploy - App") {
failFast true
parallel {
stage("ποΈ Sandbox - Helm Install"){
options {
skipDefaultCheckout(true)
}
agent { label "jenkins-agent-helm" }
when {
expression { return !(GIT_BRANCH.startsWith("master") || GIT_BRANCH.startsWith("main") )}
}
steps {
// TODO - if SANDBOX, create release in rando ns
sh '''
helm upgrade --install ${APP_NAME} --set application.fullname=${APP_NAME} \
--namespace=${DESTINATION_NAMESPACE} \
http://nexus:8081/repository/${NEXUS_REPO_HELM}/${APP_NAME}-${CHART_VERSION}.tgz
'''
}
}
stage("π§ͺ TestEnv - ArgoCD Git Commit") {
agent { label "jenkins-agent-argocd" }
when {
expression { GIT_BRANCH.startsWith("master") || GIT_BRANCH.startsWith("main") }
}
steps {
echo '### Commit new image tag to git ###'
sh '''
git clone https://${GIT_CREDS}@${ARGOCD_CONFIG_REPO} config-repo
cd config-repo
git checkout ${ARGOCD_CONFIG_REPO_BRANCH} # master or main
PREVIOUS_VERSION=$(yq eval .applications.\\"${APP_NAME}\\".values.image_version "${ARGOCD_CONFIG_REPO_PATH}")
PREVIOUS_CHART_VERSION=$(yq eval .applications.\\"${APP_NAME}\\".source_ref "${ARGOCD_CONFIG_REPO_PATH}")
# patch ArgoCD App config with new app & chart version
yq eval -i .applications.\\"${APP_NAME}\\".source_ref=\\"${CHART_VERSION}\\" "${ARGOCD_CONFIG_REPO_PATH}"
yq eval -i .applications.\\"${APP_NAME}\\".values.image_version=\\"${VERSION}\\" "${ARGOCD_CONFIG_REPO_PATH}"
yq eval -i .applications.\\"${APP_NAME}\\".values.image_namespace=\\"${IMAGE_NAMESPACE}\\" "${ARGOCD_CONFIG_REPO_PATH}"
yq eval -i .applications.\\"${APP_NAME}\\".values.image_repository=\\"${IMAGE_REPOSITORY}\\" "${ARGOCD_CONFIG_REPO_PATH}"
# Commit the changes :P
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 of ${APP_NAME} at version ${VERSION} π" || rc=$?
git remote set-url origin https://${GIT_CREDS}@${ARGOCD_CONFIG_REPO}
git push -u origin ${ARGOCD_CONFIG_REPO_BRANCH}
# verify the deployment by checking the VERSION against PREVIOUS_VERSION
until [ "$label" == "${VERSION}" ]; do
echo "${APP_NAME}-${VERSION} version hasn't started to roll out"
label=$(oc get dc/${APP_NAME} -o yaml -n ${DESTINATION_NAMESPACE} | yq e '.metadata.labels["app.kubernetes.io/version"]' -)
sleep 1
done
oc rollout status --timeout=2m dc/${APP_NAME} -n ${DESTINATION_NAMESPACE} || rc1=$?
if [[ $rc1 != '' ]]; then
yq eval -i .applications.\\"${APP_NAME}\\".source_ref=\\"${PREVIOUS_CHART_VERSION}\\" "${ARGOCD_CONFIG_REPO_PATH}"
yq eval -i .applications.\\"${APP_NAME}\\".values.image_version=\\"${PREVIOUS_VERSION}\\" "${ARGOCD_CONFIG_REPO_PATH}"
git add ${ARGOCD_CONFIG_REPO_PATH}
git commit -m "π’π€¦π»ββοΈ AUTOMATED COMMIT - ${APP_NAME} deployment is reverted to version ${PREVIOUS_VERSION} π’π€¦π»ββοΈ" || rc2=$?
git push -u origin ${ARGOCD_CONFIG_REPO_BRANCH}
# TODO - check the roll back has not failed also...
exit $rc1
else
echo "${APP_NAME} v${VERSION} deployment in ${DESTINATION_NAMESPACE} is successful π πͺ"
fi
'''
}
}
}
}
// π IMAGE SIGN EXAMPLE GOES HERE
// πΈ SBOM EXAMPLE GOES HERE
// π OWASP ZAP STAGE GOES HERE
// ππ»ββοΈ LOAD TESTING EXAMPLE GOES HERE
// π₯Ύ Trigger System Tests
// π₯π¨ BLUE / GREEN DEPLOYMENT GOES HERE
}
}