|
1 | 1 | Build Notifications and Webhooks
|
2 | 2 | ================================
|
3 | 3 |
|
4 |
| -.. note:: |
| 4 | +Read the Docs features a number of build notification mechanisms. |
| 5 | +Build notifications can alert you when your builds fail so you can take immediate action. |
5 | 6 |
|
6 |
| - Currently we don't send notifications or trigger webhooks |
7 |
| - on :doc:`builds from pull requests </pull-requests>`. |
| 7 | +Email notifications: |
| 8 | + Read the Docs allows you to configure emails that can be sent on failing builds. |
| 9 | + This makes sure you know when your builds have failed. |
8 | 10 |
|
9 |
| -Email notifications |
10 |
| -------------------- |
| 11 | +Build Status Webhooks: |
| 12 | + Build notifications can happen via webhooks. |
| 13 | + This means that we are able to support a wide variety of services that receive notifications. |
11 | 14 |
|
12 |
| -Read the Docs allows you to configure emails that can be sent on failing builds. |
13 |
| -This makes sure you know when your builds have failed. |
| 15 | + Slack and Discord are supported through ready-made templates. |
14 | 16 |
|
15 |
| -Take these steps to enable build notifications using email: |
| 17 | + Webhooks can be customized through your own template and a variety of variable substitutions. |
16 | 18 |
|
17 |
| -* Go to :guilabel:`Admin` > :guilabel:`Notifications` in your project. |
18 |
| -* Fill in the **Email** field under the **New Email Notifications** heading |
19 |
| -* Submit the form |
| 19 | +.. seealso:: |
| 20 | + :doc:`/guides/build-notifications` |
| 21 | + All the practical steps for setting up build notifications |
20 | 22 |
|
21 |
| -You should now get notified by email when your builds fail! |
22 |
| - |
23 |
| -Build Status Webhooks |
24 |
| ---------------------- |
25 |
| - |
26 |
| -Read the Docs can also send webhooks when builds are triggered, successful or failed. |
27 |
| - |
28 |
| -Take these steps to enable build notifications using a webhook: |
29 |
| - |
30 |
| -* Go to :guilabel:`Admin` > :guilabel:`Webhooks` in your project. |
31 |
| -* Fill in the **URL** field and select what events will trigger the webhook |
32 |
| -* Modify the payload or leave the default (see below) |
33 |
| -* Click on :guilabel:`Save` |
34 |
| - |
35 |
| -.. figure:: /_static/images/webhooks-events.png |
36 |
| - :align: center |
37 |
| - :alt: URL and events for a webhook |
38 |
| - |
39 |
| - URL and events for a webhook |
40 |
| - |
41 |
| -Every time one of the checked events triggers, |
42 |
| -Read the Docs will send a POST request to your webhook URL. |
43 |
| -The default payload will look like this: |
44 |
| - |
45 |
| -.. code-block:: json |
46 |
| -
|
47 |
| - { |
48 |
| - "event": "build:triggered", |
49 |
| - "name": "docs", |
50 |
| - "slug": "docs", |
51 |
| - "version": "latest", |
52 |
| - "commit": "2552bb609ca46865dc36401dee0b1865a0aee52d", |
53 |
| - "build": "15173336", |
54 |
| - "start_date": "2021-11-03T16:23:14", |
55 |
| - "build_url": "https://readthedocs.org/projects/docs/builds/15173336/", |
56 |
| - "docs_url": "https://docs.readthedocs.io/en/latest/" |
57 |
| - } |
58 |
| -
|
59 |
| -When a webhook is sent, a new entry will be added to the |
60 |
| -"Recent Activity" table. By clicking on each individual entry, |
61 |
| -you will see the server response, the webhook request, and the payload. |
62 |
| - |
63 |
| -.. figure:: /_static/images/webhooks-activity.png |
64 |
| - :align: center |
65 |
| - :alt: Activity of a webhook |
66 |
| - |
67 |
| - Activity of a webhook |
68 |
| - |
69 |
| -Custom payload examples |
70 |
| -~~~~~~~~~~~~~~~~~~~~~~~ |
71 |
| - |
72 |
| -You can customize the payload of the webhook to suit your needs, |
73 |
| -as long as it is valid JSON. Below you have a couple of examples, |
74 |
| -and in the following section you will find all the available variables. |
75 |
| - |
76 |
| -.. figure:: /_static/images/webhooks-payload.png |
77 |
| - :width: 80% |
78 |
| - :align: center |
79 |
| - :alt: Custom payload |
80 |
| - |
81 |
| - Custom payload |
82 |
| - |
83 |
| -Slack |
84 |
| -+++++ |
85 |
| - |
86 |
| -.. code-block:: json |
87 |
| -
|
88 |
| - { |
89 |
| - "attachments": [ |
90 |
| - { |
91 |
| - "color": "#db3238", |
92 |
| - "blocks": [ |
93 |
| - { |
94 |
| - "type": "section", |
95 |
| - "text": { |
96 |
| - "type": "mrkdwn", |
97 |
| - "text": "*Read the Docs build failed*" |
98 |
| - } |
99 |
| - }, |
100 |
| - { |
101 |
| - "type": "section", |
102 |
| - "fields": [ |
103 |
| - { |
104 |
| - "type": "mrkdwn", |
105 |
| - "text": "*Project*: <{{ project.url }}|{{ project.name }}>" |
106 |
| - }, |
107 |
| - { |
108 |
| - "type": "mrkdwn", |
109 |
| - "text": "*Version*: {{ version.name }} ({{ build.commit }})" |
110 |
| - }, |
111 |
| - { |
112 |
| - "type": "mrkdwn", |
113 |
| - "text": "*Build*: <{{ build.url }}|{{ build.id }}>" |
114 |
| - } |
115 |
| - ] |
116 |
| - } |
117 |
| - ] |
118 |
| - } |
119 |
| - ] |
120 |
| - } |
121 |
| -
|
122 |
| -More information on `the Slack Incoming Webhooks documentation <https://api.slack.com/messaging/webhooks>`_. |
123 |
| - |
124 |
| -Discord |
125 |
| -+++++++ |
126 |
| - |
127 |
| -.. code-block:: json |
128 |
| -
|
129 |
| - { |
130 |
| - "username": "Read the Docs", |
131 |
| - "content": "Read the Docs build failed", |
132 |
| - "embeds": [ |
133 |
| - { |
134 |
| - "title": "Build logs", |
135 |
| - "url": "{{ build.url }}", |
136 |
| - "color": 15258703, |
137 |
| - "fields": [ |
138 |
| - { |
139 |
| - "name": "*Project*", |
140 |
| - "value": "{{ project.url }}", |
141 |
| - "inline": true |
142 |
| - }, |
143 |
| - { |
144 |
| - "name": "*Version*", |
145 |
| - "value": "{{ version.name }} ({{ build.commit }})", |
146 |
| - "inline": true |
147 |
| - }, |
148 |
| - { |
149 |
| - "name": "*Build*", |
150 |
| - "value": "{{ build.url }}" |
151 |
| - } |
152 |
| - ] |
153 |
| - } |
154 |
| - ] |
155 |
| - } |
156 |
| -
|
157 |
| -More information on `the Discord webhooks documentation <https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks>`_. |
158 |
| - |
159 |
| -Variable substitutions reference |
160 |
| -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
161 |
| - |
162 |
| -``{{ event }}`` |
163 |
| - Event that triggered the webhook, one of ``build:triggered``, ``build:failed``, or ``build:passed``. |
164 |
| - |
165 |
| -``{{ build.id }}`` |
166 |
| - Build ID. |
167 |
| - |
168 |
| -``{{ build.commit }}`` |
169 |
| - Commit corresponding to the build, if present (otherwise ``""``). |
170 |
| - |
171 |
| -``{{ build.url }}`` |
172 |
| - URL of the build, for example ``https://readthedocs.org/projects/docs/builds/15173336/``. |
173 |
| - |
174 |
| -``{{ build.docs_url }}`` |
175 |
| - URL of the documentation corresponding to the build, |
176 |
| - for example ``https://docs.readthedocs.io/en/latest/``. |
177 |
| - |
178 |
| -``{{ build.start_date }}`` |
179 |
| - Start date of the build (UTC, ISO format), for example ``2021-11-03T16:23:14``. |
180 |
| - |
181 |
| -``{{ organization.name }}`` |
182 |
| - Organization name (Commercial only). |
183 |
| - |
184 |
| -``{{ organization.slug }}`` |
185 |
| - Organization slug (Commercial only). |
186 |
| - |
187 |
| -``{{ project.slug }}`` |
188 |
| - Project slug. |
189 |
| - |
190 |
| -``{{ project.name }}`` |
191 |
| - Project name. |
192 |
| - |
193 |
| -``{{ project.url }}`` |
194 |
| - URL of the project :term:`dashboard`. |
195 |
| - |
196 |
| -``{{ version.slug }}`` |
197 |
| - Version slug. |
198 |
| - |
199 |
| -``{{ version.name }}`` |
200 |
| - Version name. |
201 |
| - |
202 |
| -Validating the payload |
203 |
| -~~~~~~~~~~~~~~~~~~~~~~ |
204 |
| - |
205 |
| -After you add a new webhook, Read the Docs will generate a secret key for it |
206 |
| -and uses it to generate a hash signature (HMAC-SHA256) for each payload |
207 |
| -that is included in the ``X-Hub-Signature`` header of the request. |
208 |
| - |
209 |
| -.. figure:: /_static/images/webhooks-secret.png |
210 |
| - :width: 80% |
211 |
| - :align: center |
212 |
| - :alt: Webhook secret |
213 |
| - |
214 |
| - Webhook secret |
215 |
| - |
216 |
| -We highly recommend using this signature |
217 |
| -to verify that the webhook is coming from Read the Docs. |
218 |
| -To do so, you can add some custom code on your server, |
219 |
| -like this: |
220 |
| - |
221 |
| -.. code-block:: python |
222 |
| -
|
223 |
| - import hashlib |
224 |
| - import hmac |
225 |
| - import os |
226 |
| -
|
227 |
| -
|
228 |
| - def verify_signature(payload, request_headers): |
229 |
| - """ |
230 |
| - Verify that the signature of payload is the same as the one coming from request_headers. |
231 |
| - """ |
232 |
| - digest = hmac.new( |
233 |
| - key=os.environ["WEBHOOK_SECRET"].encode(), |
234 |
| - msg=payload.encode(), |
235 |
| - digestmod=hashlib.sha256, |
236 |
| - ) |
237 |
| - expected_signature = digest.hexdigest() |
238 |
| -
|
239 |
| - return hmac.compare_digest( |
240 |
| - request_headers["X-Hub-Signature"].encode(), |
241 |
| - expected_signature.encode(), |
242 |
| - ) |
243 |
| -
|
244 |
| -Legacy webhooks |
245 |
| -~~~~~~~~~~~~~~~ |
246 |
| - |
247 |
| -Webhooks created before the custom payloads functionality was added to Read the Docs |
248 |
| -send a payload with the following structure: |
249 |
| - |
250 |
| -.. code-block:: json |
251 |
| -
|
252 |
| - { |
253 |
| - "name": "Read the Docs", |
254 |
| - "slug": "rtd", |
255 |
| - "build": { |
256 |
| - "id": 6321373, |
257 |
| - "commit": "e8dd17a3f1627dd206d721e4be08ae6766fda40", |
258 |
| - "state": "finished", |
259 |
| - "success": false, |
260 |
| - "date": "2017-02-15 20:35:54" |
261 |
| - } |
262 |
| - } |
263 |
| -
|
264 |
| -To migrate to the new webhooks and keep a similar structure, |
265 |
| -you can use this payload: |
266 |
| - |
267 |
| -.. code-block:: json |
268 |
| -
|
269 |
| - { |
270 |
| - "name": "{{ project.name }}", |
271 |
| - "slug": "{{ project.slug }}", |
272 |
| - "build": { |
273 |
| - "id": "{{ build.id }}", |
274 |
| - "commit": "{{ build.commit }}", |
275 |
| - "state": "{{ event }}", |
276 |
| - "date": "{{ build.start_date }}" |
277 |
| - } |
278 |
| - } |
| 23 | + :doc:`pull-requests` |
| 24 | + Similarly to build notifications, you can also configure automated feedback for your pull requests. |
0 commit comments