[#exec-webhook] = Execute on webhook 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. OliveTin provides a dedicated webhook endpoint at `/webhooks` that can receive webhook payloads and match them to configured actions. == Basic Configuration 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]). [source,yaml] .`config.yaml` ---- actions: - title: Deploy Application id: deploy exec: - /opt/scripts/deploy.sh execOnWebhook: - authType: bearer secret: "${{ WEBHOOK_TOKEN }}" matchHeaders: X-Event-Type: deploy ---- 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 `, where the token is the value of the `WEBHOOK_TOKEN` environment variable. Every webhook needs an `authType`; see <>. == Webhook Endpoint All webhooks are received at: ---- http://your-olivetin-server:1337/webhooks ---- or ---- http://your-olivetin-server:1337/webhooks/ ---- Both paths work identically. All webhook requests must use the HTTP POST method. == Matching Webhooks OliveTin can match incoming webhooks based on several criteria: === Match by Headers Match webhooks based on HTTP header values: [source,yaml] ---- actions: - title: Process Event exec: - echo - "Processing event" execOnWebhook: - authType: bearer secret: "${{ WEBHOOK_TOKEN }}" matchHeaders: X-Event-Type: my-event X-Source: my-service ---- All specified headers must match for the webhook to trigger the action. === Match by Query Parameters Match webhooks based on URL query parameters: [source,yaml] ---- actions: - title: Process Request exec: - echo - "Processing request for {{ service }}" arguments: - name: service type: ascii execOnWebhook: - authType: bearer secret: "${{ WEBHOOK_TOKEN }}" matchQuery: action: deploy env: production extract: service: "$.service" ---- 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. === Match by JSON Body Path Match webhooks based on values in the JSON request body using JSONPath expressions: [source,yaml] ---- actions: - title: Handle Push Event exec: - echo - "Push to {{ branch }}" arguments: - name: branch type: ascii execOnWebhook: - authType: bearer secret: "${{ WEBHOOK_TOKEN }}" matchPath: "$.event_type=push" extract: branch: "$.branch" ---- The `matchPath` format is `jsonpath=value`. You can also just specify a JSONPath without a value to match if the path exists: [source,yaml] ---- execOnWebhook: - authType: bearer secret: "${{ WEBHOOK_TOKEN }}" matchPath: "$.repository.name" # Matches if this path exists in the JSON ---- === Using Regex for Matching Header and query parameter values can use regex patterns by prefixing with `regex:`: [source,yaml] ---- actions: - title: Handle Multiple Events exec: - echo - "Handling event" execOnWebhook: - authType: bearer secret: "${{ WEBHOOK_TOKEN }}" matchHeaders: X-Event-Type: "regex:^(push|pull_request|release)$" ---- === Combining Match Criteria You can combine multiple match criteria. All criteria must match for the webhook to trigger: [source,yaml] ---- actions: - title: Production Deploy exec: - /opt/scripts/deploy.sh - production execOnWebhook: - authType: bearer secret: "${{ WEBHOOK_TOKEN }}" matchHeaders: X-Event-Type: deploy matchQuery: environment: production matchPath: "$.status=approved" ---- == Extracting Arguments from Webhooks You can extract values from the webhook payload and pass them as arguments to your action using JSONPath expressions: [source,yaml] ---- actions: - title: Deploy Version exec: - /opt/scripts/deploy.sh - "{{ version }}" - "{{ environment }}" arguments: - name: version type: ascii - name: environment type: ascii execOnWebhook: - authType: bearer secret: "${{ WEBHOOK_TOKEN }}" matchHeaders: X-Event-Type: deploy extract: version: "$.release.tag_name" environment: "$.target.environment" ---- 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. === Automatic Webhook Metadata OliveTin automatically adds several metadata arguments from each webhook request: * `webhook_method` - The HTTP method (always POST for webhooks) * `webhook_path` - The request URL path * `webhook_query` - The raw query string * `webhook_header_` - Each HTTP header (lowercase name) For example, to access the `X-Request-Id` header in your action: [source,yaml] ---- actions: - title: Log Request exec: - echo - 'Request ID: {{ index .Arguments "webhook_header_x-request-id" }}' arguments: - name: webhook_header_x-request-id type: ascii execOnWebhook: - authType: bearer secret: "${{ WEBHOOK_TOKEN }}" matchHeaders: X-Event-Type: log ---- 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. These metadata arguments come from the request's path, query string and headers, which HMAC signatures do not cover. Do not rely on them for values an action needs to trust; see <>. == Webhook Authentication OliveTin supports several authentication methods to verify webhook requests: [#every-webhook-needs-an-authtype] === Every webhook needs an `authType` 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]. `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]). === No Authentication 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: [source,yaml] ---- execOnWebhook: - authType: none matchHeaders: X-Event-Type: my-event ---- === HMAC-SHA256 Signature Verify webhooks using HMAC-SHA256 signatures (commonly used by GitHub, GitLab, etc.): [source,yaml] ---- execOnWebhook: - authType: hmac-sha256 authHeader: X-Hub-Signature-256 secret: your-webhook-secret matchHeaders: X-Event-Type: push ---- The `authHeader` specifies which header contains the signature. The signature should be in the format `sha256=`. === HMAC-SHA1 Signature For services using HMAC-SHA1 (legacy GitHub webhooks): [source,yaml] ---- execOnWebhook: - authType: hmac-sha1 authHeader: X-Hub-Signature secret: your-webhook-secret matchHeaders: X-Event-Type: push ---- [#what-hmac-signatures-cover] === What HMAC signatures cover HMAC signatures, including the ones checked for the GitHub `template` webhooks, are worked out by the sender over the request body only. This is how GitHub, GitLab and similar services sign webhooks, so OliveTin cannot check anything more than the sender signed: * The request path, query string and headers are not signed. Anyone who has seen one signed request can send it again with a different path, query string or headers, and it will still pass the signature check. * There is no timestamp or nonce, so a signed request can be sent again, and it will run the action again. To keep webhooks safe: * Send webhooks to OliveTin over HTTPS, so that signed requests cannot be read or changed on the way. * Take any value that an action needs to trust from the request body, with `extract`. Do not use the `webhook_path`, `webhook_query` or `webhook_header_*` arguments for those values with HMAC webhooks. The xref:advanced_configuration/diagnostics.adoc[Diagnostics] page shows a warning (code `webhook_unsigned_argument`) for an action that does. * Make webhook-triggered actions safe to run more than once. `bearer` and `basic` authentication do not sign anything: they check that the request carries the secret. Anyone who has the secret can send any request. === Bearer Token Verify webhooks using a Bearer token in the Authorization header: [source,yaml] ---- execOnWebhook: - authType: bearer secret: your-bearer-token matchHeaders: X-Event-Type: deploy ---- The webhook sender must include `Authorization: Bearer your-bearer-token` in the request. === Basic Authentication Verify webhooks using HTTP Basic authentication: [source,yaml] ---- execOnWebhook: - authType: basic secret: "username:password" matchHeaders: X-Event-Type: deploy ---- Or with password only: [source,yaml] ---- execOnWebhook: - authType: basic secret: "mypassword" matchHeaders: X-Event-Type: deploy ---- == Multiple Webhook Triggers An action can have multiple webhook configurations. The action will be triggered if any of them match: [source,yaml] ---- actions: - title: Deploy exec: - /opt/scripts/deploy.sh execOnWebhook: - authType: bearer secret: "${{ WEBHOOK_TOKEN }}" matchHeaders: X-Event-Type: deploy-manual - authType: bearer secret: "${{ WEBHOOK_TOKEN }}" matchHeaders: X-Event-Type: deploy-auto matchPath: "$.status=success" ---- == Testing Webhooks You can test your webhook configuration using `curl`: [source,bash] ---- # Simple webhook with headers and a bearer token curl -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $WEBHOOK_TOKEN" \ -H "X-Event-Type: deploy" \ -d '{"version": "1.2.3"}' \ http://localhost:1337/webhooks # Webhook with HMAC-SHA256 authentication SECRET="your-secret" PAYLOAD='{"event": "push", "branch": "main"}' SIGNATURE=$(echo -n "$PAYLOAD" | openssl dgst -sha256 -hmac "$SECRET" | cut -d' ' -f2) curl -X POST \ -H "Content-Type: application/json" \ -H "X-Event-Type: push" \ -H "X-Hub-Signature-256: sha256=$SIGNATURE" \ -d "$PAYLOAD" \ http://localhost:1337/webhooks ---- == See Also * xref:action_execution/onwebhook_github.adoc[GitHub Webhooks] - Specific configuration for GitHub webhook events * xref:solutions/on-git-push/index.adoc[GitOps Solution] - Running actions on Git push using hooks * xref:api/start_action.adoc[Start Action API] - Alternative method for triggering actions via API