onwebhook.adoc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. [#exec-webhook]
  2. = Execute on webhook
  3. Webhooks allow external services to trigger OliveTin actions by sending HTTP POST requests. This is useful for integrating OliveTin with CI/CD pipelines, monitoring systems, IoT devices, or any service that can send HTTP requests.
  4. OliveTin provides a dedicated webhook endpoint at `/webhooks` that can receive webhook payloads and match them to configured actions.
  5. == Basic Configuration
  6. To configure an action to run on a webhook, add the `execOnWebhook` property to your action. Webhook-triggered actions **must** use `exec:` (not `shell:` or `shellAfterCompleted`).
  7. [source,yaml]
  8. .`config.yaml`
  9. ----
  10. actions:
  11. - title: Deploy Application
  12. id: deploy
  13. exec:
  14. - /opt/scripts/deploy.sh
  15. execOnWebhook:
  16. - matchHeaders:
  17. X-Event-Type: deploy
  18. ----
  19. This action will be triggered when a POST request is sent to `/webhooks` with the header `X-Event-Type: deploy`.
  20. == Webhook Endpoint
  21. All webhooks are received at:
  22. ----
  23. http://your-olivetin-server:1337/webhooks
  24. ----
  25. or
  26. ----
  27. http://your-olivetin-server:1337/webhooks/
  28. ----
  29. Both paths work identically. All webhook requests must use the HTTP POST method.
  30. == Matching Webhooks
  31. OliveTin can match incoming webhooks based on several criteria:
  32. === Match by Headers
  33. Match webhooks based on HTTP header values:
  34. [source,yaml]
  35. ----
  36. actions:
  37. - title: Process Event
  38. exec:
  39. - echo
  40. - "Processing event"
  41. execOnWebhook:
  42. - matchHeaders:
  43. X-Event-Type: my-event
  44. X-Source: my-service
  45. ----
  46. All specified headers must match for the webhook to trigger the action.
  47. === Match by Query Parameters
  48. Match webhooks based on URL query parameters:
  49. [source,yaml]
  50. ----
  51. actions:
  52. - title: Process Request
  53. exec:
  54. - echo
  55. - "Processing request for {{ service }}"
  56. arguments:
  57. - name: service
  58. type: ascii
  59. execOnWebhook:
  60. - matchQuery:
  61. action: deploy
  62. env: production
  63. ----
  64. A request to `/webhooks?action=deploy&env=production` would match this action.
  65. === Match by JSON Body Path
  66. Match webhooks based on values in the JSON request body using JSONPath expressions:
  67. [source,yaml]
  68. ----
  69. actions:
  70. - title: Handle Push Event
  71. exec:
  72. - echo
  73. - "Push to {{ branch }}"
  74. arguments:
  75. - name: branch
  76. type: ascii
  77. execOnWebhook:
  78. - matchPath: "$.event_type=push"
  79. ----
  80. The `matchPath` format is `jsonpath=value`. You can also just specify a JSONPath without a value to match if the path exists:
  81. [source,yaml]
  82. ----
  83. execOnWebhook:
  84. - matchPath: "$.repository.name" # Matches if this path exists in the JSON
  85. ----
  86. === Using Regex for Matching
  87. Header and query parameter values can use regex patterns by prefixing with `regex:`:
  88. [source,yaml]
  89. ----
  90. actions:
  91. - title: Handle Multiple Events
  92. exec:
  93. - echo
  94. - "Handling event"
  95. execOnWebhook:
  96. - matchHeaders:
  97. X-Event-Type: "regex:^(push|pull_request|release)$"
  98. ----
  99. === Combining Match Criteria
  100. You can combine multiple match criteria. All criteria must match for the webhook to trigger:
  101. [source,yaml]
  102. ----
  103. actions:
  104. - title: Production Deploy
  105. exec:
  106. - /opt/scripts/deploy.sh
  107. - production
  108. execOnWebhook:
  109. - matchHeaders:
  110. X-Event-Type: deploy
  111. matchQuery:
  112. environment: production
  113. matchPath: "$.status=approved"
  114. ----
  115. == Extracting Arguments from Webhooks
  116. You can extract values from the webhook payload and pass them as arguments to your action using JSONPath expressions:
  117. [source,yaml]
  118. ----
  119. actions:
  120. - title: Deploy Version
  121. exec:
  122. - /opt/scripts/deploy.sh
  123. - "{{ version }}"
  124. - "{{ environment }}"
  125. arguments:
  126. - name: version
  127. type: ascii
  128. - name: environment
  129. type: ascii
  130. execOnWebhook:
  131. - matchHeaders:
  132. X-Event-Type: deploy
  133. extract:
  134. version: "$.release.tag_name"
  135. environment: "$.target.environment"
  136. ----
  137. The `extract` map defines which action arguments to populate from the webhook payload. The key is the argument name, and the value is the JSONPath expression to extract the value.
  138. === Automatic Webhook Metadata
  139. OliveTin automatically adds several metadata arguments from each webhook request:
  140. * `webhook_method` - The HTTP method (always POST for webhooks)
  141. * `webhook_path` - The request URL path
  142. * `webhook_query` - The raw query string
  143. * `webhook_header_<name>` - Each HTTP header (lowercase name)
  144. For example, to access the `X-Request-Id` header in your action:
  145. [source,yaml]
  146. ----
  147. actions:
  148. - title: Log Request
  149. exec:
  150. - echo
  151. - "Request ID: {{ webhook_header_x-request-id }}"
  152. arguments:
  153. - name: webhook_header_x-request-id
  154. type: ascii
  155. execOnWebhook:
  156. - matchHeaders:
  157. X-Event-Type: log
  158. ----
  159. == Webhook Authentication
  160. OliveTin supports several authentication methods to verify webhook requests:
  161. === No Authentication
  162. By default, webhooks have no authentication. Any request matching the criteria will trigger the action:
  163. [source,yaml]
  164. ----
  165. execOnWebhook:
  166. - authType: none
  167. matchHeaders:
  168. X-Event-Type: my-event
  169. ----
  170. === HMAC-SHA256 Signature
  171. Verify webhooks using HMAC-SHA256 signatures (commonly used by GitHub, GitLab, etc.):
  172. [source,yaml]
  173. ----
  174. execOnWebhook:
  175. - authType: hmac-sha256
  176. authHeader: X-Hub-Signature-256
  177. secret: your-webhook-secret
  178. matchHeaders:
  179. X-Event-Type: push
  180. ----
  181. The `authHeader` specifies which header contains the signature. The signature should be in the format `sha256=<hex-encoded-signature>`.
  182. === HMAC-SHA1 Signature
  183. For services using HMAC-SHA1 (legacy GitHub webhooks):
  184. [source,yaml]
  185. ----
  186. execOnWebhook:
  187. - authType: hmac-sha1
  188. authHeader: X-Hub-Signature
  189. secret: your-webhook-secret
  190. matchHeaders:
  191. X-Event-Type: push
  192. ----
  193. === Bearer Token
  194. Verify webhooks using a Bearer token in the Authorization header:
  195. [source,yaml]
  196. ----
  197. execOnWebhook:
  198. - authType: bearer
  199. secret: your-bearer-token
  200. matchHeaders:
  201. X-Event-Type: deploy
  202. ----
  203. The webhook sender must include `Authorization: Bearer your-bearer-token` in the request.
  204. === Basic Authentication
  205. Verify webhooks using HTTP Basic authentication:
  206. [source,yaml]
  207. ----
  208. execOnWebhook:
  209. - authType: basic
  210. secret: "username:password"
  211. matchHeaders:
  212. X-Event-Type: deploy
  213. ----
  214. Or with password only:
  215. [source,yaml]
  216. ----
  217. execOnWebhook:
  218. - authType: basic
  219. secret: "mypassword"
  220. matchHeaders:
  221. X-Event-Type: deploy
  222. ----
  223. == Multiple Webhook Triggers
  224. An action can have multiple webhook configurations. The action will be triggered if any of them match:
  225. [source,yaml]
  226. ----
  227. actions:
  228. - title: Deploy
  229. exec:
  230. - /opt/scripts/deploy.sh
  231. execOnWebhook:
  232. - matchHeaders:
  233. X-Event-Type: deploy-manual
  234. - matchHeaders:
  235. X-Event-Type: deploy-auto
  236. matchPath: "$.status=success"
  237. ----
  238. == Testing Webhooks
  239. You can test your webhook configuration using `curl`:
  240. [source,bash]
  241. ----
  242. # Simple webhook with headers
  243. curl -X POST \
  244. -H "Content-Type: application/json" \
  245. -H "X-Event-Type: deploy" \
  246. -d '{"version": "1.2.3"}' \
  247. http://localhost:1337/webhooks
  248. # Webhook with HMAC-SHA256 authentication
  249. SECRET="your-secret"
  250. PAYLOAD='{"event": "push", "branch": "main"}'
  251. SIGNATURE=$(echo -n "$PAYLOAD" | openssl dgst -sha256 -hmac "$SECRET" | cut -d' ' -f2)
  252. curl -X POST \
  253. -H "Content-Type: application/json" \
  254. -H "X-Event-Type: push" \
  255. -H "X-Hub-Signature-256: sha256=$SIGNATURE" \
  256. -d "$PAYLOAD" \
  257. http://localhost:1337/webhooks
  258. ----
  259. == See Also
  260. * xref:action_execution/onwebhook_github.adoc[GitHub Webhooks] - Specific configuration for GitHub webhook events
  261. * xref:solutions/on-git-push/index.adoc[GitOps Solution] - Running actions on Git push using hooks
  262. * xref:api/start_action.adoc[Start Action API] - Alternative method for triggering actions via API