Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Webhook docs extended #4310

Merged
merged 22 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c68b636
Webhook docs extended
tkonieczny Aug 31, 2023
2b98806
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
697e2e3
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
3293c07
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
950feaf
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
de7e085
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
07f36a9
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
ad73757
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
fb6a5c7
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
a9bb8c3
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
1048dae
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
b5a1907
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
a421de5
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
ffe26e4
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
a1f3019
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
44519a7
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
59602bc
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
b11a9a6
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
5f68e54
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
2ab550d
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
701747c
Update docs/docs/articles/webhooks.mdx
tkonieczny Sep 3, 2023
d631a55
Updated after CR
tkonieczny Sep 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 212 additions & 15 deletions docs/docs/articles/webhooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,148 @@ 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.

<Tabs>
<TabItem value="dash" label="Dashboard">
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.

</TabItem>

<TabItem value="cli" label="CLI">
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 <YOUR_ENDPOINT_URL>
```
`--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.

</TabItem>

<TabItem value="crd" label="Custom Resource">

```yaml title="webhook.yaml"
apiVersion: executor.testkube.io/v1
kind: Webhook
metadata:
name: example-webhook
namespace: testkube
spec:
uri: <YOUR_ENDPOINT_URL>
events:
- start-test
- end-test-success
- end-test-failed
selector: ""
```
Where <YOUR_ENDPOINT_URL> 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
```

</TabItem>

</Tabs>

### 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.

<Tabs>
<TabItem value="dash" label="Dashboard">

![Dashboard webhook - resource identifier](../img/dashboard-create-webhook-resource-identifier.png)

</TabItem>

<TabItem value="cli" label="CLI">

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`)

</TabItem>

<TabItem value="crd" label="Custom Resource">

```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: <YOUR_ENDPOINT_URL>
events:
- start-test
- end-test-success
- end-test-failed
selector: test-type=postman-collection
```

</TabItem>
</Tabs>


### Webhook Payload

Webhook payload can be configured - in this example, `event id`:
```
{"text": "event id {{ .Id }}"}
```

<Tabs>
<TabItem value="dash" label="Dashboard">
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)

</TabItem>

<TabItem value="cli" label="CLI">
Create a webhook payload template file:

```json title="template.json"
{
"text": "event id {{ .Id }}"
}
```

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 <YOUR_ENDPOINT_URL>
testkube create webhook --name example-webhook --events start-test --events end-test-passed --events end-test-failed --payload-template template.json --uri <YOUR_ENDPOINT_URL>
```
Where <YOUR_ENDPOINT_URL> should be replaced with the HTTPS endpoint URL where you want to receive the webhook events.


```sh title="Expected output:"
Webhook created example-webhook 🥇
Expand All @@ -31,7 +156,14 @@ Webhook created example-webhook 🥇

<TabItem value="crd" label="Custom Resource">

```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:
Expand All @@ -47,30 +179,95 @@ spec:
payloadObjectField: ""
payloadTemplate: |
{"text": "event id {{ .Id }}"}
headers:
X-Token: "12345"
```

And then apply with:

```sh
kubectl apply -f webhook.yaml
```

</TabItem>

</Tabs>

In the example above, replace <YOUR_ENDPOINT_URL> 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:

```sh title="template.txt"
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.

<Tabs>
<TabItem value="dash" label="Dashboard">

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)

</TabItem>

<TabItem value="cli" label="CLI">
Custom headers can be set using `--header` - for example:

`--header X-Token="12345"`

</TabItem>

<TabItem value="crd" label="Custom Resource">

```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: <YOUR_ENDPOINT_URL>
events:
- start-test
- end-test-success
- end-test-failed
selector: ""
headers:
X-Token: "12345"
```

</TabItem>
</Tabs>

## 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:
Expand Down
Binary file added docs/docs/img/dashboard-create-webhook-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/docs/img/dashboard-create-webhook-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/docs/img/dashboard-webhook-headers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/docs/img/dashboard-webhook-payload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/docs/img/dashboard-webhooks-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading