CI
A CI job can start a run in your workspace, wait for it, and fail the build when a gate fails.
How it works
- A workflow step calls your workspace's API with a personal token.
- The workspace runs a suite it already holds, against the system address written in that suite.
- The step waits for the run, prints each gate, and fails when the run failed or is incomplete.
The suite must be saved in the workspace. The step names it by file name, for example support-bot.yaml: the part after /builder/ in the Builder's address.
The workspace tests whatever answers at the address in the suite. Deploy the change there first, for example to a staging environment, and run this job after the deploy job.
The Automate page writes workflows that run HarnessLab on the CI runner itself. On Cloud, use the workflow on this page instead, so the run is recorded in your workspace.
Set up
| Step | Where |
|---|---|
| Create a personal token | In the workspace: Settings → Your account → Personal tokens. Pick an expiry: 30 days, 90 days, 1 year or Never. Copy the token: it is shown once. |
| Store the token | In GitHub: Settings → Secrets and variables → Actions → Secrets, as HARNESSLAB_TOKEN. |
| Store the address | Same page, Variables, as HARNESSLAB_URL: the workspace address the console shows. |
| Add gates | In the suite, see gates. Without a gate, only a failed or incomplete run fails the build. |
A token acts as you, with your role. Starting a run needs the editor or admin role.
| Name | Kind | Value |
|---|---|---|
HARNESSLAB_TOKEN | secret | A personal token. |
HARNESSLAB_URL | variable | The workspace address. |
The workflow
Save this as .github/workflows/eval.yml and set SUITE to your suite's file name.
The step uses only Python's standard library, which the runner has, so it needs no checkout and no install.
name: eval
on:
pull_request:
push:
branches: [main]
concurrency: harnesslab-eval
jobs:
eval:
runs-on: ubuntu-latest
steps:
- name: Run the suite in the workspace
shell: python3 {0}
env:
HARNESSLAB_URL: ${{ vars.HARNESSLAB_URL }}
HARNESSLAB_TOKEN: ${{ secrets.HARNESSLAB_TOKEN }}
SUITE: support-bot.yaml
CHANGED: ${{ github.event.head_commit.message || github.event.pull_request.title }}
run: |
import json, os, sys, time, urllib.error, urllib.request
base = os.environ["HARNESSLAB_URL"].rstrip("/")
auth = {"Authorization": "Bearer " + os.environ["HARNESSLAB_TOKEN"], "Content-Type": "application/json"}
start_limit = 300 # seconds a stopped workspace may take to start
def call(method, path, body=None):
data = None if body is None else json.dumps(body).encode()
deadline = time.monotonic() + start_limit
while True:
request = urllib.request.Request(base + path, data=data, headers=auth, method=method)
try:
with urllib.request.urlopen(request, timeout=60) as reply:
return json.load(reply)
except urllib.error.HTTPError as error:
wait = error.headers.get("Retry-After", "")
if error.code != 503 or not wait.isdigit():
sys.exit(f"{method} {path}: HTTP {error.code} {error.read().decode()}")
if time.monotonic() + int(wait) > deadline:
sys.exit(f"{method} {path}: the workspace did not start within {start_limit // 60} minutes")
print(f"The workspace is starting. Trying again in {wait} seconds.")
time.sleep(max(1, int(wait)))
run_id = call("POST", "/api/runs", {
"suite": os.environ["SUITE"],
"changed": os.environ.get("CHANGED", ""),
"source_revision": os.environ["GITHUB_SHA"],
"source_repo": os.environ["GITHUB_SERVER_URL"] + "/" + os.environ["GITHUB_REPOSITORY"],
})["run_id"]
print(f"{base}/experiments/{run_id}")
status = call("GET", f"/api/runs/{run_id}/status")
while status["status"] == "running":
time.sleep(15)
status = call("GET", f"/api/runs/{run_id}/status")
if status["status"] in ("complete", "failed"):
for gate in call("GET", f"/api/experiments/{run_id}")["gates"]:
mark = "pass" if gate["passed"] else ("warn" if gate["severity"] == "warn" else "FAIL")
print(f"{mark} {gate['gate_id']}: {gate.get('reason') or ''}")
completeness = status.get("completeness") or {}
for reason in completeness.get("reasons", []):
print("incomplete: " + reason)
if status.get("error"):
print("error: " + status["error"])
if status["status"] != "complete" or not completeness.get("complete"):
sys.exit(f"run {run_id} did not pass: {status['status']}")
concurrency keeps two pushes from starting runs at the same time, which your workspace's limit on concurrent runs may refuse.
The run page records the commit and the repository from source_revision and source_repo.
The API
| Call | Does |
|---|---|
POST /api/runs | Starts a run. Returns {run_id, status: "running"}. |
GET /api/runs/{run_id}/status | status is running, complete, failed or cancelled. Also done and total, gate_blocked, completeness and error. |
GET /api/experiments/{run_id} | The full result. gates lists each gate with gate_id, passed, severity and reason. |
POST /api/runs takes these fields. Only suite is required.
| Field | Meaning |
|---|---|
suite | The suite's file name. |
label | A name for the run. |
changed | What changed since the last run, shown with the run. |
hypothesis | What you expect, shown with the run. |
after | The run this one follows. vs gates compare against it. |
baseline | The baseline arm for comparisons. |
repeats | Replaces every repeats in the suite for this run. |
concurrency | Requests in flight at once for this run. |
source_revision | The full commit id of the system under test. |
source_repo | The repository's URL. |
What fails the build
| Outcome | Status | Step |
|---|---|---|
Every error gate passed and the run is complete | complete | passes |
An error gate failed or could not be evaluated | failed, with gate_blocked: true | fails |
| Attempts errored, so the evidence is incomplete | completeness.complete: false | fails |
| The run stopped with an error | failed, with error | fails |
A failed warn gate is printed but does not fail the build.
A failing required: true scorer does not change the status by itself. Add the gate {id: no_required_failures, metric: required_failures, op: "==", value: 0} to fail on it.
A vs gate fails on the first run of a suite, because there is no earlier run to compare with.
| Error | Cause |
|---|---|
| HTTP 401 | The token is missing, wrong, expired or revoked. |
| HTTP 403 | Your role cannot start runs. |
| HTTP 402 | The workspace refused the run: a feature the beta does not include, too many runs at once, or no monthly cases left. The message says which. |
| HTTP 404 | No suite of that file name in the workspace. |
| HTTP 422 | The suite or the request is invalid. The message says why. |
| HTTP 503 | The workspace is not running and cannot start by itself, for example after a failed setup. Its owner starts it in the console. |
Every run counts toward your workspace's monthly cases: one case, asked once of one arm, counts as one.
A stopped workspace
A workspace stops after two weeks without use. A call to its address starts it again.
Until it runs, every call gets HTTP 503 with a Retry-After header: the seconds to wait before asking again.
The step above waits and asks again, for up to 5 minutes, and prints The workspace is starting. while it waits.
Calls alone are not use: a workspace that only answered calls stops again within half an hour. A run started there is use.