-
Notifications
You must be signed in to change notification settings - Fork 2
/
ci-cd-github-webhook.yaml
77 lines (67 loc) · 2.93 KB
/
ci-cd-github-webhook.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
id: ci-cd-github-webhook
namespace: company.team
inputs:
- id: payload
type: JSON
tasks:
- id: return
type: io.kestra.plugin.core.debug.Return
format: "{{ trigger.body.pull_request.comments_url ??
inputs.payload.pull_request.comments_url }}"
- id: python_action
type: io.kestra.plugin.scripts.python.Script
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
containerImage: ghcr.io/kestra-io/pydata:latest
script: |
import requests
import json
url = "{{ outputs.return.value }}"
headers = {
'Authorization': 'token {{ secret('GITHUB_ACCESS_TOKEN') }}',
'Accept': 'application/vnd.github.v3+json'
}
payload = {'body': 'hello from `{{ execution.id }}` in `{{ flow.id }}`'}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 201:
print("Comment successfully created.")
else:
print(f"Failed to create comment: {response.text}")
triggers:
- id: github
type: io.kestra.plugin.core.trigger.Webhook
key: "{{ secret('WEBHOOK_KEY') }}"
conditions:
- type: io.kestra.plugin.core.condition.ExpressionCondition
expression: "{{ trigger.body.pull_request.state == 'open' and
trigger.body.pull_request.comments_url }}"
extend:
title: CI/CD with a GitHub webhook trigger in production and a manually-supplied
JSON-type input for local testing
description: >-
This flow can be used as a template to implement a CI/CD pipeline that
processes event payload from a GitHub webhook trigger. In this example, any
time you open a new Pull Request, the flow will be triggered and the flow
will use the payload information to implement custom actions. Here, we
simply add a comment to the pull request using the Pull Request's comment
URL from the event payload, but your use case might require retrieving
information such as branch name, etc.
To facilitate local testing, the expression `'{{
trigger.body.pull_request.comments_url ??
inputs.payload.pull_request.comments_url }}'` will make sure that the flow
uses a value from the webhook if the flow is triggered via a webhook, and
will otherwise use the value from a manually provided input e.g. [a JSON
payload](https://gist.github.com/anna-geller/54c999b3b2ba3647223618f8c657910f).
This way, you can test the workflow end-to-end using a mock data provided as
a JSON-type input before testing it with live data from a webhook.
For more information about this usage pattern, check [the following blog
post](https://levelup.gitconnected.com/when-github-actions-get-painful-to-troubleshoot-try-this-instead-9a134c9e9baf).
tags:
- Git
- API
- DevOps
- Task Runner
ee: false
demo: true
meta_description: This flow can be used as a template to implement a CI/CD
pipeline that processes event payload from a GitHub webhook trigger.