-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcloud-deploy-bot.yaml
489 lines (472 loc) · 19.9 KB
/
cloud-deploy-bot.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This example sends notifications Cloud Deploy events to a Google Chat
# space. For more information, see this Medium article: <link>
#
# This covers a simple example of notifying on new releases being created.
#
globals:
chatSpace: "spaces/AAAA1234567"
serviceAccountEmail: [email protected]
buildBucket: project-build-bucket
statusToText:
Start: "started"
Succeed: "succeeded"
Failure: "failed"
Terminated: "was terminated"
Cancelled: "was cancelled"
Create: "was created"
Update: "was updated"
Delete: "was deleted"
pipeline:
- type: processor.genericjson
runIf: '{% if event.attributes.DeliveryPipelineId is not defined %}1{% endif %}'
- type: processor.setvariable # setvariable is a helper for complex evaluations
output: command
config:
value: |
{%- if (data.type is defined and data.type == "MESSAGE" and
data.message.annotations and
(data.message.annotations|first).type == "SLASH_COMMAND" and
(data.message.annotations|first).slashCommand.commandName == "/release"
)
-%}create{%- endif -%}
{%- if (data.type is defined and data.type == "MESSAGE" and
data.message.annotations and
(data.message.annotations|first).type == "SLASH_COMMAND" and
(data.message.annotations|first).slashCommand.commandName == "/promote"
)
-%}promote{%- endif -%}
{%- if (data.type is defined and data.type == "MESSAGE" and
data.message.annotations and
(data.message.annotations|first).type == "SLASH_COMMAND" and
(data.message.annotations|first).slashCommand.commandName == "/rollback"
)
-%}rollback{%- endif -%}
{%- if (data.type is defined and data.type == "MESSAGE" and
data.message.annotations and
(data.message.annotations|first).type == "SLASH_COMMAND" and
(data.message.annotations|first).slashCommand.commandName == "/approve"
)
-%}approve{%- endif -%}
{%- if (data.type is defined and data.type == "MESSAGE" and
data.message.annotations and
(data.message.annotations|first).type == "SLASH_COMMAND" and
(data.message.annotations|first).slashCommand.commandName == "/reject"
)
-%}reject{%- endif -%}
{%- if (data.type is not defined and
event.attributes.ResourceType is defined and
event.attributes.ResourceType == "Release" and
event.attributes.Action == "Create")
-%}notify-release{%- endif -%}
{%- if (data.type is not defined and
event.attributes.ResourceType is defined and
event.attributes.ResourceType == "Rollout" and
event.attributes.Action is in(["Succeed", "Failure", "Terminated", "Cancelled"]))
-%}notify-rollout{%- endif -%}
{%- if (data.type is not defined and
event.attributes.Action is defined and
event.attributes.Action == "Required")
-%}notify-approval-required{%- endif -%}
{%- if (data.type is not defined and
event.attributes.Action is defined and
event.attributes.Action == "Rejected")
-%}notify-approval-rejected{%- endif -%}
{%- if (data.type is not defined and
event.attributes.Action is defined and
event.attributes.Action == "Approved")
-%}notify-approval-approved{%- endif -%}
# This file combines both notification handling from Cloud Deploy and incoming
# messages from Google Chat. It could be also split into 2 or 3 functions (eg.
# one for chat commands, one for releases and one for rollouts).
## CREATE RELEASE (/release)
- type: processor.git # Download repository containing Skaffold files
runIf: |
{% if command == 'create' %}1{% endif %}
output: git
config:
url: 'https://github.com/your-org/your-repo.git' # You need to set a SSH private key if you have a private repository, see Git processor documentation
directory: 'build'
depth: 1
- type: processor.compress # Create a tar.gz archive of the Skaffold files
runIf: |
{% if command == 'create' %}1{% endif %}
output: release_archive
config:
glob: 'build/*.yaml'
format: 'tar.gz'
output: 'deploy-{{ req_random_uuid }}.tar.gz'
strip: 1
exclude:
- build/clouddeploy.yaml
- type: output.gcs # Upload created archive to GCS
runIf: |
{% if command == 'create' %}1{% endif %}
config:
bucket: '{{ buildBucket }}'
object: '{{ release_archive.filename }}'
file: '{{ release_archive.path }}'
- type: processor.clouddeploy # Create a new release
runIf: |
{% if command == 'create' %}1{% endif %}
output: create_release
config:
region: europe-west1
name: helloworld-app
mode: releases.create
releaseId: |
{{ (data.message.text|parse_string("/release {releaseId}")).releaseId }}
release:
description: |
Release created by {{ data.message.sender.displayName }}.
skaffoldConfigUri: 'gs://{{ buildBucket }}/{{ release_archive.filename }}'
buildArtifacts:
- image: helloworld-image
tag: gcr.io/cloudrun/hello
- type: processor.clouddeploy # Create the initial rollout to dev
runIf: |
{% if command == 'create' %}1{% endif %}
output: create_rollout
config:
region: europe-west1
name: helloworld-app
mode: releases.rollouts.create
releaseId: |
{{ create_release.name }}
rolloutId: |
{{ (create_release.name|parse_url).name }}-to-qsdev
rollout:
description: |
Rollout created by {{ data.message.sender.displayName }}.
targetId: run-qsdev
- type: output.logger
runIf: |
{% if command == 'create' %}1{% endif %}
config:
message: |
Release: {{ create_release|json_encode }}
Rollout: {{ create_rollout|json_encode }}
## PROMOTE RELEASE (/promote)
- type: processor.clouddeploy
runIf: |
{% if command == 'promote' %}1{% endif %}
output: promote_release
variables:
releaseId: '{{ (data.message.text|parse_string("/promote {releaseId} to {targetId}")).releaseId }}'
targetId: '{{ (data.message.text|parse_string("/promote {releaseId} to {targetId}")).targetId }}'
config:
region: europe-west1
name: helloworld-app
mode: releases.rollouts.create
releaseId: |
{{ releaseId }}
rolloutId: |
{{ releaseId }}-to-{{ targetId }}
rollout:
description: |
Promotion created by {{ data.message.sender.displayName }}.
targetId: '{{ targetId }}'
- type: output.logger
runIf: |
{% if command == 'promote' %}1{% endif %}
config:
message: |
{{ promote_release|json_encode }}
## ROLLBACK RELEASE (/rollback)
- type: processor.clouddeploy
runIf: |
{% if command == 'rollback' %}1{% endif %}
output: rollback_release
variables:
releaseId: '{{ (data.message.text|parse_string("/rollback {releaseId} to {targetId}")).releaseId }}'
targetId: '{{ (data.message.text|parse_string("/rollback {releaseId} to {targetId}")).targetId }}'
config:
region: europe-west1
name: helloworld-app
mode: releases.rollouts.create
releaseId: |
{{ releaseId }}
rolloutId: |
{{ releaseId }}-rollback-to-{{ targetId }}
rollout:
description: |
Rollback created by {{ data.message.sender.displayName }}.
targetId: '{{ targetId }}'
- type: output.logger
runIf: |
{% if command == 'rollback' %}1{% endif %}
config:
message: |
{{ rollback_release|json_encode }}
## APPROVE RELEASE (/approve)
- type: processor.clouddeploy
runIf: |
{% if command == 'approve' %}1{% endif %}
output: approve_rollout
variables:
releaseId: '{{ (data.message.text|parse_string("/approve {releaseId} rollout {rolloutId}")).releaseId }}'
rolloutId: '{{ (data.message.text|parse_string("/approve {releaseId} rollout {rolloutId}")).rolloutId }}'
config:
region: europe-west1
name: helloworld-app
mode: releases.rollouts.approve
releaseId: |
{{ releaseId }}
rolloutId: |
{{ rolloutId }}
- type: output.logger
runIf: |
{% if command == 'approve' %}1{% endif %}
config:
message: |
{{ approve_rollout|json_encode }}
## REJECT RELEASE (/reject)
- type: processor.clouddeploy
runIf: |
{% if command == 'reject' %}1{% endif %}
output: reject_rollout
variables:
releaseId: '{{ (data.message.text|parse_string("/reject {releaseId} rollout {rolloutId}")).releaseId }}'
rolloutId: '{{ (data.message.text|parse_string("/reject {releaseId} rollout {rolloutId}")).rolloutId }}'
config:
region: europe-west1
name: helloworld-app
mode: releases.rollouts.reject
releaseId: |
{{ releaseId }}
rolloutId: |
{{ rolloutId }}
- type: output.logger
runIf: |
{% if command == 'reject' %}1{% endif %}
config:
message: |
{{ reject_rollout|json_encode }}
## NOTIFICATIONS
# Notify on creation of a new release
- type: output.chat
runIf: |
{% if command == 'notify-release' %}1{% endif %}
config:
parent: "{{ chatSpace }}"
serviceAccountEmail: "{{ serviceAccountEmail }}"
message:
cardsV2:
- cardId: new-release
card:
header:
title: A new release has been created!
subtitle: Google Cloud Deploy
imageUrl: https://lh3.googleusercontent.com/9E-FDrxYH-Q8I-eLLDljpdbpXd5Gt3oJqH1eN1vkV168sdFt1C2DwnVXzTaJb7ruqU35_RTPKQ=e14-rw-lo-sc0xffffff-h260-w204
imageType: CIRCLE
imageAltText: Google Cloud Deploy
sections:
- header: "Release details"
collapsible: true
uncollapsibleWidgetsCount: 4
widgets:
- decoratedText:
topLabel: "Release pipeline"
text: "{{ event.attributes.DeliveryPipelineId }}"
- decoratedText:
topLabel: "Release ID"
text: "{{ event.attributes.ReleaseId }}"
- decoratedText:
topLabel: "Location"
text: "{{ event.attributes.Location }}"
- buttonList:
buttons:
- text: View in console
onClick:
openLink:
url: |
https://console.cloud.google.com/deploy/delivery-pipelines/{{ event.attributes.Location }}/{{ event.attributes.DeliveryPipelineId }}/releases/{{ event.attributes.ReleaseId }}/rollouts?project={{ event.attributes.ProjectId }}
#text: |
# 🚀 A new release has been created for *`{{ event.attributes.DeliveryPipelineId }}`*: `{{ event.attributes.ReleaseId }}` 🚀
# Notify for rollout of a release (eg. a deploy), appears in the clouddeploy-operations topic
- type: output.chat
runIf: |
{% if command == 'notify-rollout' %}1{% endif %}
config:
parent: "{{ chatSpace }}"
serviceAccountEmail: "{{ serviceAccountEmail }}"
message:
cardsV2:
- cardId: new-rollout
card:
header:
title: A rollout operation {{ statusToText[event.attributes.Action] }}.
subtitle: Google Cloud Deploy
imageUrl: https://lh3.googleusercontent.com/9E-FDrxYH-Q8I-eLLDljpdbpXd5Gt3oJqH1eN1vkV168sdFt1C2DwnVXzTaJb7ruqU35_RTPKQ=e14-rw-lo-sc0xffffff-h260-w204
imageType: CIRCLE
imageAltText: Google Cloud Deploy
sections:
- header: "Deployment details"
collapsible: true
uncollapsibleWidgetsCount: 6
widgets:
- decoratedText:
topLabel: "Target"
text: "{{ event.attributes.TargetId }}"
- decoratedText:
topLabel: "Rollout ID"
text: "{{ event.attributes.RolloutId }}"
- decoratedText:
topLabel: "Release pipeline"
text: "{{ event.attributes.DeliveryPipelineId }}"
- decoratedText:
topLabel: "Release ID"
text: "{{ event.attributes.ReleaseId }}"
- decoratedText:
topLabel: "Location"
text: "{{ event.attributes.Location }}"
- buttonList:
buttons:
- text: View in console
onClick:
openLink:
url: |
https://console.cloud.google.com/deploy/delivery-pipelines/{{ event.attributes.Location }}/{{ event.attributes.DeliveryPipelineId }}/releases/{{ event.attributes.ReleaseId }}/rollouts?project={{ event.attributes.ProjectId }}
#text: |
# 🚀 A new release has been created for *`{{ event.attributes.DeliveryPipelineId }}`*: `{{ event.attributes.ReleaseId }}` 🚀
# Notify that approval is required for a release
- type: output.chat
runIf: |
{% if command == 'notify-approval-required' %}1{% endif %}
config:
parent: "{{ chatSpace }}"
serviceAccountEmail: "{{ serviceAccountEmail }}"
message:
cardsV2:
- cardId: rollout-approval
card:
header:
title: A rollout requires approval.
subtitle: Google Cloud Deploy
imageUrl: https://lh3.googleusercontent.com/9E-FDrxYH-Q8I-eLLDljpdbpXd5Gt3oJqH1eN1vkV168sdFt1C2DwnVXzTaJb7ruqU35_RTPKQ=e14-rw-lo-sc0xffffff-h260-w204
imageType: CIRCLE
imageAltText: Google Cloud Deploy
sections:
- header: "Rollout details"
collapsible: true
uncollapsibleWidgetsCount: 5
widgets:
- decoratedText:
topLabel: "Target"
text: "{{ event.attributes.TargetId }}"
- decoratedText:
topLabel: "Rollout ID"
text: "{{ event.attributes.RolloutId }}"
- decoratedText:
topLabel: "Release pipeline"
text: "{{ event.attributes.DeliveryPipelineId }}"
- decoratedText:
topLabel: "Release ID"
text: "{{ event.attributes.ReleaseId }}"
- decoratedText:
topLabel: "Location"
text: "{{ event.attributes.Location }}"
#text: |
# 🚀 A new rollout (`{{ event.attributes.RolloutId }}`) requires approval in *`{{ event.attributes.DeliveryPipelineId }}`*: `{{ event.attributes.ReleaseId }}` to `{{ event.attributes.TargetId }}` 🚀
# Notify that approval was rejected
- type: output.chat
runIf: |
{% if command == 'notify-approval-rejected' %}1{% endif %}
config:
parent: "{{ chatSpace }}"
serviceAccountEmail: "{{ serviceAccountEmail }}"
message:
cardsV2:
- cardId: rollout-rejection
card:
header:
title: A rollout was rejected.
subtitle: Google Cloud Deploy
imageUrl: https://lh3.googleusercontent.com/9E-FDrxYH-Q8I-eLLDljpdbpXd5Gt3oJqH1eN1vkV168sdFt1C2DwnVXzTaJb7ruqU35_RTPKQ=e14-rw-lo-sc0xffffff-h260-w204
imageType: CIRCLE
imageAltText: Google Cloud Deploy
sections:
- header: "Rollout details"
collapsible: true
uncollapsibleWidgetsCount: 5
widgets:
- decoratedText:
topLabel: "Target"
text: "{{ event.attributes.TargetId }}"
- decoratedText:
topLabel: "Rollout ID"
text: "{{ event.attributes.RolloutId }}"
- decoratedText:
topLabel: "Release pipeline"
text: "{{ event.attributes.DeliveryPipelineId }}"
- decoratedText:
topLabel: "Release ID"
text: "{{ event.attributes.ReleaseId }}"
- decoratedText:
topLabel: "Location"
text: "{{ event.attributes.Location }}"
#text: |
# 🚀 A rollout (`{{ event.attributes.RolloutId }}`) in *`{{ event.attributes.DeliveryPipelineId }}`* for release `{{ event.attributes.ReleaseId }}` to `{{ event.attributes.TargetId }}` was rejected. 🚀
# Notify that approval was approved
- type: output.chat
runIf: |
{% if command == 'notify-approval-approved' %}1{% endif %}
config:
parent: "{{ chatSpace }}"
serviceAccountEmail: "{{ serviceAccountEmail }}"
message:
cardsV2:
- cardId: rollout-approved
card:
header:
title: A rollout was approved!
subtitle: Google Cloud Deploy
imageUrl: https://lh3.googleusercontent.com/9E-FDrxYH-Q8I-eLLDljpdbpXd5Gt3oJqH1eN1vkV168sdFt1C2DwnVXzTaJb7ruqU35_RTPKQ=e14-rw-lo-sc0xffffff-h260-w204
imageType: CIRCLE
imageAltText: Google Cloud Deploy
sections:
- header: "Rollout details"
collapsible: true
uncollapsibleWidgetsCount: 5
widgets:
- decoratedText:
topLabel: "Target"
text: "{{ event.attributes.TargetId }}"
- decoratedText:
topLabel: "Rollout ID"
text: "{{ event.attributes.RolloutId }}"
- decoratedText:
topLabel: "Release pipeline"
text: "{{ event.attributes.DeliveryPipelineId }}"
- decoratedText:
topLabel: "Release ID"
text: "{{ event.attributes.ReleaseId }}"
- decoratedText:
topLabel: "Location"
text: "{{ event.attributes.Location }}"
#text: |
# 🚀 A rollout (`{{ event.attributes.RolloutId }}`) in *`{{ event.attributes.DeliveryPipelineId }}`* for release `{{ event.attributes.ReleaseId }}` to `{{ event.attributes.TargetId }}` was approved. 🚀
canFail: true
onError:
type: output.chat
config:
parent: "{{ chatSpace }}"
serviceAccountEmail: "{{ serviceAccountEmail }}"
message:
text: |
Sorry, something went wrong. The error was:
```
{{ exception }}
```