# Running from CI/CD

A pipeline re-tests every release with the same `LOAD/`, fails when the target got worse, pins each accepted release as the baseline for the next one, and posts a summary to the pull request.

**Every pipeline run is a charged load test.** An agent adds this job only after its user said yes to a concrete proposal: what triggers it (every merge to the main branch, a release tag, nightly, every pull request), about how many runs a month that makes, the credits per run (`capacitylab-load check` prints each profile's price) and per month. Without that yes, leave the command in `LOAD/README.md` for your user to run by hand.

## Set up once

| | |
|---|---|
| Key | a CI secret `CAPACITYLAB_KEY`; never print it ([access.md](/docs/access.md)) |
| Hosts | commit the key's line to the application's `/.well-known/capacitylab-verify.txt`: every environment that deploys it proves itself, preview hosts included. A DNS TXT record proves one exact name. |
| Scenario secrets | CI secrets exposed as environment variables with the names `LOAD/` declares |
| Order | deploy to staging first, then the load test |
| Data | reset the staging data before the test (restore a snapshot or run the seed script), so a growing database does not slow every next release; see [data-volume.md](/docs/data-volume.md). If the application has seed and cleanup endpoints, `LOAD/`'s own setup and teardown can call them ([load-format.md §7.4](/docs/load-format.md#74-setup-teardown-and-background-workflows)) |
| Cost | every run is charged ([access.md](/docs/access.md#ceilings-and-price)): 30 iterations/s for 10 minutes is 10 credits, so a gate on every pull request costs that per pull request. Re-test releases with a short constant run; keep the full search for planned changes. Waiting (`--wait-for-slot`, `--wait-for-window`) costs no credits, but your CI bills the job's minutes while it waits. |

## The gate

Keep the release run in `LOAD/` as a profile ([load-format.md §7.2](/docs/load-format.md#72-run-profiles)), so the pipeline and your own re-tests run exactly the same thing:

```yaml
x-capacitylab-load:
  runs:
    release: {rps: 30, duration: 10m, gate: true, tags: [release], stop: {when: [{p95: 800ms, errors: 5%}]}}
```

```bash
capacitylab-load run ./LOAD --target "$STAGING_URL" --cloud --profile release \
  --tag "$GITHUB_SHA" --markdown capacitylab.md --wait-for-slot 30m
```

The profile's `tags` and each `--tag` mark the run: here the commit (`$GITHUB_SHA` on GitHub, `$CI_COMMIT_SHA` on GitLab; an empty tag fails the step). `capacitylab-load runs --tag release` lists the release series, and `capacitylab-load runs --tag <sha>` finds the run of one build.

The profile's `gate` (or `--gate` on the command line) fails the step when the run stopped on an SLO breach, had 1 % errors or more (`--gate-errors`; `0%` fails on any error), or a p95 got worse than in a comparable baseline (same `LOAD/` and schedule) by more than it may. It checks the overall p95, every step's and every workflow's, so one slow endpoint fails the release even when the average holds. Each may lose `--gate-p95` (default 20 %) plus its own run-to-run spread, when the service has measured it (see [Noise](#noise)). Without a comparable baseline the regression check is skipped with a note.

| Exit code | Meaning |
|---|---|
| 0 | the run finished and passed the gate |
| 1 | the run did not finish: it failed, was stopped, its setup failed, the generator cut it short, or it stopped itself because the target gave no HTTP response or showed a wrong certificate (`target_stop`, [results.md](/docs/results.md)) |
| 2 | wrong flags or `LOAD/` |
| 3 | the run finished and failed the gate; the output says why |
| 4 | the run did not start: a target host's test window is closed (see below) |

Use the gate for a constant-rate re-test. In a capacity search an SLO stop is the expected result, not a failure.

## Production and test windows

A pipeline that runs during the day must not load production then. Set the production host's windows once ([access.md](/docs/access.md#test-windows)); a run outside them does not start and the CLI exits 4. Pick what the pipeline does then:

- **Skip**: treat 4 as "not now": `capacitylab-load run ... --profile release || [ $? -eq 4 ]`, and run the production re-test from a scheduled night pipeline.
- **Wait**: `--wait-for-window 12h` waits until the window opens, then starts; the CI job's timeout must cover the wait plus the run, and your CI bills those minutes.

Staging usually needs no windows.

## The baseline

Each run is compared with the key's **pinned baseline** for that target, else with the previous run ([results.md](/docs/results.md#comparison-with-the-previous-run)). Comparing with the previous run lets a series of slightly slower releases drift unnoticed, so pin the release you accepted:

- `--pin-baseline` pins the run when it finished and, with `--gate`, passed it: use it on the main branch or the release pipeline, not on pull requests. If pinning itself fails, the step stays green and prints a warning with the command to pin later.
- `capacitylab-load baseline <run_id>` pins any finished run by hand, e.g. after changing `LOAD/` or the schedule, when the old baseline stops being comparable.
- `--baseline <run_id>` compares one run with another run instead.

## Pull request summary

`--markdown <file>` writes the verdict, the baseline against this run (p50/p95/p99, errors, rate, the limit for stepped runs), the steps that failed most and the report link. Post it with your CI: `gh pr comment <number> --body-file capacitylab.md`, or append it to `$GITHUB_STEP_SUMMARY`; on GitLab, a merge request note through the API.

## Parallel pipelines

One key runs one load test at a time. `--wait-for-slot 30m` waits for the key's active run to finish instead of failing with `409 run_in_progress`. It does not stop two pipelines from deploying different builds to the same staging: run the whole workflow in one concurrency group (as below), or give each pipeline its own staging and key.

## Noise

Two runs of the same release never give the same p95, and every endpoint and workflow varies differently. The service measures it for you: for each run it takes the key's last (up to five) comparable runs on that target (same `LOAD/` and schedule, no SLO stop or generator abort) and computes the spread of p95, (max − min) / median, overall, per step and per workflow, once there are at least three. The gate adds each item's own spread to `--gate-p95`; the report shows it next to each step. Until three comparable runs exist, every item gets the same `--gate-p95`, and the gate says so in a note.

## GitHub Actions

```yaml
# Trigger it as agreed with your user (e.g. on push to the main branch): every run is charged.
# Workflow level: one pipeline on staging at a time, so deploy and load test measure the same build.
concurrency: { group: staging, cancel-in-progress: false }

jobs:
  # deploy-staging: ...
  load-test:
    runs-on: ubuntu-latest
    needs: deploy-staging
    steps:
      - uses: actions/checkout@v4
      - uses: oven-sh/setup-bun@v2
      - run: bun install -g git+https://trylle.com/oleglugov09-ship-it/capacitylab-load.git
      - run: >
          capacitylab-load run ./LOAD --target "${{ vars.STAGING_URL }}" --cloud
          --profile release --tag ${{ github.sha }} --markdown capacitylab.md --wait-for-slot 30m
          ${{ github.ref == 'refs/heads/main' && '--pin-baseline' || '' }}
        env:
          CAPACITYLAB_KEY: ${{ secrets.CAPACITYLAB_KEY }}
          SHOP_TEST_PASSWORD: ${{ secrets.SHOP_TEST_PASSWORD }}   # every secret LOAD/ declares
      - if: always()
        run: test -f capacitylab.md && cat capacitylab.md >> "$GITHUB_STEP_SUMMARY" || true
```

To post the summary on the pull request instead: `gh pr comment ${{ github.event.pull_request.number }} --body-file capacitylab.md` (with `GH_TOKEN` and `pull-requests: write`). If staging deploys only from the main branch, a pull request cannot be measured there: gate after the merge, or against a preview environment that deploys the pull request.

Other CI systems need the same steps: install the CLI, run the `release` profile (or `--gate`), publish the Markdown file. `--summary summary.json` saves the raw summary for checks of your own ([results.md](/docs/results.md#summary-fields)).
