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

feat: add docker CLI example #27

Merged
merged 7 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/.env
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ technologies:
## TypeScript

- [Receive a webhook with TypeScript and Express.js](typescript/inbound)
- [Create a Hookdeck connection with TypeScript and Node.js or Deno](typescript/create-connection)

## Python

Expand All @@ -36,6 +37,10 @@ technologies:

- [Receive a webhook with Go and Gin](go/inbound)

## CLI

- [Run the Hookdeck CLI within a Docker container for CI/CD](cli/docker)

## Learn more

- Check out the [Hookdeck docs](https://hookdeck.com/docs?ref=github-quickstarts)
3 changes: 3 additions & 0 deletions cli/docker/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HOOKDECK_API_KEY={YOUR_PROJECT_API_KEY}
HOOKDECK_SOURCE_NAME=example-webhooks-v3
APP_SERVICE_PORT=8080
70 changes: 70 additions & 0 deletions cli/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Using the Hookdeck CLI with Docker

This quickstart demonstrates how to use the Hookdeck CLI from Docker.

It includes ensuring that a Hookdeck [Connection](https://hookdeck.com/docs/connections?ref=github-quickstarts-cli-docker)
(a [Source](https://hookdeck.com/docs/sources?ref=github-quickstarts-cli-docker) and a [Destination](https://hookdeck.com/docs/destinations?ref=github-quickstarts-cli-docker))
exists by running a cURL command from the container.

To receive events within the docker contains, you also need to add your own web server.

## Before you begin

You'll need:

- [Docker](https://docs.docker.com/get-docker/) installed
- A [Hookdeck account](https://dashboard.hookdeck.com/signup?ref=github-quickstarts-cli-docker)

## Set up

Copy the example `.env.example` file:

```sh
cp .env.example .env
```

Set `HOOKDECK_API_KEY` to the value of your Project API key, which is found in the [secrets settings](https://dashboard.hookdeck.com/settings/project/secrets).

Set `HOOKDECK_SOURCE_NAME` to the name of your source. If this source does not exist, it will be created.

Set `APP_SERVICE_PORT` to the port of the application running in the Docker container.

## Run

```sh
docker-compose up
```


You will see output similar to the following:

```sh
docker-compose up
[+] Running 1/0
✔ Container hookdeck Created 0.0s
Attaching to hookdeck
hookdeck | hookdeck version 0.10.1
hookdeck | Checking for new versions...
hookdeck |
hookdeck | 2024/07/16 12:22:00 The Hookdeck CLI is configured on project quickstarts in organization Examples
hookdeck |
hookdeck | OK: 14 MiB in 24 packages
hookdeck | Ensuring connection exists for source example-webhooks-v3
hookdeck | % Total % Received % Xferd Average Speed Time Time Time Current
hookdeck | Dload Upload Total Spent Left Speed
100 1366 100 991 100 375 1806 683 --:--:-- --:--:-- --:--:-- 2488
hookdeck | {"id":"web_qafVAiPDiXFq","team_id":"tm_zDDhLIPBQw2e","updated_at":"2024-07-16T12:22:00.709Z","created_at":"2024-07-16T12:22:00.882Z","paused_at":null,"name":null,"rules":[],"description":null,"destination":{"id":"des_JZ9yNGgVwjcu","team_id":"tm_zDDhLIPBQw2e","url":null,"updated_at":"2024-07-16T12:22:00.715Z","created_at":"2024-07-16T12:11:09.437Z","rate_limit":null,"rate_limit_period":"second","cli_path":"/","path_forwarding_disabled":false,"name":"CLI","http_method":null,"auth_method":{"type":"HOOKDECK_SIGNATURE","config":{}},"description":null,"disabled_at":null},"source":{"id":"src_l5u7nwyghz0gec","team_id":"tm_zDDhLIPBQw2e","updated_at":"2024-07-16T12:22:00.714Z","created_at":"2024-07-16T11:44:55.964Z","name":"example-webhooks-v3","allowed_http_methods":["GET","PUT","PATCH","DELETE","POST"],"custom_response":null,"description":null,"url":"https://hkdk.events/l5u7nwyghz0gec","disabled_at":null,"verification":null},"disabled_at":null,"full_name":"example-webhooks-v3 -> CLI"}
hookdeck | Dashboard
hookdeck | 👉 Inspect and replay events: https://dashboard.hookdeck.com?team_id=tm_zDDhLIPBQw2e
hookdeck |
hookdeck | example-webhooks-v3 Source
hookdeck | 🔌 Event URL: https://hkdk.events/irq4hh3eu54hxs
hookdeck |
hookdeck | Connections
hookdeck | example-webhooks-v3_to_CLI forwarding to /
hookdeck |
hookdeck | Getting ready...
hookdeck | Ready! (^C to quit)
```

Requests to the **Event URL** from the CLI output will be forwarded to an application running on the port identified by `APP_SERVICE_PORT`. You will need to create this application.
40 changes: 40 additions & 0 deletions cli/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
services:
hookdeck:
image: hookdeck/hookdeck-cli:latest
container_name: hookdeck
entrypoint: ["/bin/sh","-c"]
env_file:
- path: ./.env
required: true
command:
- |
/bin/hookdeck version
/bin/hookdeck ci --api-key $HOOKDECK_API_KEY

echo "Installing curl dependency. This could be done within a Dockerfile."
apk add curl

echo "Ensuring connection exists for source $HOOKDECK_SOURCE_NAME"
curl --location --request PUT 'https://api.hookdeck.com/2024-03-01/connections' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer $HOOKDECK_API_KEY' \
--data '{
"name": "${HOOKDECK_SOURCE_NAME}_to_CLI",
"source": {
"name": "$HOOKDECK_SOURCE_NAME",
"allowed_http_methods": [
"GET",
"PUT",
"PATCH",
"DELETE",
"POST"
]
},
"destination": {
"name": "CLI",
"cli_path": "/"
}
}'

/bin/hookdeck listen http://host.docker.internal:$APP_SERVICE_PORT $HOOKDECK_SOURCE_NAME