-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy path05-build-and-deploy.yaml
More file actions
386 lines (379 loc) · 13.5 KB
/
Copy path05-build-and-deploy.yaml
File metadata and controls
386 lines (379 loc) · 13.5 KB
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
apiVersion: template.openshift.io/v1
kind: Template
metadata:
name: basic-python-tekton-template
parameters:
- description: The name for the application.
name: APPLICATION_NAME
value: basic-python-tekton
- name: NAMESPACE
description: The namespace the various objects will be deployed in.
value: basic-python-tekton
- name: PROJECT_URI
description: "Project URI with the python-demo"
value: https://github.com/konveyor/pelorus
- name: PROJECT_REF
description: "Project ref with the python-demo"
value: master
objects:
# Build
- kind: BuildConfig
apiVersion: build.openshift.io/v1
metadata:
name: ${APPLICATION_NAME}
namespace: ${NAMESPACE}
labels:
app.kubernetes.io/name: ${APPLICATION_NAME}
spec:
source:
type: Git
git:
uri: ${PROJECT_URI}
ref: ${PROJECT_REF}
contextDir: "demo/python-example"
strategy:
sourceStrategy:
from:
kind: "ImageStreamTag"
name: "python-39:latest"
output:
to:
kind: ImageStreamTag
name: "${APPLICATION_NAME}:latest"
- kind: ImageStream
apiVersion: image.openshift.io/v1
metadata:
name: ${APPLICATION_NAME}
namespace: ${NAMESPACE}
labels:
app.kubernetes.io/name: ${APPLICATION_NAME}
app.kubernetes.io/instance: ${APPLICATION_NAME}-build
app.kubernetes.io/component: api
app.kubernetes.io/part-of: ${APPLICATION_NAME}
- kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: ${APPLICATION_NAME}-build-pvc
namespace: ${NAMESPACE}
labels:
app.kubernetes.io/name: ${APPLICATION_NAME}
app.kubernetes.io/instance: ${APPLICATION_NAME}-build
app.kubernetes.io/component: api
app.kubernetes.io/part-of: ${APPLICATION_NAME}
spec:
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 500Mi
- kind: Task
apiVersion: tekton.dev/v1beta1
metadata:
name: debug
namespace: ${NAMESPACE}
description: collect git repo debug information
spec:
# notes: each task specifies the workspace name
# the same pvc is used, only the mounted directory changes based on the name
# in this case "debug"
workspaces:
- name: debug
results:
- name: list-files
description: lists the repos files
- name: commit-time
description: get the time of the last commit
steps:
- name: list-workspace-files
image: ubi9/toolbox
script: |
set -ex
find /workspace
cd /workspace/debug
git remote -vv
git log -n 1
- name: time-last-commit
image: ubi9/toolbox
script: |
set -ex
find /workspace
cd /workspace/debug
printf "epoch timestamps are recommended: git log -1 --format=%ct"
epoch=`git log -1 --format=%ct`
human=`date -d @$epoch`
printf "epoch time: " + $epoch
printf "human readable time: " + $human
echo $epoch > "$(results.commit-time.path)"
- name: debug-time-last-commit
image: ubi9/toolbox
script: |
cat "$(results.commit-time.path)"
- kind: Pipeline
apiVersion: tekton.dev/v1beta1
metadata:
name: ${APPLICATION_NAME}-pipeline
namespace: ${NAMESPACE}
labels:
app.kubernetes.io/name: ${APPLICATION_NAME}
app.kubernetes.io/instance: ${APPLICATION_NAME}-build
app.kubernetes.io/component: api
app.kubernetes.io/part-of: ${APPLICATION_NAME}
spec:
workspaces:
- name: repo
params:
- name: git-url
type: string
default: ${PROJECT_URI}
- name: git-revision
type: string
default: ${PROJECT_REF}
- name: build-no
type: string
default: "1"
- name: BUILD_TYPE
default: "buildconfig"
description: "what type of build to run. One of buildconfig, binary, or s2i"
type: string
tasks:
- name: import-build-image
params:
- name: SCRIPT
value: |
oc import-image python-39 --from=registry.access.redhat.com/ubi9/python-39:latest --confirm -n ${NAMESPACE}
oc import-image toolbox --from=registry.access.redhat.com/ubi9/toolbox:latest --confirm -n ${NAMESPACE}
taskRef:
kind: ClusterTask
name: openshift-client
- name: checkout
timeout: "0h1m0s"
runAfter:
- import-build-image
taskRef:
name: git-clone
kind: ClusterTask
params:
- name: url
value: $(params.git-url)
- name: revision
value: $(params.git-revision)
workspaces:
- name: output
workspace: repo
- name: listfiles
taskRef:
name: debug
kind: Task
workspaces:
- name: debug
workspace: repo
runAfter:
- checkout
# s2i build
- name: builds2i
when:
- input: "$(params.BUILD_TYPE)"
operator: in
values: ["s2i"]
taskRef:
name: s2i-python
kind: ClusterTask
params:
- name: PATH_CONTEXT
value: demo/python-example
- name: IMAGE
value: image-registry.openshift-image-registry.svc:5000/${NAMESPACE}/${APPLICATION_NAME}:latest
workspaces:
- name: source
workspace: repo
runAfter:
- listfiles
# label the tekton s2i build
- name: labels2i
when:
- input: "$(params.BUILD_TYPE)"
operator: in
values: ["s2i"]
params:
- name: SCRIPT
value: |
oc label image "$(tasks.builds2i.results.IMAGE_DIGEST)" app.kubernetes.io/name=${APPLICATION_NAME}
oc annotate image "$(tasks.builds2i.results.IMAGE_DIGEST)" io.openshift.build.commit.id="$(tasks.checkout.results.commit)"
oc annotate image "$(tasks.builds2i.results.IMAGE_DIGEST)" io.openshift.build.source-location="$(tasks.checkout.results.url)"
oc annotate image "$(tasks.builds2i.results.IMAGE_DIGEST)" io.openshift.build.commit.date="$(tasks.listfiles.results.commit-time)"
runAfter: [builds2i]
taskRef:
kind: ClusterTask
name: openshift-client
# buildConfig build
- name: buildconfig
when:
- input: "$(params.BUILD_TYPE)"
operator: in
values: ["buildconfig"]
params:
- name: SCRIPT
value: |
oc patch bc/${APPLICATION_NAME} -p '{"spec":{"source":{"git":{"uri": "$(tasks.checkout.results.url)"}}}}'
oc start-build ${APPLICATION_NAME} --wait
- name: VERSION
value: latest
runAfter: [listfiles]
taskRef:
kind: ClusterTask
name: openshift-client
# binary build
- name: buildbinary
when:
- input: "$(params.BUILD_TYPE)"
operator: in
values: ["binary"]
taskRef:
kind: ClusterTask
name: openshift-client
params:
- name: SCRIPT
value: |
if [ $(params.build-no) == "1" ]; then
oc delete buildConfig ${APPLICATION_NAME} || true
fi
cd $(workspaces.manifest-dir.path)
pwd
# create build
oc get bc "${APPLICATION_NAME}"
retVal=$?
if [ $retVal -ne 0 ]; then
echo "oc new-build python --name=${APPLICATION_NAME} --binary=true"
oc new-build python --name=${APPLICATION_NAME} --binary=true
# label build name
echo "oc label bc ${APPLICATION_NAME} app.kubernetes.io/name=${APPLICATION_NAME}"
oc label bc ${APPLICATION_NAME} app.kubernetes.io/name=${APPLICATION_NAME}
fi
# start build
oc start-build bc/${APPLICATION_NAME} --from-dir=$(workspaces.manifest-dir.path)/demo/python-example --wait || exit 2
# annotate build
echo "annotate binary build no $(params.build-no) with git data: "
echo "git-url: $(params.git-url)"
echo "commit.id: $(tasks.checkout.results.commit)"
oc annotate build "${APPLICATION_NAME}-$(params.build-no)" --overwrite \
io.openshift.build.commit.id="$(tasks.checkout.results.commit)" \
io.openshift.build.source-location="$(params.git-url)"
- name: VERSION
value: latest
workspaces:
- name: manifest-dir
workspace: repo
runAfter:
- listfiles
# test the build
finally:
- name: test-build
params:
- name: SCRIPT
value: |
printf "Test the basic-python app is running:\n"
curl $(oc get route -n ${NAMESPACE} ${APPLICATION_NAME} -o=template='http://{{.spec.host}}') 2>&1 | grep "Hello world!" || exit 2
printf "\n"
printf "Test that Pelorus has found the latest git commit:\n"
printf "Build Type is $(params.BUILD_TYPE)\n"
if [[ "$(params.BUILD_TYPE)" == "buildconfig" ]]; then
printf "test buildconfig: requires exporter named committime-exporter \n"
curl $(oc get route -n pelorus committime-exporter -o=template='http://{{.spec.host | printf "%s\n"}}') | grep "$(tasks.checkout.results.commit)" || exit 2
fi
if [[ "$(params.BUILD_TYPE)" == "binary" ]]; then
printf "test binary: requires exporter named committime-exporter \n"
curl $(oc get route -n pelorus committime-exporter -o=template='http://{{.spec.host | printf "%s\n"}}') | grep "$(tasks.checkout.results.commit)" || exit 2
fi
if [[ "$(params.BUILD_TYPE)" == "s2i" ]]; then
printf "test s2i: requires exporter named committime-image-exporter with variable PROVIDER=image \n"
curl $(oc get route -n pelorus committime-image-exporter -o=template='http://{{.spec.host | printf "%s\n"}}') | grep "$(tasks.checkout.results.commit)" || exit 2
fi
timeout: 2m
retries: 3
taskRef:
kind: ClusterTask
name: openshift-client
# Deploy
- kind: Service
apiVersion: v1
metadata:
annotations:
description: The web server's http port.
labels:
app.kubernetes.io/name: ${APPLICATION_NAME}
app.kubernetes.io/instance: ${APPLICATION_NAME}-deployment
app.kubernetes.io/component: api
app.kubernetes.io/part-of: ${APPLICATION_NAME}
name: ${APPLICATION_NAME}
namespace: ${NAMESPACE}
spec:
ports:
- port: 8080
targetPort: 8080
selector:
deploymentConfig: ${APPLICATION_NAME}
- kind: Route
apiVersion: route.openshift.io/v1
metadata:
name: ${APPLICATION_NAME}
namespace: ${NAMESPACE}
annotations:
description: Route for application's http service.
labels:
app.kubernetes.io/name: ${APPLICATION_NAME}
app.kubernetes.io/instance: ${APPLICATION_NAME}-deployment
app.kubernetes.io/component: api
app.kubernetes.io/part-of: ${APPLICATION_NAME}
spec:
to:
name: ${APPLICATION_NAME}
- kind: DeploymentConfig
apiVersion: apps.openshift.io/v1
metadata:
name: ${APPLICATION_NAME}
namespace: ${NAMESPACE}
labels:
app.kubernetes.io/name: ${APPLICATION_NAME}
app.kubernetes.io/instance: ${APPLICATION_NAME}-deployment
app.kubernetes.io/component: api
app.kubernetes.io/part-of: ${APPLICATION_NAME}
spec:
replicas: 1
selector:
deploymentConfig: ${APPLICATION_NAME}
strategy:
type: Rolling
template:
metadata:
labels:
app.kubernetes.io/name: ${APPLICATION_NAME}
app.kubernetes.io/instance: ${APPLICATION_NAME}-deployment
app.kubernetes.io/component: api
app.kubernetes.io/part-of: ${APPLICATION_NAME}
deploymentConfig: ${APPLICATION_NAME}
name: ${APPLICATION_NAME}
spec:
containers:
- name: ${APPLICATION_NAME}
image: ${APPLICATION_NAME}
env:
- name: APP_FILE
value: example.py
imagePullPolicy: Always
ports:
- containerPort: 8080
name: http
protocol: TCP
readinessProbe:
exec:
command: [/bin/bash, -c, curl -s 'http://localhost:8080']
terminationGracePeriodSeconds: 60
triggers:
- imageChangeParams:
automatic: true
containerNames:
- ${APPLICATION_NAME}
from:
kind: ImageStreamTag
name: ${APPLICATION_NAME}:latest
type: ImageChange
- type: ConfigChange