Skip to content

Commit 409ba47

Browse files
committed
Add reusable client and CI workflows
1 parent e35d43c commit 409ba47

File tree

5 files changed

+95
-29
lines changed

5 files changed

+95
-29
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Build example container
14+
run: docker build .

.github/workflows/release.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*.*.*'
7+
8+
permissions:
9+
contents: read
10+
packages: write
11+
12+
jobs:
13+
push-image:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Log in to GHCR
18+
uses: docker/login-action@v3
19+
with:
20+
registry: ghcr.io
21+
username: ${{ github.actor }}
22+
password: ${{ secrets.GITHUB_TOKEN }}
23+
- name: Build and push
24+
uses: docker/build-push-action@v5
25+
with:
26+
push: true
27+
tags: ghcr.io/${{ github.repository_owner }}/bash-check:${{ github.ref_name }}

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
This directory demonstrates a minimal Kuberhealthy check written in Bash.
44

5-
The [`check.sh`](check.sh) script reads the Kuberhealthy run UUID and reporting URL from the `KH_RUN_UUID` and `KH_REPORTING_URL` environment variables that Kuberhealthy sets on checker pods. It then reports success or failure back to Kuberhealthy.
5+
The reusable [`kuberhealthy-client.sh`](kuberhealthy-client.sh) library exposes helper functions for reporting results. The [`check.sh`](check.sh) example sources the library, reads the Kuberhealthy run UUID and reporting URL from the `KH_RUN_UUID` and `KH_REPORTING_URL` environment variables that Kuberhealthy sets on checker pods, and then reports success or failure back to Kuberhealthy.
66

77
## Using the example
88

9-
1. **Add your logic** – Replace the placeholder section in `check.sh` with commands that verify the condition you care about. Call `report_success` when the check passes or `report_failure "message"` when it fails.
9+
1. **Add your logic** – Replace the placeholder section in `check.sh` with commands that verify the condition you care about. Call `kh::report_success` when the check passes or `kh::report_failure "message"` when it fails.
1010
2. **Build the image** – Build a container that runs the script:
1111

1212
```sh
@@ -37,3 +37,20 @@ The [`check.sh`](check.sh) script reads the Kuberhealthy run UUID and reporting
3737
```
3838
3939
When the pod runs, `check.sh` will report its result to Kuberhealthy. Set the `FAIL=true` environment variable to simulate a failure.
40+
41+
## Using the client library in your own scripts
42+
43+
Copy `kuberhealthy-client.sh` into your repository and source it from your Kuberhealthy check scripts:
44+
45+
```bash
46+
#!/usr/bin/env bash
47+
set -euo pipefail
48+
source "kuberhealthy-client.sh"
49+
kh::init
50+
51+
if my_check_logic; then
52+
kh::report_success
53+
else
54+
kh::report_failure "something went wrong"
55+
fi
56+
```

check.sh

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,14 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
# Load Kuberhealthy run metadata from environment variables provided to the pod.
5-
UUID="${KH_RUN_UUID:-}"
6-
REPORT_URL="${KH_REPORTING_URL:-}"
7-
8-
if [[ -z "$UUID" || -z "$REPORT_URL" ]]; then
9-
echo "KH_RUN_UUID and KH_REPORTING_URL must be set" >&2
10-
exit 1
11-
fi
12-
13-
report_success() {
14-
curl -sS -X POST \
15-
-H "Content-Type: application/json" \
16-
-H "kh-run-uuid: ${UUID}" \
17-
-d '{"ok":true,"errors":[]}' \
18-
"${REPORT_URL}"
19-
}
20-
21-
report_failure() {
22-
local msg="$1"
23-
curl -sS -X POST \
24-
-H "Content-Type: application/json" \
25-
-H "kh-run-uuid: ${UUID}" \
26-
-d '{"ok":false,"errors":["'"${msg}"'"]}' \
27-
"${REPORT_URL}"
28-
}
4+
# Source the Kuberhealthy client library and initialize it.
5+
source "$(dirname "$0")/kuberhealthy-client.sh"
6+
kh::init
297

308
# Add your check logic here. For example purposes, this check reports success
319
# unless the FAIL environment variable is set to "true".
3210
if [[ "${FAIL:-}" == "true" ]]; then
33-
report_failure "FAIL was set to true"
11+
kh::report_failure "FAIL was set to true"
3412
else
35-
report_success
13+
kh::report_success
3614
fi

kuberhealthy-client.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
# Helper functions for reporting results to Kuberhealthy from bash checks.
3+
# Source this file in your check script and call kh::init before reporting results.
4+
5+
kh::init() {
6+
UUID="${KH_RUN_UUID:-}"
7+
REPORT_URL="${KH_REPORTING_URL:-}"
8+
if [[ -z "$UUID" || -z "$REPORT_URL" ]]; then
9+
echo "KH_RUN_UUID and KH_REPORTING_URL must be set" >&2
10+
return 1
11+
fi
12+
}
13+
14+
kh::report_success() {
15+
curl -sS -X POST \
16+
-H "Content-Type: application/json" \
17+
-H "kh-run-uuid: ${UUID}" \
18+
-d '{"ok":true,"errors":[]}' \
19+
"${REPORT_URL}"
20+
}
21+
22+
kh::report_failure() {
23+
local msg="$1"
24+
curl -sS -X POST \
25+
-H "Content-Type: application/json" \
26+
-H "kh-run-uuid: ${UUID}" \
27+
-d '{"ok":false,"errors":["'"${msg}"'"]}' \
28+
"${REPORT_URL}"
29+
}
30+

0 commit comments

Comments
 (0)