forked from cloud-native-toolkit/template-node-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
335 lines (287 loc) · 13.6 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
/*
* This is a vanilla Jenkins pipeline that relies on the Jenkins kubernetes plugin to dynamically provision agents for
* the build containers.
*
* The individual containers are defined in the `jenkins-pod-template.yaml` and the containers are referenced by name
* in the `container()` blocks. The underlying pod definition expects certain kube Secrets and ConfigMap objects to
* have been created in order for the Pod to run. See `jenkins-pod-template.yaml` for more information.
*
* The cloudName variable is set dynamically based on the existance/value of env.CLOUD_NAME which allows this pipeline
* to run in both Kubernetes and OpenShift environments.
*/
def buildAgentName(String jobName, String buildNumber) {
if (jobName.length() > 55) {
jobName = jobName.substring(0, 55);
}
return "a.${jobName}.${buildNumber}".replace('_', '-').replace('/', '-').replace('-.', '.');
}
def buildLabel = buildAgentName(env.JOB_NAME, env.BUILD_NUMBER);
def namespace = env.NAMESPACE ?: "dev"
def cloudName = env.CLOUD_NAME == "openshift" ? "openshift" : "kubernetes"
def workingDir = env.CLOUD_NAME == "openshift" ? "/home/jenkins" : "/home/jenkins/agent"
podTemplate(
label: buildLabel,
cloud: cloudName,
yaml: """
apiVersion: v1
kind: Pod
spec:
serviceAccountName: jenkins
containers:
- name: node
image: node:11-stretch
tty: true
command: ["/bin/bash"]
workingDir: ${workingDir}
envFrom:
- configMapRef:
name: pactbroker-config
optional: true
- configMapRef:
name: sonarqube-config
optional: true
- secretRef:
name: sonarqube-access
optional: true
env:
- name: HOME
value: ${workingDir}
- name: ibmcloud
image: docker.io/garagecatalyst/ibmcloud-dev:1.0.8
tty: true
command: ["/bin/bash"]
workingDir: ${workingDir}
envFrom:
- configMapRef:
name: ibmcloud-config
- secretRef:
name: ibmcloud-apikey
- configMapRef:
name: artifactory-config
optional: true
- secretRef:
name: artifactory-access
optional: true
env:
- name: CHART_NAME
value: template-node-react
- name: CHART_ROOT
value: chart
- name: TMP_DIR
value: .tmp
- name: HOME
value: /home/devops
- name: ENVIRONMENT_NAME
value: ${namespace}
- name: BUILD_NUMBER
value: ${env.BUILD_NUMBER}
- name: trigger-cd
image: docker.io/garagecatalyst/ibmcloud-dev:1.0.8
tty: true
command: ["/bin/bash"]
workingDir: ${workingDir}
env:
- name: HOME
value: /home/devops
envFrom:
- secretRef:
name: gitops-cd-secret
optional: true
"""
) {
node(buildLabel) {
container(name: 'node', shell: '/bin/bash') {
checkout scm
stage('Setup') {
sh '''#!/bin/bash
# Export project name (lowercase), version, and build number to ./env-config
IMAGE_NAME=$(basename -s .git `git config --get remote.origin.url` | tr '[:upper:]' '[:lower:]' | sed "s/_/-/g")
echo "IMAGE_NAME=${IMAGE_NAME}" > ./env-config
npm run env | grep "^npm_package_version" | sed "s/npm_package_version/IMAGE_VERSION/g" >> ./env-config
echo "BUILD_NUMBER=${BUILD_NUMBER}" >> ./env-config
cat ./env-config
'''
}
stage('Build') {
sh '''#!/bin/bash
npm install
cd client
npm install
cd ..
npm run build
'''
}
stage('Test') {
sh '''#!/bin/bash
npm test
'''
}
stage('Sonar scan') {
sh '''#!/bin/bash
if [[ -z "${SONARQUBE_URL}" ]]; then
echo "Skipping Sonar Qube step as Sonar Qube not installed or configured"
exit 0
fi
npm run sonarqube:scan
'''
}
}
container(name: 'ibmcloud', shell: '/bin/bash') {
stage('Build image') {
sh '''#!/bin/bash
. ./env-config
echo -e "=========================================================================================="
echo -e "BUILDING CONTAINER IMAGE: ${REGISTRY_URL}/${REGISTRY_NAMESPACE}/${IMAGE_NAME}:${IMAGE_VERSION}"
ibmcloud cr build -t ${REGISTRY_URL}/${REGISTRY_NAMESPACE}/${IMAGE_NAME}:${IMAGE_VERSION} .
if [[ $? -ne 0 ]]; then
exit 1
fi
if [[ -n "${BUILD_NUMBER}" ]]; then
echo -e "BUILDING CONTAINER IMAGE: ${REGISTRY_URL}/${REGISTRY_NAMESPACE}/${IMAGE_NAME}:${IMAGE_VERSION}-${BUILD_NUMBER}"
ibmcloud cr image-tag ${REGISTRY_URL}/${REGISTRY_NAMESPACE}/${IMAGE_NAME}:${IMAGE_VERSION} ${REGISTRY_URL}/${REGISTRY_NAMESPACE}/${IMAGE_NAME}:${IMAGE_VERSION}-${BUILD_NUMBER}
fi
'''
}
stage('Deploy to DEV env') {
sh '''#!/bin/bash
. ./env-config
if [[ "${CHART_NAME}" != "${IMAGE_NAME}" ]]; then
cp -R "${CHART_ROOT}/${CHART_NAME}" "${CHART_ROOT}/${IMAGE_NAME}"
cat "${CHART_ROOT}/${CHART_NAME}/Chart.yaml" | \
yq w - name "${IMAGE_NAME}" > "${CHART_ROOT}/${IMAGE_NAME}/Chart.yaml"
fi
CHART_PATH="${CHART_ROOT}/${IMAGE_NAME}"
echo "KUBECONFIG=${KUBECONFIG}"
RELEASE_NAME="${IMAGE_NAME}"
echo "RELEASE_NAME: $RELEASE_NAME"
if [[ -n "${BUILD_NUMBER}" ]]; then
IMAGE_VERSION="${IMAGE_VERSION}-${BUILD_NUMBER}"
fi
echo "INITIALIZING helm with client-only (no Tiller)"
helm init --client-only 1> /dev/null 2> /dev/null
echo "CHECKING CHART (lint)"
helm lint ${CHART_PATH}
if [[ $? -ne 0 ]]; then
exit 1
fi
IMAGE_REPOSITORY="${REGISTRY_URL}/${REGISTRY_NAMESPACE}/${IMAGE_NAME}"
PIPELINE_IMAGE_URL="${REGISTRY_URL}/${REGISTRY_NAMESPACE}/${IMAGE_NAME}:${IMAGE_VERSION}"
# Update helm chart with repository and tag values
cat ${CHART_PATH}/values.yaml | \
yq w - image.repository "${IMAGE_REPOSITORY}" | \
yq w - image.tag "${IMAGE_VERSION}" > ./values.yaml.tmp
cp ./values.yaml.tmp ${CHART_PATH}/values.yaml
cat ${CHART_PATH}/values.yaml
# Using 'upgrade --install" for rolling updates. Note that subsequent updates will occur in the same namespace the release is currently deployed in, ignoring the explicit--namespace argument".
helm template ${CHART_PATH} \
--name ${RELEASE_NAME} \
--namespace ${ENVIRONMENT_NAME} \
--set ingress.tlsSecretName="${TLS_SECRET_NAME}" \
--set ingress.subdomain="${INGRESS_SUBDOMAIN}" > ./release.yaml
echo -e "Generated release yaml for: ${CLUSTER_NAME}/${ENVIRONMENT_NAME}."
cat ./release.yaml
echo -e "Deploying into: ${CLUSTER_NAME}/${ENVIRONMENT_NAME}."
kubectl apply -n ${ENVIRONMENT_NAME} -f ./release.yaml
# ${SCRIPT_ROOT}/deploy-checkstatus.sh ${ENVIRONMENT_NAME} ${IMAGE_NAME} ${IMAGE_REPOSITORY} ${IMAGE_VERSION}
'''
}
stage('Health Check') {
sh '''#!/bin/bash
. ./env-config
INGRESS_NAME="${IMAGE_NAME}"
INGRESS_HOST=$(kubectl get ingress/${INGRESS_NAME} --namespace ${ENVIRONMENT_NAME} --output=jsonpath='{ .spec.rules[0].host }')
PORT='80'
# sleep for 10 seconds to allow enough time for the server to start
sleep 30
if [ $(curl -sL -w "%{http_code}\\n" "http://${INGRESS_HOST}:${PORT}/health" -o /dev/null --connect-timeout 3 --max-time 5 --retry 3 --retry-max-time 30) == "200" ]; then
echo "Successfully reached health endpoint: http://${INGRESS_HOST}:${PORT}/health"
echo "====================================================================="
else
echo "Could not reach health endpoint: http://${INGRESS_HOST}:${PORT}/health"
exit 1;
fi;
'''
}
stage('Package Helm Chart') {
sh '''#!/bin/bash
if [[ -z "${ARTIFACTORY_ENCRPT}" ]]; then
echo "Skipping Artifactory step as Artifactory is not installed or configured"
exit 0
fi
. ./env-config
if [[ -n "${BUILD_NUMBER}" ]]; then
IMAGE_BUILD_VERSION="${IMAGE_VERSION}-${BUILD_NUMBER}"
fi
if [[ -z "${ARTIFACTORY_ENCRPT}" ]]; then
echo "Encrption key not available for Jenkins pipeline, please add it to the artifactory-access"
exit 1
fi
# Check if a Generic Local Repo has been created and retrieve the URL for it
export URL=$(curl -u${ARTIFACTORY_USER}:${ARTIFACTORY_PASSWORD} -X GET "${ARTIFACTORY_URL}/artifactory/api/repositories?type=LOCAL" | jq '.[0].url' | tr -d \\")
echo ${URL}
# Check if the URL is valid and we can continue
if [ -n "${URL}" ]; then
echo "Successfully read Repo ${URL}"
else
echo "No Repository Created"
exit 1;
fi;
# Package Helm Chart
helm package --version ${IMAGE_BUILD_VERSION} ${CHART_ROOT}/${IMAGE_NAME}
# Get the index and re index it with current Helm Chart
curl -u${ARTIFACTORY_USER}:${ARTIFACTORY_ENCRPT} -O "${URL}/${REGISTRY_NAMESPACE}/index.yaml"
if [[ $(cat index.yaml | jq '.errors[0].status') != "404" ]]; then
# Merge the chart index with the current index.yaml held in Artifactory
echo "Merging Chart into index.yaml for Chart Repository"
helm repo index . --url ${URL}/${REGISTRY_NAMESPACE} --merge index.yaml
else
# Dont Merge this is first time one is being created
echo "Creating a new index.yaml for Chart Repository"
rm index.yaml
helm repo index . --url ${URL}/${REGISTRY_NAMESPACE}
fi;
# Persist the Helm Chart in Artifactory for us by ArgoCD
curl -u${ARTIFACTORY_USER}:${ARTIFACTORY_ENCRPT} -i -vvv -T ${IMAGE_NAME}-${IMAGE_BUILD_VERSION}.tgz "${URL}/${REGISTRY_NAMESPACE}/${IMAGE_NAME}-${IMAGE_BUILD_VERSION}.tgz"
# Persist the Helm Chart in Artifactory for us by ArgoCD
curl -u${ARTIFACTORY_USER}:${ARTIFACTORY_ENCRPT} -i -vvv -T index.yaml "${URL}/${REGISTRY_NAMESPACE}/index.yaml"
'''
}
}
container(name: 'trigger-cd', shell: '/bin/bash') {
stage('Trigger CD Pipeline') {
sh '''#!/bin/bash
if [[ -z "${url}" ]]; then
echo "'url' not set. Not triggering CD pipeline"
exit 0
fi
if [[ -z "${host}" ]]; then
echo "'host' not set. Not triggering CD pipeline"
exit 0
fi
if [[ -z "${branch}" ]]; then
branch="master"
fi
. ./env-config
if [[ -n "${BUILD_NUMBER}" ]]; then
IMAGE_BUILD_VERSION="${IMAGE_VERSION}-${BUILD_NUMBER}"
fi
# This email is not used and is not valid, you can ignore but git requires it
git config --global user.email "[email protected]"
git config --global user.name "Jenkins Pipeline"
GIT_URL="https://${username}:${password}@${host}/${org}/${repo}"
git clone -b ${branch} ${GIT_URL} gitops_cd
cd gitops_cd
echo "Requirements before update"
cat "./${IMAGE_NAME}/requirements.yaml"
npm i -g @garage-catalyst/ibm-garage-cloud-cli
igc yq w ./${IMAGE_NAME}/requirements.yaml "dependencies[?(@.name == '${IMAGE_NAME}')].version" ${IMAGE_BUILD_VERSION} -i
echo "Requirements after update"
cat "./${IMAGE_NAME}/requirements.yaml"
git add -u
git commit -m "Updates ${IMAGE_NAME} to ${IMAGE_BUILD_VERSION}"
git push -v
'''
}
}
}
}