diff --git a/docs/docs/articles/webhooks.mdx b/docs/docs/articles/webhooks.mdx index 77cfe991eb2..5b099133724 100644 --- a/docs/docs/articles/webhooks.mdx +++ b/docs/docs/articles/webhooks.mdx @@ -5,13 +5,134 @@ import TabItem from "@theme/TabItem"; Webhooks allow you to integrate Testkube with external systems by sending HTTP POST payloads containing information about Testkube executions and their current state when specific events occur. To set up webhooks in Testkube, you'll need to have an HTTPS endpoint to receive the events and a payload template to be sent along with the data. -Here's an example format for creating a webhook in Testkube using either the CLI or custom response: +## Creating a Webhook +The webhook can be created using the Dashboard, CLI, or a Custom Resource. + +If you prefer to use the Dashboard, you can view existing webhooks by going to the Webhooks tab. + +![Dashboard menu - webhooks icon](../img/dashboard-webhooks-icon.png) + +Here you can also create a new webhook by clicking the `Create a new webhook` button. + +Then, fill in the webhook details: +![Dashboard webhook - create dialog 1](../img/dashboard-create-webhook-1.png) +- Name - your webhook name (in this case `example-webhook`) +- Resource identifier - the resource (or resources) selected by `label` for which the webhook can be triggered (in the example: `test-type:postman-collection` - any postman test) +- Triggered events - events that will trigger the webhook (in this case `start-test`, `end-test-success`, and `end-test-failed`). All available trigger events can be found in the [Supported Event types](#supported-event-types) section. + +![Dashboard webhook - create dialog 2](../img/dashboard-create-webhook-2.png) + +Set your webhook URI - the HTTPS endpoint where you want to receive the webhook events. +After the webhook is created, the custom payload and headers can be set in Settings->Action. + + + + +Webhooks can be created with Testkube CLI using the `create webhook` command. + +```sh +testkube create webhook --name example-webhook --events start-test --events end-test-success --events end-test-failed --uri +``` +`--name` - Your webhook name (in this case `example-webhook`). +`--events` - Event that will trigger a webhook. Multiple `--events` can be defined (in this case `--events start-test --events end-test-success --events end-test-failed`). All available trigger events can be found in the [Supported Event types](#supported-event-types) section. +`--uri` - The HTTPS endpoint where you want to receive the webhook events. + + + + + +```yaml title="webhook.yaml" +apiVersion: executor.testkube.io/v1 +kind: Webhook +metadata: + name: example-webhook + namespace: testkube +spec: + uri: + events: + - start-test + - end-test-success + - end-test-failed + selector: "" +``` +Where should be replaced with the HTTPS endpoint URL where you want to receive the webhook events. + +And then apply with: + +```sh +kubectl apply -f webhook.yaml +``` + + + + + +### Resource Selector (labels) +In order to limit webhook triggers to a specific resource, or resources, the Resource Selector can be used. It allows you to select the specific resource by label, or labels. + + + + +![Dashboard webhook - resource identifier](../img/dashboard-create-webhook-resource-identifier.png) + + -Create a webhook template payload file: +The Resource Selector can be set with `--selector`. +For example, `--selector test-type=postman-collection` will limit the resources to the postman tests (label: `test-type=postman-collection`) + + + + + +```yaml +spec: + selector: test-type=postman-collection +``` + +So, the complete definition may look like this: + +```yaml title="webhook.yaml" +apiVersion: executor.testkube.io/v1 +kind: Webhook +metadata: + name: example-webhook + namespace: testkube +spec: + uri: + events: + - start-test + - end-test-success + - end-test-failed + selector: test-type=postman-collection +``` + + + + + +### Webhook Payload + +Webhook payload can be configured - in this example, `event id`: +``` +{"text": "event id {{ .Id }}"} +``` + + + +Webhook payload can be configured in Webhook Settings->Action + +![Dashboard webhook - webhook settings action`](../img/dashboard-webhook-settings-action.png) + +![Dashboard webhook - webhook payload](../img/dashboard-webhook-payload.png) + + + + +Create a webhook payload template file: ```json title="template.json" { @@ -19,9 +140,13 @@ Create a webhook template payload file: } ``` +And set it with `--payload-template template.json`. + ```sh -testkube create webhook --name example-webhook --events start-test --events end-test-failed --payload-template template.json --uri +testkube create webhook --name example-webhook --events start-test --events end-test-passed --events end-test-failed --payload-template template.json --uri ``` +Where should be replaced with the HTTPS endpoint URL where you want to receive the webhook events. + ```sh title="Expected output:" Webhook created example-webhook 🥇 @@ -31,7 +156,14 @@ Webhook created example-webhook 🥇 -```yaml title="webhook.yaml" +Payload template can be configured with `spec.payloadTemplate`. +``` + payloadTemplate: | + {"text": "event id {{ .Id }}"} +``` + +Example: +``` apiVersion: executor.testkube.io/v1 kind: Webhook metadata: @@ -47,23 +179,14 @@ spec: payloadObjectField: "" payloadTemplate: | {"text": "event id {{ .Id }}"} - headers: - X-Token: "12345" -``` - -And then apply with: - -```sh -kubectl apply -f webhook.yaml ``` -In the example above, replace with the HTTPS endpoint URL where you want to receive the webhook events. The payload template can be customized to include additional information. In the above example, only the event `Id` is being sent. The template's variables will be replaced with data when events occur. - -You can add additional HTTP headers like `Authorization` or `x-api-key` to have a secret token. +#### Customizing Webhook Payload +The payload template can be customized to include additional information. In the above example, only the event `Id` is being sent. The template's variables will be replaced with data when events occur. It's possible to get access to env variables of testkube-api-server pod in webhook template: @@ -71,6 +194,80 @@ It's possible to get access to env variables of testkube-api-server pod in webho TESTKUBE_CLOUD_URL: {{ index .Envs "TESTKUBE_CLOUD_URL" }} ``` +### HTTP Headers +You can add additional HTTP headers like `Authorization` or `x-api-key` to have a secret token. + + + + +Webhook headers can be configured in Webhook Settings->Action. + +![Dashboard webhook - webhook settings action`](../img/dashboard-webhook-settings-action.png) + +![Dashboard webhook - webhook headers](../img/dashboard-webhook-headers.png) + + + + +Custom headers can be set using `--header` - for example: + +`--header X-Token="12345"` + + + + + +```yaml +spec: + headers: + X-Token: "12345" +``` + +```yaml title="webhook.yaml" +apiVersion: executor.testkube.io/v1 +kind: Webhook +metadata: + name: example-webhook + namespace: testkube +spec: + uri: + events: + - start-test + - end-test-success + - end-test-failed + selector: "" + headers: + X-Token: "12345" +``` + + + + +## Supported Event types +Webhooks can be triggered on any of the following events: +- start-test +- end-test-success +- end-test-failed +- end-test-aborted +- end-test-timeout +- start-testsuite +- end-testsuite-success +- end-testsuite-failed +- end-testsuite-aborted +- end-testsuite-timeout +- created +- updated +- deleted + +They can be triggered by the following resources: +- test +- testsuite +- executor +- trigger +- webhook +- testexecution +- testsuiteexecution + ## Testing Webhooks If you are just getting started and want to test your webhook configuration, you can use public and free services that act as HTTP catch-all apps. Here are a couple of options: diff --git a/docs/docs/img/dashboard-create-webhook-1.png b/docs/docs/img/dashboard-create-webhook-1.png new file mode 100644 index 00000000000..5b6f568773b Binary files /dev/null and b/docs/docs/img/dashboard-create-webhook-1.png differ diff --git a/docs/docs/img/dashboard-create-webhook-2.png b/docs/docs/img/dashboard-create-webhook-2.png new file mode 100644 index 00000000000..a7df407e856 Binary files /dev/null and b/docs/docs/img/dashboard-create-webhook-2.png differ diff --git a/docs/docs/img/dashboard-create-webhook-resource-identifier.png b/docs/docs/img/dashboard-create-webhook-resource-identifier.png new file mode 100644 index 00000000000..46b4b664d16 Binary files /dev/null and b/docs/docs/img/dashboard-create-webhook-resource-identifier.png differ diff --git a/docs/docs/img/dashboard-webhook-headers.png b/docs/docs/img/dashboard-webhook-headers.png new file mode 100644 index 00000000000..f37ca8bbddf Binary files /dev/null and b/docs/docs/img/dashboard-webhook-headers.png differ diff --git a/docs/docs/img/dashboard-webhook-payload.png b/docs/docs/img/dashboard-webhook-payload.png new file mode 100644 index 00000000000..ca344879707 Binary files /dev/null and b/docs/docs/img/dashboard-webhook-payload.png differ diff --git a/docs/docs/img/dashboard-webhook-settings-action.png b/docs/docs/img/dashboard-webhook-settings-action.png new file mode 100644 index 00000000000..01044ed2e58 Binary files /dev/null and b/docs/docs/img/dashboard-webhook-settings-action.png differ diff --git a/docs/docs/img/dashboard-webhooks-icon.png b/docs/docs/img/dashboard-webhooks-icon.png new file mode 100644 index 00000000000..619d58e8918 Binary files /dev/null and b/docs/docs/img/dashboard-webhooks-icon.png differ