onwebhook.adoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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`). This also applies to any action started through `triggers` from a webhook-triggered action, at any depth of the trigger chain (see xref:upgrade/upgrade_notes.adoc#webhook-trigger-chains-exec-only[Upgrade Notes]).
  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. - authType: bearer
  17. secret: "${{ WEBHOOK_TOKEN }}"
  18. matchHeaders:
  19. X-Event-Type: deploy
  20. ----
  21. This action will be triggered when a POST request is sent to `/webhooks` with the header `X-Event-Type: deploy` and the header `Authorization: Bearer <token>`, where the token is the value of the `WEBHOOK_TOKEN` environment variable. Every webhook needs an `authType`; see <<every-webhook-needs-an-authtype,Webhook Authentication>>.
  22. == Webhook Endpoint
  23. All webhooks are received at:
  24. ----
  25. http://your-olivetin-server:1337/webhooks
  26. ----
  27. or
  28. ----
  29. http://your-olivetin-server:1337/webhooks/
  30. ----
  31. Both paths work identically. All webhook requests must use the HTTP POST method.
  32. == Matching Webhooks
  33. OliveTin can match incoming webhooks based on several criteria:
  34. === Match by Headers
  35. Match webhooks based on HTTP header values:
  36. [source,yaml]
  37. ----
  38. actions:
  39. - title: Process Event
  40. exec:
  41. - echo
  42. - "Processing event"
  43. execOnWebhook:
  44. - authType: bearer
  45. secret: "${{ WEBHOOK_TOKEN }}"
  46. matchHeaders:
  47. X-Event-Type: my-event
  48. X-Source: my-service
  49. ----
  50. All specified headers must match for the webhook to trigger the action.
  51. === Match by Query Parameters
  52. Match webhooks based on URL query parameters:
  53. [source,yaml]
  54. ----
  55. actions:
  56. - title: Process Request
  57. exec:
  58. - echo
  59. - "Processing request for {{ service }}"
  60. arguments:
  61. - name: service
  62. type: ascii
  63. execOnWebhook:
  64. - authType: bearer
  65. secret: "${{ WEBHOOK_TOKEN }}"
  66. matchQuery:
  67. action: deploy
  68. env: production
  69. extract:
  70. service: "$.service"
  71. ----
  72. A request to `/webhooks?action=deploy&env=production` with a JSON body containing `"service"` would match this action and pass that field into the `service` argument.
  73. === Match by JSON Body Path
  74. Match webhooks based on values in the JSON request body using JSONPath expressions:
  75. [source,yaml]
  76. ----
  77. actions:
  78. - title: Handle Push Event
  79. exec:
  80. - echo
  81. - "Push to {{ branch }}"
  82. arguments:
  83. - name: branch
  84. type: ascii
  85. execOnWebhook:
  86. - authType: bearer
  87. secret: "${{ WEBHOOK_TOKEN }}"
  88. matchPath: "$.event_type=push"
  89. extract:
  90. branch: "$.branch"
  91. ----
  92. The `matchPath` format is `jsonpath=value`. You can also just specify a JSONPath without a value to match if the path exists:
  93. [source,yaml]
  94. ----
  95. execOnWebhook:
  96. - authType: bearer
  97. secret: "${{ WEBHOOK_TOKEN }}"
  98. matchPath: "$.repository.name" # Matches if this path exists in the JSON
  99. ----
  100. === Using Regex for Matching
  101. Header and query parameter values can use regex patterns by prefixing with `regex:`:
  102. [source,yaml]
  103. ----
  104. actions:
  105. - title: Handle Multiple Events
  106. exec:
  107. - echo
  108. - "Handling event"
  109. execOnWebhook:
  110. - authType: bearer
  111. secret: "${{ WEBHOOK_TOKEN }}"
  112. matchHeaders:
  113. X-Event-Type: "regex:^(push|pull_request|release)$"
  114. ----
  115. === Combining Match Criteria
  116. You can combine multiple match criteria. All criteria must match for the webhook to trigger:
  117. [source,yaml]
  118. ----
  119. actions:
  120. - title: Production Deploy
  121. exec:
  122. - /opt/scripts/deploy.sh
  123. - production
  124. execOnWebhook:
  125. - authType: bearer
  126. secret: "${{ WEBHOOK_TOKEN }}"
  127. matchHeaders:
  128. X-Event-Type: deploy
  129. matchQuery:
  130. environment: production
  131. matchPath: "$.status=approved"
  132. ----
  133. == Extracting Arguments from Webhooks
  134. You can extract values from the webhook payload and pass them as arguments to your action using JSONPath expressions:
  135. [source,yaml]
  136. ----
  137. actions:
  138. - title: Deploy Version
  139. exec:
  140. - /opt/scripts/deploy.sh
  141. - "{{ version }}"
  142. - "{{ environment }}"
  143. arguments:
  144. - name: version
  145. type: ascii
  146. - name: environment
  147. type: ascii
  148. execOnWebhook:
  149. - authType: bearer
  150. secret: "${{ WEBHOOK_TOKEN }}"
  151. matchHeaders:
  152. X-Event-Type: deploy
  153. extract:
  154. version: "$.release.tag_name"
  155. environment: "$.target.environment"
  156. ----
  157. 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.
  158. === Automatic Webhook Metadata
  159. OliveTin automatically adds several metadata arguments from each webhook request:
  160. * `webhook_method` - The HTTP method (always POST for webhooks)
  161. * `webhook_path` - The request URL path
  162. * `webhook_query` - The raw query string
  163. * `webhook_header_<name>` - Each HTTP header (lowercase name)
  164. For example, to access the `X-Request-Id` header in your action:
  165. [source,yaml]
  166. ----
  167. actions:
  168. - title: Log Request
  169. exec:
  170. - echo
  171. - 'Request ID: {{ index .Arguments "webhook_header_x-request-id" }}'
  172. arguments:
  173. - name: webhook_header_x-request-id
  174. type: ascii
  175. execOnWebhook:
  176. - authType: bearer
  177. secret: "${{ WEBHOOK_TOKEN }}"
  178. matchHeaders:
  179. X-Event-Type: log
  180. ----
  181. Header names that contain hyphens become argument keys with the same hyphens (for example `webhook_header_x-request-id`). Use the `index` map-lookup form shown above, because Go templates treat hyphens in bare identifiers as subtraction.
  182. == Webhook Authentication
  183. OliveTin supports several authentication methods to verify webhook requests:
  184. [#every-webhook-needs-an-authtype]
  185. === Every webhook needs an `authType`
  186. Every webhook must set an `authType`. A webhook without one is rejected, and the xref:advanced_configuration/diagnostics.adoc[Diagnostics] page shows an error for it (code `webhook_auth_type_unset`). Webhooks that use one of the xref:action_execution/onwebhook_github.adoc[GitHub templates] choose their own `authType`. A `template` name that OliveTin does not know is ignored, and Diagnostics shows an error for it (code `webhook_template_unknown`). See xref:upgrade/upgrade_notes.adoc#webhook-auth-type-required[Upgrade Notes].
  187. `matchHeaders`, `matchQuery` and `matchPath` only decide which action a request is for. They are not secrets, so use one of the authentication methods below unless anyone who can reach `/webhooks` may run the action. Webhook-triggered actions run as the `system` usergroup, not as the caller (see xref:security/acl.adoc#acls-for-triggers[ACLs for triggers]).
  188. === No Authentication
  189. To accept webhook requests without any authentication, set `authType: none` explicitly. Any request that matches will then trigger the action, so only do this for actions that are safe for anyone to run:
  190. [source,yaml]
  191. ----
  192. execOnWebhook:
  193. - authType: none
  194. matchHeaders:
  195. X-Event-Type: my-event
  196. ----
  197. === HMAC-SHA256 Signature
  198. Verify webhooks using HMAC-SHA256 signatures (commonly used by GitHub, GitLab, etc.):
  199. [source,yaml]
  200. ----
  201. execOnWebhook:
  202. - authType: hmac-sha256
  203. authHeader: X-Hub-Signature-256
  204. secret: your-webhook-secret
  205. matchHeaders:
  206. X-Event-Type: push
  207. ----
  208. The `authHeader` specifies which header contains the signature. The signature should be in the format `sha256=<hex-encoded-signature>`.
  209. === HMAC-SHA1 Signature
  210. For services using HMAC-SHA1 (legacy GitHub webhooks):
  211. [source,yaml]
  212. ----
  213. execOnWebhook:
  214. - authType: hmac-sha1
  215. authHeader: X-Hub-Signature
  216. secret: your-webhook-secret
  217. matchHeaders:
  218. X-Event-Type: push
  219. ----
  220. === Bearer Token
  221. Verify webhooks using a Bearer token in the Authorization header:
  222. [source,yaml]
  223. ----
  224. execOnWebhook:
  225. - authType: bearer
  226. secret: your-bearer-token
  227. matchHeaders:
  228. X-Event-Type: deploy
  229. ----
  230. The webhook sender must include `Authorization: Bearer your-bearer-token` in the request.
  231. === Basic Authentication
  232. Verify webhooks using HTTP Basic authentication:
  233. [source,yaml]
  234. ----
  235. execOnWebhook:
  236. - authType: basic
  237. secret: "username:password"
  238. matchHeaders:
  239. X-Event-Type: deploy
  240. ----
  241. Or with password only:
  242. [source,yaml]
  243. ----
  244. execOnWebhook:
  245. - authType: basic
  246. secret: "mypassword"
  247. matchHeaders:
  248. X-Event-Type: deploy
  249. ----
  250. == Multiple Webhook Triggers
  251. An action can have multiple webhook configurations. The action will be triggered if any of them match:
  252. [source,yaml]
  253. ----
  254. actions:
  255. - title: Deploy
  256. exec:
  257. - /opt/scripts/deploy.sh
  258. execOnWebhook:
  259. - authType: bearer
  260. secret: "${{ WEBHOOK_TOKEN }}"
  261. matchHeaders:
  262. X-Event-Type: deploy-manual
  263. - authType: bearer
  264. secret: "${{ WEBHOOK_TOKEN }}"
  265. matchHeaders:
  266. X-Event-Type: deploy-auto
  267. matchPath: "$.status=success"
  268. ----
  269. == Testing Webhooks
  270. You can test your webhook configuration using `curl`:
  271. [source,bash]
  272. ----
  273. # Simple webhook with headers and a bearer token
  274. curl -X POST \
  275. -H "Content-Type: application/json" \
  276. -H "Authorization: Bearer $WEBHOOK_TOKEN" \
  277. -H "X-Event-Type: deploy" \
  278. -d '{"version": "1.2.3"}' \
  279. http://localhost:1337/webhooks
  280. # Webhook with HMAC-SHA256 authentication
  281. SECRET="your-secret"
  282. PAYLOAD='{"event": "push", "branch": "main"}'
  283. SIGNATURE=$(echo -n "$PAYLOAD" | openssl dgst -sha256 -hmac "$SECRET" | cut -d' ' -f2)
  284. curl -X POST \
  285. -H "Content-Type: application/json" \
  286. -H "X-Event-Type: push" \
  287. -H "X-Hub-Signature-256: sha256=$SIGNATURE" \
  288. -d "$PAYLOAD" \
  289. http://localhost:1337/webhooks
  290. ----
  291. == See Also
  292. * xref:action_execution/onwebhook_github.adoc[GitHub Webhooks] - Specific configuration for GitHub webhook events
  293. * xref:solutions/on-git-push/index.adoc[GitOps Solution] - Running actions on Git push using hooks
  294. * xref:api/start_action.adoc[Start Action API] - Alternative method for triggering actions via API