> ## Documentation Index
> Fetch the complete documentation index at: https://empirical.run/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# From CI/CD pipelines

> Trigger a test run from GitHub Actions or any CI/CD provider after a deployment, with build metadata, variables and concurrency

Trigger a test run from your pipeline after each deployment, so every build is tested against the
environment it deployed to. The run report links back to the commit and branch that triggered it.

## Before you begin

Create a project API key under [Settings → API keys](https://empirical.run/settings/api-keys), and
store it as a secret named `EMPIRICALRUN_KEY` in your CI provider. Every method on this page uses it.

## GitHub Actions

With GitHub Actions, add the [dispatch action](https://github.com/empirical-run/dispatch-action) after
your deploy step:

```yaml theme={null}
- name: Deploy step
  id: deploy-step
  run: ...

- name: Dispatch for tests
  uses: empirical-run/dispatch-action@v1
  with:
    auth-key: ${{ secrets.EMPIRICALRUN_KEY }}
    environment: production
    environment-variables: |
      BUILD_URL: ${{ steps.deploy-step.outputs.url }}
    concurrency: |
      group: production/acme/web
      on-conflict: wait
```

`v1` receives backwards-compatible fixes, and breaking changes ship as a new major version. To pin
an exact release, use a tag such as `v1.0.0`.

Use the `environment-variables` input to set environment variables for the test
run, one per line in `NAME: value` format. These override the environment's
configured variables. The URL of the build under test is passed to tests as the
`BUILD_URL` environment variable.

Set `group` and `on-conflict` in the `concurrency` input to control what happens
when the group already has an active run. For preview deployments, a group such
as `preview/acme/web@${{ github.head_ref }}` with `cancel` lets a new push
supersede the previous run for that branch. For production, use an
environment/repository group with `wait` to prevent deployments from testing
concurrently. See [Concurrency](https://empirical.run/docs/features/concurrency-limits.md).

> [!WARNING]
>
> The `build-url` input is deprecated. Set `BUILD_URL` with `environment-variables`. See
> [Deprecations](https://empirical.run/docs/deprecations.md#build-url-input-of-the-dispatch-action).

### Vercel deployments

For Vercel deployments, trigger tests on the `deployment_status` event. You can
copy this workflow into `.github/workflows/trigger-e2e.yaml`:

```yaml theme={null}
name: Trigger e2e tests

on:
  workflow_dispatch:
  deployment_status:

jobs:
  trigger-e2e-tests-production:
    if: github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'deployment_status' &&
          github.event.deployment_status.state == 'success' &&
          github.event.deployment_status.environment == 'Production')
    runs-on: ubuntu-latest
    steps:
      - name: Dispatch for tests
        uses: empirical-run/dispatch-action@v1
        with:
          auth-key: ${{ secrets.EMPIRICALRUN_KEY }}
          environment: production
          environment-variables: |
            BUILD_URL: ${{ github.event.deployment_status.target_url }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

The `GITHUB_TOKEN` environment variable lets the action pull branch info for
the deployment, which is attached to the test run report.

## Other providers

With any other CI/CD provider, trigger a run with the [Trigger a test run](https://empirical.run/docs/api-reference/test-runs/trigger-a-test-run.md) API.

### Minimal snippet

```sh theme={null}
curl -X PUT \
  https://api.empirical.run/api/test-runs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $EMPIRICALRUN_KEY" \
  -d '{ "environment": "production" }'
```

### Metadata for reporting

Add build details and metadata to show them on the run report:

* `build`: the repository, commit and branch that triggered the run
* `metadata`: any other key-value pairs
* `environment_variables_overrides`: variables for this run, such as `BUILD_URL`

```sh theme={null}
jq -n \
  --arg repo "$REPOSITORY" \
  --arg commit "$COMMIT" \
  --arg branch "$BRANCH" \
  --arg build_url "$BUILD_URL" \
  '{
    environment: "production",
    build: {
      repo: $repo,
      commit: $commit,
      branch: $branch
    },
    metadata: {
      pairs: "of",
      key: "value"
    },
    environment_variables_overrides: [
      {name: "BUILD_URL", value: $build_url},
      {name: "MY_VAR", value: "my_value"}
    ]
  }' | curl -X PUT \
  https://api.empirical.run/api/test-runs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $EMPIRICALRUN_KEY" \
  --data-binary @-
```

### Pull request runs

For a pre-merge run on an application pull request, send `trigger` with the pull request. The
run uses the matching test branch, merged into the default branch. See
[Branching](https://empirical.run/docs/features/branching.md#pull-request-runs).

```sh theme={null}
jq -n \
  --arg commit "$COMMIT" \
  --arg head "$PR_HEAD_BRANCH" \
  --arg base "$PR_BASE_BRANCH" \
  --argjson number "$PR_NUMBER" \
  '{
    environment: "preview",
    build: { repo: "acme/web", branch: $head, commit: $commit },
    trigger: {
      event: "pull_request",
      pull_request: { number: $number, head: $head, base: $base }
    }
  }' | curl -X PUT \
  https://api.empirical.run/api/test-runs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $EMPIRICALRUN_KEY" \
  --data-binary @-
```

The [dispatch-action](#github-actions) sends `trigger` automatically on `pull_request` workflow events.

### Concurrency

Tell Empirical what to do when a run for the same thing is already going. Runs that share a
`concurrency.group` (within the project) execute one at a time; `on_conflict` is what the
new run does when the group is busy. See [Concurrency](https://empirical.run/docs/features/concurrency-limits.md).

```sh theme={null}
jq -n \
  --arg branch "$BRANCH" \
  --arg build_url "$BUILD_URL" \
  '{
    environment: "preview",
    build: { branch: $branch, repo: "acme/web" },
    environment_variables_overrides: [{name: "BUILD_URL", value: $build_url}],
    concurrency: {
      group: ("preview/acme/web@" + $branch),
      on_conflict: "cancel"
    }
  }' | curl -X PUT \
  https://api.empirical.run/api/test-runs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $EMPIRICALRUN_KEY" \
  --data-binary @-
```

A new push to the same branch cancels the run still going for the previous push. Use
`"on_conflict": "wait"` to queue behind it instead, or the deployment URL as the group when
one server can only take one run at a time.

### Reporters

Choose which reporters a run posts to with `reporters`. Leave it out to use the environment's
reporters. This posts the GitHub check and PR comment for a pull request build, but no Slack
message:

```sh theme={null}
jq -n \
  --arg commit "$COMMIT" \
  --arg branch "$BRANCH" \
  '{
    environment: "preview",
    build: { repo: "acme/web", branch: $branch, commit: $commit },
    reporters: [
      { reporter_type: "github_check" },
      { reporter_type: "github_pr_comment" }
    ]
  }' | curl -X PUT \
  https://api.empirical.run/api/test-runs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $EMPIRICALRUN_KEY" \
  --data-binary @-
```

Add `{ reporter_type: "slack_run_status", slack_channels: ["C0123456789"] }` to also post to a
Slack channel. Use the [List reporters API](https://empirical.run/docs/api-reference/reporter-configuration/list-reporters.md) to
find your channel ids and the repositories the GitHub Reporter covers. See
[Report notifications](https://empirical.run/docs/platform/test-run-configuration.md#report-notifications).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.