Using hadronbox in CI
Last updated: 1 December 2026 · CLI 0.4.0 · Also works with the REST API · API v1 (beta)
This guide explains how to open a hadronbox machine from a CI workflow, run your tests in it and delete the machine in every case. The examples are for GitHub Actions and Woodpecker; the same steps work in other CI systems too.
Version: The examples use the published
hadronbox@0.4.0CLI (npx -y hadronbox@0.4.0). If you do not use the CLI, you can set up the same flow with the REST API.
How it works
The steps below are written with the CLI. You can do the same four steps without the CLI, with the REST API: with the REST API.
- Verify the key:
npx hadronbox whoamishows that the key is valid and which account it belongs to. - Create a machine:
npx hadronbox create <name>creates the machine and prints its name. The name must be different on every run; the examples use the run number. - Fetch the code onto the machine and run the command: The new machine's workspace is empty; the checkout in CI is not carried to the machine. So the command that runs on the machine first fetches the CI run's commit onto the machine with
git, then runsnpm ci && npm test(see Repository code). The CLI'snpx -y hadronbox@0.4.0 exec <name> -- <command>runs it on the machine, streams its output (stdout and stderr) to the CI log and exits with the command's exit code. If the command fails, the job fails too. - Delete the machine:
npx hadronbox rm <name> --yesdeletes the machine. This step runs whether the job succeeds or fails, so no machine is left open behind.
Creating a CI key
Use a separate, scoped key for CI:
- Open the CI page in the panel and give the key a name in the Create a CI key section.
- Choose the validity period (at most 90 days). When the period ends, the key stops working; you create a new one and update the secret.
- Copy the key. It is shown only when it is created.
The CI template gives the key only these scopes:
With this key you cannot pause a machine, change its settings, take snapshots or do panel operations. A request outside its scopes is rejected with 403 and the code INSUFFICIENT_SCOPE. All the scopes are on the API keys page.
Secret
Save the key as a secret of the CI system; do not write it into the workflow file or the repository.
- GitHub: in the repository settings, save it under Settings → Secrets and variables → Actions with the name
HADRONBOX_API_KEY. With the GitHub CLI:gh secret set HADRONBOX_API_KEY(paste the key when the value is asked for; that way the key does not end up in the shell history). - Woodpecker: in the repository settings, save it under Secrets with the name
hadronbox_api_key.
The CLI reads the key from the HADRONBOX_API_KEY environment variable; a key on the command line is not accepted. The examples print the key nowhere. Secrets are not given to pull requests that come from forks; that is why the examples run only on push and on a manual start. If you also want to run on pull requests, limit that to requests that come from your own repository's branches.
Repository code
For the tests to run on the machine, the code has to be on the machine. The examples do this the simplest way: the command that runs on the machine fetches the CI run's commit with a read-only repository key (git pull --depth 1 … <commit>), then runs the tests. The machine template must have git and Node.js.
- Create a read-only key for the repository. On GitHub: a fine-grained personal access token that reaches only this repository, with Contents: Read-only. On Gitea / Forgejo: an access key with permission to read the repository.
- Save the key not in CI but in hadronbox: on the Secrets page in the panel with the name
REPO_READ_TOKEN, scope All machines. Secrets are given to every command on the machine as an environment variable. - The workflow command sends the name
$REPO_READ_TOKENto the machine as it is; the value is expanded only on the machine. The key is not visible in the CI log, in the CI secrets or in the command text.
The key is not embedded in the address: with git -c credential.helper=… it is given only on the machine, at the moment git asks for it; it does not show in the address, in git's output or in the CI log. If the repository is public, no key is needed: you can delete the -c credential.helper='…' part.
Without the CLI: with the REST API
Without the CLI, you can set up the same flow with the REST API through curl. Five requests are enough; all of them fall within the scopes of the CI key:
Deleting is done with POST …/destroy, not with DELETE. The address is https://api.hadronbox.tr/v1; the key goes in the Authorization: Bearer $HADRONBOX_API_KEY header. The script needs curl, jq and awk (they are ready on GitHub's ubuntu-latest runner). Save the script below in your repository as ci/hadronbox-ci.sh:
#!/usr/bin/env bash
# With the REST API: create a machine, run the command, delete the machine in every case.
# Required environment: HADRONBOX_API_KEY (a CI secret), HB_MACHINE (a unique machine name), TEST_COMMAND.
set -euo pipefail
API="${HADRONBOX_API_URL:-https://api.hadronbox.tr}/v1"
: "${HADRONBOX_API_KEY:?}" "${HB_MACHINE:?}" "${TEST_COMMAND:?}"
OUT="$(mktemp)"
ID=""
api() { curl -fsS -H "Authorization: Bearer $HADRONBOX_API_KEY" -H 'Content-Type: application/json' "$@"; }
cleanup() {
if [ -n "$ID" ]; then
api -X POST "$API/sandboxes/$ID/destroy" >/dev/null || echo "Could not delete the machine: $ID" >&2
fi
rm -f "$OUT"
}
trap cleanup EXIT
ID="$(api -X POST "$API/sandboxes" -d "$(jq -n --arg n "$HB_MACHINE" '{name: $n}')" | jq -r '.data.sandboxId')"
STATUS=""
for _ in $(seq 1 60); do
STATUS="$(api "$API/sandboxes/$ID" | jq -r '.data.status')"
[ "$STATUS" = running ] && break
[ "$STATUS" = error ] && { echo "Could not start the machine" >&2; exit 1; }
sleep 2
done
[ "$STATUS" = running ] || { echo "The machine was not ready in 2 minutes" >&2; exit 1; }
EXEC="$(api -X POST "$API/sandboxes/$ID/exec" \
-d "$(jq -n --arg c "$TEST_COMMAND" '{command: $c, timeoutMs: 540000}')" | jq -r '.data.executionId')"
# The output is an SSE stream: stdout/stderr events go to the CI log, the exit event carries the exit code.
curl -fsSN --max-time 600 -H "Authorization: Bearer $HADRONBOX_API_KEY" \
"$API/sandboxes/$ID/executions/$EXEC/output" |
awk -v out="$OUT" '
/^event:/ { ev = $2; next }
/^data:/ {
d = $0; sub(/^data: ?/, "", d)
if (ev == "exit") { print d > out; close(out) }
else { gsub(/\\n/, "\n", d); printf "%s", d; fflush() }
}'
CODE="$(cat "$OUT")"
[ -n "$CODE" ] || { echo "Could not read the exit code: the stream dropped" >&2; exit 125; }
exit "$CODE"
In GitHub Actions, call the script with this step (the machine name comes from the run number):
- name: Run the tests on the hadronbox machine
timeout-minutes: 12
env:
HADRONBOX_API_KEY: ${{ secrets.HADRONBOX_API_KEY }}
HB_MACHINE: ci-${{ github.run_id }}-${{ github.run_attempt }}
TEST_COMMAND: >-
git init -q app && cd app
&& git -c credential.helper='!f() { echo username=x-access-token; echo "password=$REPO_READ_TOKEN"; }; f' pull -q --depth 1 "https://github.com/${{ github.repository }}.git" ${{ github.sha }}
&& npm ci && npm test
run: bash ci/hadronbox-ci.sh
In Woodpecker, the step's image must have curl and jq; give ci-wp-${CI_PIPELINE_NUMBER} as HB_MACHINE. The $REPO_READ_TOKEN in TEST_COMMAND is expanded on the machine; the Repository code section explains why. The script deletes the machine on every exit with trap; if the CI job is killed by force, the machine may be left, see the Clean-up section. The script's exit code is the same as the test's exit code; if the stream drops, it returns 125.
GitHub Actions
Save the file in your repository as .github/workflows/hadronbox.yml.
.github/workflows/hadronbox.yml# .github/workflows/hadronbox.yml
# hadronbox CI example: runs the tests on a new machine and always deletes the machine.
# The same flow without the CLI, with the REST API + curl: https://hadronbox.tr/en/docs/ci#without-the-cli-with-the-rest-api
# Secret: HADRONBOX_API_KEY (the key created in the panel with the CI template).
name: hadronbox
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 20
env:
HADRONBOX_API_KEY: ${{ secrets.HADRONBOX_API_KEY }}
MACHINE: ci-${{ github.run_id }}-${{ github.run_attempt }}
steps:
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Verify the key
timeout-minutes: 2
run: npx -y hadronbox@0.4.0 whoami
- name: Create the machine
timeout-minutes: 5
run: npx -y hadronbox@0.4.0 create "$MACHINE"
- name: Run the tests on the machine
timeout-minutes: 10
env:
# The machine's workspace is empty: the command first fetches this commit onto the machine, then runs the tests.
# REPO_READ_TOKEN is a hadronbox secret (a read-only repository key; panel → Secrets,
# "All machines"). Its value is expanded on the machine; the CI side carries only its name.
TEST_COMMAND: >-
git init -q app && cd app
&& git -c credential.helper='!f() { echo username=x-access-token; echo "password=$REPO_READ_TOKEN"; }; f' pull -q --depth 1 "https://github.com/${{ github.repository }}.git" ${{ github.sha }}
&& npm ci && npm test
run: npx -y hadronbox@0.4.0 exec "$MACHINE" --timeout 540 -- "$TEST_COMMAND"
- name: Delete the machine
if: always()
timeout-minutes: 3
run: npx -y hadronbox@0.4.0 rm "$MACHINE" --yes
if: always() runs the delete step even if the earlier steps fail or the job is cancelled. The test command inside the machine does not need the CI runner's actions/checkout step; it fetches the code itself.
Woodpecker
Save the file in your repository as .woodpecker/hadronbox.yaml.
.woodpecker/hadronbox.yaml# .woodpecker/hadronbox.yaml
# hadronbox CI example: runs the tests on a new machine and always deletes the machine.
# The same flow without the CLI, with the REST API + curl: https://hadronbox.tr/en/docs/ci#without-the-cli-with-the-rest-api
# Secret: hadronbox_api_key (the key created in the panel with the CI template).
when:
- event: [push, manual]
branch: main
steps:
- name: hadronbox-test
image: node:20-bookworm-slim
environment:
HADRONBOX_API_KEY:
from_secret: hadronbox_api_key
commands:
- npx -y hadronbox@0.4.0 whoami
- npx -y hadronbox@0.4.0 create "ci-wp-${CI_PIPELINE_NUMBER}" > .hadronbox-machine
# The machine's workspace is empty: the command first fetches this commit onto the machine, then runs the tests.
# REPO_READ_TOKEN is a hadronbox secret (a read-only repository key); its value is expanded on the machine.
- REPO="$${CI_REPO_CLONE_URL#https://}"
- timeout 600 npx -y hadronbox@0.4.0 exec "$$(cat .hadronbox-machine)" --timeout 540 -- "git init -q app && cd app && git -c credential.helper='!f() { echo username=x-access-token; echo \"password=\$$REPO_READ_TOKEN\"; }; f' pull -q --depth 1 https://$$REPO $$CI_COMMIT_SHA && npm ci && npm test"
- name: hadronbox-cleanup
image: node:20-bookworm-slim
environment:
HADRONBOX_API_KEY:
from_secret: hadronbox_api_key
commands:
- test -s .hadronbox-machine || exit 0
- npx -y hadronbox@0.4.0 rm "$$(cat .hadronbox-machine)" --yes
when:
- status: [success, failure]
In Woodpecker every step runs in a separate container, while the working directory is shared. That is why the machine's name is written to the .hadronbox-machine file; the clean-up step reads the name from there and runs in every case with the status: [success, failure] condition. If the machine could not be created, the file stays empty and the clean-up step ends without doing anything. $$ is an escape from Woodpecker's own variable substitution; the shell sees it as $(…).
How hb exec behaves
- Command: If you give a single argument after the
--marker, it is treated as a shell line ("npm ci && npm test"); if you give several arguments, they are passed to the machine as they are, quoted. If you will use pipes and&&, give the command as a single quoted argument. - Output: The command's stdout and stderr stream to the CI log while it runs; with
--jsonit is written as a single JSON document. stdin is not forwarded to the machine. - Exit code: The CLI exits with the remote command's exit code; if
npm testreturns1, the CI step ends with1too. - Error code 125: Errors on the CLI side (before the command starts, a dropped connection, the
--timeoutperiod running out) end with125. With--json,details.cliExitCodetells the class. - Timeout: With
--timeout <sec>, the command is terminated if it does not finish in that many seconds (1–3600). When the time runs out, the CLI exits with125. - Known limit: Ctrl+C or CI cancelling the job (the CLI exits with
130) does not stop the command running on the machine; the command may continue on the machine. That is why the delete step must run in every case, so that no machine is left after a cancellation.
Timeouts
A long-running or stuck job uses both your CI minutes and your machine time. The examples have three layers:
- Job:
timeout-minutes: 20on GitHub; the timeout in the repository settings on Woodpecker. - Step: each step's own
timeout-minutesvalue on GitHub;timeout 600at the start of the command on Woodpecker. - Command:
hb exec --timeout 540(9 minutes); when the time runs out, the command is terminated on the machine. At most 3600 seconds (1 hour).
Keep the command timeout shorter than the step timeout; that way the command ends before the CI job is cut off and the delete step has time to run.
Clean-up
The delete step runs in every case (if: always() on GitHub, status: [success, failure] on Woodpecker). Still, if the CI system terminates the job by force (for example, if the runner is lost), a machine may be left. Check the Machines page in the panel from time to time, or list the machines that start with ci- with npx hadronbox ls. Unused machines are paused automatically when they sit idle.
Troubleshooting
INVALID_API_KEY(401): The key is wrong, revoked or expired. Create a new CI key in the panel and update the secret.INSUFFICIENT_SCOPE(403): The job does an operation outside the CI key's scopes (for example, pausing a machine).details.requiredScopetells the scope needed;hb execneedsmachines:exec.PLAN_LIMIT_MACHINES(402): Your machine quota is full. Delete the CI machines that were left.NAME_TAKEN(409): A machine with the same name exists; add the run number to the machine name.COMMAND_BLOCKED(400): The security scan refused the command before sending it to the machine; it never started. The error names the part the scan refused (details.target) and why (details.reason). The scan is a coarse text scan and also catches some harmless commands:number:numberparts (echo 12:00,docker run -p 8080:80 nginx) read as an address and port, and local addresses such aslocalhost:3000read as a target outside the allowlist. In that case write the command to a script file, send it withhb cp ./script.sh <machine>:script.shand run it withhb exec <machine> -- sh script.sh(the script's content is not scanned), or run it in an interactive terminal withhb sh <machine>. The cloud metadata service, internal network addresses and paths out of the machine (details.hint: not_allowed) are always blocked.