-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcicd-template.yaml
394 lines (368 loc) · 14 KB
/
cicd-template.yaml
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
apiVersion: v1
kind: Template
labels:
template: cicd
group: cicd
metadata:
annotations:
iconClass: icon-jenkins
tags: instant-app,jenkins,gogs,nexus,cicd
name: cicd
message: "Use the following credentials for login:\nJenkins: use your OpenShift credentials\nNexus: admin/admin123\nSonarQube: admin/admin\nGogs Git Server: gogs/gogs"
parameters:
- displayName: DEV project name
value: dev
name: DEV_PROJECT
required: true
- displayName: STAGE project name
value: stage
name: STAGE_PROJECT
required: true
- displayName: Deploy Eclipse Che
description: Deploy Eclipse Che in order to use as an online IDE for changing code in this demo
value: "false"
name: WITH_CHE
required: true
- displayName: Ephemeral
description: Use no persistent storage for Gogs and Nexus
value: "true"
name: EPHEMERAL
required: true
- description: Webhook secret
from: '[a-zA-Z0-9]{8}'
generate: expression
name: WEBHOOK_SECRET
required: true
objects:
- apiVersion: v1
groupNames: null
kind: RoleBinding
metadata:
name: default_admin
roleRef:
name: admin
subjects:
- kind: ServiceAccount
name: default
# Pipeline
- apiVersion: v1
kind: BuildConfig
metadata:
annotations:
pipeline.alpha.openshift.io/uses: '[{"name": "jenkins", "namespace": "", "kind": "DeploymentConfig"}]'
labels:
app: cicd-pipeline
name: cicd-pipeline
name: tasks-pipeline-preloaded
spec:
triggers:
- type: GitHub
github:
secret: "secret101"
- type: Generic
generic:
secret: "secret101"
runPolicy: Serial
source:
type: None
strategy:
jenkinsPipelineStrategy:
env:
- name: DEV_PROJECT
value: ${DEV_PROJECT}
- name: STAGE_PROJECT
value: ${STAGE_PROJECT}
jenkinsfile: |-
def version, mvnCmd = "mvn -s configuration/cicd-settings-nexus3.xml"
pipeline {
agent {
label 'maven'
}
stages {
stage('Build App') {
steps {
git branch: 'eap-7', url: 'http://gogs:3000/gogs/openshift-tasks.git'
script {
def pom = readMavenPom file: 'pom.xml'
version = pom.version
}
sh "${mvnCmd} install -DskipTests=true"
}
}
stage('Test') {
steps {
sh "${mvnCmd} test"
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
}
}
stage('Code Analysis') {
steps {
script {
sh "${mvnCmd} sonar:sonar -Dsonar.host.url=http://sonarqube:9000 -DskipTests=true"
}
}
}
stage('Archive App') {
steps {
sh "${mvnCmd} deploy -DskipTests=true -P nexus3"
}
}
stage('Create Image Builder') {
when {
expression {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
return !openshift.selector("bc", "tasks").exists();
}
}
}
}
steps {
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
openshift.newBuild("--name=tasks", "--image-stream=jboss-eap70-openshift:1.5", "--binary=true")
}
}
}
}
}
stage('Build Image') {
steps {
sh "rm -rf oc-build && mkdir -p oc-build/deployments"
sh "cp target/openshift-tasks.war oc-build/deployments/ROOT.war"
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
openshift.selector("bc", "tasks").startBuild("--from-dir=oc-build", "--wait=true")
}
}
}
}
}
stage('Create DEV') {
when {
expression {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
return !openshift.selector('dc', 'tasks').exists()
}
}
}
}
steps {
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
def app = openshift.newApp("tasks:latest")
app.narrow("svc").expose();
def dc = openshift.selector("dc", "tasks")
while (dc.object().spec.replicas != dc.object().status.availableReplicas) {
sleep 10
}
openshift.set("triggers", "dc/tasks", "--manual")
}
}
}
}
}
stage('Deploy DEV') {
steps {
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
openshift.selector("dc", "tasks").rollout().latest();
}
}
}
}
}
stage('Promote to STAGE?') {
steps {
timeout(time:15, unit:'MINUTES') {
input message: "Promote to STAGE?", ok: "Promote"
}
script {
openshift.withCluster() {
openshift.tag("${env.DEV_PROJECT}/tasks:latest", "${env.STAGE_PROJECT}/tasks:${version}")
}
}
}
}
stage('Deploy STAGE') {
steps {
script {
openshift.withCluster() {
openshift.withProject(env.STAGE_PROJECT) {
if (openshift.selector('dc', 'tasks').exists()) {
openshift.selector('dc', 'tasks').delete()
openshift.selector('svc', 'tasks').delete()
openshift.selector('route', 'tasks').delete()
}
openshift.newApp("tasks:${version}").narrow("svc").expose()
}
}
}
}
}
}
}
type: JenkinsPipeline
- apiVersion: v1
kind: ConfigMap
metadata:
labels:
app: cicd-pipeline
role: jenkins-slave
name: jenkins-slaves
data:
maven-template: |-
<org.csanchez.jenkins.plugins.kubernetes.PodTemplate>
<inheritFrom></inheritFrom>
<name>maven-old</name>
<privileged>false</privileged>
<alwaysPullImage>false</alwaysPullImage>
<instanceCap>2147483647</instanceCap>
<idleMinutes>0</idleMinutes>
<label>maven</label>
<serviceAccount>jenkins</serviceAccount>
<nodeSelector></nodeSelector>
<customWorkspaceVolumeEnabled>false</customWorkspaceVolumeEnabled>
<workspaceVolume class="org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.EmptyDirWorkspaceVolume">
<memory>false</memory>
</workspaceVolume>
<volumes />
<containers>
<org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate>
<name>jnlp</name>
<image>registry.access.redhat.com/openshift3/jenkins-slave-maven-rhel7</image>
<privileged>false</privileged>
<alwaysPullImage>false</alwaysPullImage>
<workingDir>/tmp</workingDir>
<command></command>
<args>${computer.jnlpmac} ${computer.name}</args>
<ttyEnabled>false</ttyEnabled>
<resourceRequestCpu>200m</resourceRequestCpu>
<resourceRequestMemory>512Mi</resourceRequestMemory>
<resourceLimitCpu>2</resourceLimitCpu>
<resourceLimitMemory>4Gi</resourceLimitMemory>
<envVars/>
</org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate>
</containers>
<envVars/>
<annotations/>
<imagePullSecrets/>
</org.csanchez.jenkins.plugins.kubernetes.PodTemplate>
# Setup Demo
- apiVersion: batch/v1
kind: Job
metadata:
name: cicd-demo-installer
spec:
activeDeadlineSeconds: 400
completions: 1
parallelism: 1
template:
spec:
containers:
- env:
- name: CICD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
command:
- /bin/bash
- -x
- -c
- |
oc set resources dc/jenkins --limits=memory=1Gi --requests=memory=512Mi
oc label dc jenkins app=jenkins --overwrite
#HOSTNAME=$(oc get route jenkins -o template --template='{{.spec.host}}' | sed "s/jenkins-${CICD_NAMESPACE}.//g")
#GOGS_HOSTNAME="gogs-$CICD_NAMESPACE.$HOSTNAME"
#if [ "${EPHEMERAL}" == "true" ] ; then
# oc new-app -f https://raw.githubusercontent.com/epe105/gogs-openshift-docker/master/openshift/gogs-template.yaml \
# --param=GOGS_VERSION=0.11.34 \
# --param=HOSTNAME=$GOGS_HOSTNAME \
# --param=SKIP_TLS_VERIFY=true
#else
# oc new-app -f https://raw.githubusercontent.com/epe105/gogs-openshift-docker/master/openshift/gogs-persistent-template.yaml \
# --param=GOGS_VERSION=0.11.34 \
# --param=HOSTNAME=$GOGS_HOSTNAME \
# --param=SKIP_TLS_VERIFY=true
#fi
#sleep 5
#oc new-app -f https://raw.githubusercontent.com/epe105/sonarqube-openshift-docker/master/sonarqube-template.yaml --param=SONARQUBE_VERSION=7.0 --param=SONAR_MAX_MEMORY=4Gi
#oc new-app -f https://raw.githubusercontent.com/RHsyseng/docker-rhel-sonarqube/master/templates/sonarqube-ephemeral-template.yaml --param=SONARQUBE_SERVICE_NAME=sonarqube
oc new-app registry.centos.org/rhsyseng/sonarqube
oc expose svc/sonarqube
#if [ "${WITH_CHE}" == "true" ] ; then
# oc process -f https://raw.githubusercontent.com/epe105/minishift-addons/master/add-ons/che/templates/che-single-user.yml \
# --param PROJECT_NAME=$CICD_NAMESPACE \
# --param DOMAIN_NAME=$HOSTNAME \
# --param OPENSHIFT_OAUTH_TOKEN="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
# | oc create -f -
# oc set resources dc/che --limits=cpu=1,memory=2Gi --requests=cpu=200m,memory=512Mi
#fi
#if [ "${EPHEMERAL}" == "true" ] ; then
# oc new-app -f https://raw.githubusercontent.com/OpenShiftDemos/nexus/master/nexus3-template.yaml --param=NEXUS_VERSION=3.13.0 --param=MAX_MEMORY=2Gi
#else
# oc new-app -f https://raw.githubusercontent.com/OpenShiftDemos/nexus/master/nexus3-persistent-template.yaml --param=NEXUS_VERSION=3.13.0 --param=MAX_MEMORY=2Gi
#fi
#oc set resources dc/nexus --requests=cpu=200m --limits=cpu=2
#GOGS_SVC=$(oc get svc gogs -o template --template='{{.spec.clusterIP}}')
#GOGS_USER=gogs
#GOGS_PWD=gogs
#oc rollout status dc gogs
#_RETURN=$(curl -o /tmp/curl.log -sL --post302 -w "%{http_code}" http://$GOGS_SVC:3000/user/sign_up \
# --form user_name=$GOGS_USER \
# --form password=$GOGS_PWD \
# --form retype=$GOGS_PWD \
# --form email=admin@gogs.com)
#sleep 5
#if [ $_RETURN != "200" ] && [ $_RETURN != "302" ] ; then
# echo "ERROR: Failed to create Gogs admin"
# cat /tmp/curl.log
# exit 255
#fi
#sleep 10
#cat <<EOF > /tmp/data.json
#{
# "clone_addr": "https://github.com/epe105/openshift-tasks.git",
# "uid": 1,
# "repo_name": "openshift-tasks"
#}
#EOF
#_RETURN=$(curl -o /tmp/curl.log -sL -w "%{http_code}" -H "Content-Type: application/json" \
#-u $GOGS_USER:$GOGS_PWD -X POST http://$GOGS_SVC:3000/api/v1/repos/migrate -d @/tmp/data.json)
#if [ $_RETURN != "201" ] ;then
# echo "ERROR: Failed to imported openshift-tasks GitHub repo"
# cat /tmp/curl.log
# exit 255
#fi
#sleep 5
#cat <<EOF > /tmp/data.json
#{
# "type": "gogs",
# "config": {
# "url": "https://openshift.default.svc.cluster.local/oapi/v1/namespaces/$CICD_NAMESPACE/buildconfigs/tasks-pipeline/webhooks/secret101/generic",
# "content_type": "json"
# },
# "events": [
# "push"
# ],
# "active": true
#}
#EOF
#_RETURN=$(curl -o /tmp/curl.log -sL -w "%{http_code}" -H "Content-Type: application/json" \
#-u $GOGS_USER:$GOGS_PWD -X POST http://$GOGS_SVC:3000/api/v1/repos/gogs/openshift-tasks/hooks -d @/tmp/data.json)
#if [ $_RETURN != "201" ] ; then
# echo "ERROR: Failed to set webhook"
# cat /tmp/curl.log
# exit 255
#fi
image: openshift/origin:v3.9.0
name: cicd-demo-installer-job
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
restartPolicy: Never