# Running in CI

In CI you run the same four things you run locally, in order: the server, `channelstack env`, your app, and the tests. This page has a GitHub Actions job for Vitest and for pytest, a nightly live job, and what to look at when a job hangs.

## The job

```yaml title=".github/workflows/agent.yml"
name: Agent tests
on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: npm
      - run: npm ci

      - name: Start channelstack
        env:
          CHANNELSTACK_SLACK_WEBHOOK_URL: http://localhost:3000/slack/events
        run: |
          npx channelstack start --no-portless --port 4500 > channelstack.log 2>&1 &
          timeout 30 bash -c 'until npx channelstack env > /dev/null 2>&1; do sleep 1; done'

      - name: Point the agent at it
        run: npx channelstack env >> "$GITHUB_ENV"

      - name: Start the agent
        env:
          PORT: 3000
        run: |
          node agent.js > agent.log 2>&1 &
          timeout 30 bash -c 'until curl -s -o /dev/null http://localhost:3000/; do sleep 1; done'

      - run: npx vitest run

      - if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: logs
          path: "*.log"
```

What each step does:

1. **Start channelstack.** `--no-portless` serves every fake on one loopback port over plain HTTP (`http://127.0.0.1:4500/slack`, …), so the runner needs no portless and no CA. The server reads `CHANNELSTACK_<CHANNEL>_WEBHOOK_URL` when it starts, so set it on this step; a Socket Mode agent connects out and needs none. The loop waits until the server has written `~/.channelstack/server.json` and answers.
2. **Point the app at it.** `channelstack env` prints the fake tokens, signing secrets and API URLs as `KEY=value` lines. Appending them to `$GITHUB_ENV` makes them part of every later step's environment. They are fake values, made fresh for this runner.
3. **Start the app** in the background, unchanged, and wait until its port answers.
4. **Run the tests.** The Vitest plugin finds the running server through `server.json` and gives each test its own session.

Make the plugin refuse to start a server of its own in CI. In `auto` mode (the default), a missing server makes it start a private one, which your app doesn't know about, and every test would time out instead of failing at once:

```ts title="vitest.config.ts"
import { defineConfig } from "vitest/config";
import channelstack from "channelstack/vitest";

export default defineConfig({
  plugins: [channelstack({ mode: process.env.CI ? "attach" : "auto" })],
});
```

The server, its logs and its state are gone when the job ends. Nothing needs cleaning up in fake mode.

### With pytest

The server comes from npm in a Python project too. Install it globally, so the Python SDK finds `channelstack` on the `PATH`:

```yaml title=".github/workflows/agent.yml"
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
      - uses: actions/setup-python@v5
        with:
          python-version: "3.13"
          cache: pip
      - run: npm install --global channelstack
      - run: pip install -r requirements.txt pytest-channelstack

      - name: Start channelstack
        env:
          CHANNELSTACK_SLACK_WEBHOOK_URL: http://localhost:3000/slack/events
        run: |
          channelstack start --no-portless --port 4500 > channelstack.log 2>&1 &
          timeout 30 bash -c 'until channelstack env > /dev/null 2>&1; do sleep 1; done'
      - run: channelstack env >> "$GITHUB_ENV"

      - name: Start the agent
        env:
          PORT: 3000
        run: |
          python agent.py > agent.log 2>&1 &
          timeout 30 bash -c 'until curl -s -o /dev/null http://localhost:3000/; do sleep 1; done'

      - run: pytest --channelstack-mode attach
```

`--channelstack-mode attach` does what `mode: "attach"` does for Vitest; `channelstack_mode = attach` in your pytest ini settings does the same ([pytest](https://channelstack.dev/docs/guide/pytest)).

## Caching

- **npm:** `actions/setup-node` with `cache: npm` caches the npm cache, which holds the server binary too: it comes with `channelstack` as an optional dependency for the runner's platform (`@channelstack/server-linux-x64` on `ubuntu-latest`). Don't install with `--no-optional`, or the binary is missing.
- **pip:** `actions/setup-python` with `cache: pip`.
- **Don't cache `~/.channelstack`.** It holds the install's fake secrets and, for live runs, saved browser sessions. A fresh runner makes new secrets, and `channelstack env` prints them in the same job.

## A nightly live job

The same tests run against real sandboxes when the credentials are set ([Live mode](https://channelstack.dev/docs/guide/live-mode)). Run that on a schedule, never on pull requests, so a fork's code never sees the secrets:

```yaml title=".github/workflows/live.yml"
name: Live
on:
  schedule:
    - cron: "17 3 * * *"
  workflow_dispatch:

concurrency:
  group: live            # one run at a time: the sandbox and its rate limits are shared
  cancel-in-progress: false

jobs:
  live:
    runs-on: ubuntu-latest
    environment: live    # the secrets live in a protected environment
    timeout-minutes: 30
    env:
      CHANNELSTACK_RUN_ID: gh${{ github.run_id }}
      CHANNELSTACK_LIVE_SLACK_TEAMS: ${{ vars.CHANNELSTACK_LIVE_SLACK_TEAMS }}
      CHANNELSTACK_SLACK_APP_ID: ${{ secrets.CHANNELSTACK_SLACK_APP_ID }}
      CHANNELSTACK_SLACK_BOT_TOKEN: ${{ secrets.CHANNELSTACK_SLACK_BOT_TOKEN }}
      CHANNELSTACK_SLACK_USER_ANA_TOKEN: ${{ secrets.CHANNELSTACK_SLACK_USER_ANA_TOKEN }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: npm
      - run: npm ci

      - name: Restore Ana's browser session
        env:
          SESSION: ${{ secrets.SLACK_ANA_BROWSER_SESSION }}
        run: |
          mkdir -p ~/.channelstack/browser
          (umask 077; printf '%s' "$SESSION" > ~/.channelstack/browser/slack-ana.json)

      - run: npx channelstack doctor

      - name: Start channelstack
        run: |
          node_modules/.bin/channelstack start --no-portless --port 4500 > channelstack.log 2>&1 &
          echo $! > channelstack.pid
          timeout 30 bash -c 'until npx channelstack env > /dev/null 2>&1; do sleep 1; done'

      - name: Start the agent with its real app, in Socket Mode
        env:
          SLACK_BOT_TOKEN: ${{ secrets.CHANNELSTACK_SLACK_BOT_TOKEN }}
          SLACK_APP_TOKEN: ${{ secrets.SLACK_APP_TOKEN }}
        run: node agent.js > agent.log 2>&1 &

      - run: npx vitest run

      - name: Stop channelstack
        if: always()
        run: kill "$(cat channelstack.pid)" && sleep 5

      - name: Check that the run left nothing behind
        if: always()
        run: npx channelstack sweep --check --run "$CHANNELSTACK_RUN_ID"

      - name: Remove what it left
        if: failure()
        run: npx channelstack sweep --run "$CHANNELSTACK_RUN_ID"
```

How it differs from the fake job:

- **Credentials are job-level `env`,** so the server (app credentials, allowlists, `CHANNELSTACK_RUN_ID`), the test process (people's tokens) and `doctor` and `sweep` all see them. Allowlists aren't secret, so they can be repository variables.
- **`CHANNELSTACK_RUN_ID`** tags everything the run creates, and a re-run of the job keeps it. It must be 1 to 24 letters and digits. A run of 7 or more digits, like GitHub's run number, goes into names in base 36 (Linear takes it for a phone number otherwise), and `sweep --run` finds the run by the same id.
- **The app runs with its real app**, not with `channelstack env` (which prints nothing for a live channel). Real Slack delivers to it; Socket Mode means the runner needs no public URL.
- **The browser actor** needs each person's saved session: run `channelstack login slack --user ana` once on your machine and store the file's contents as a secret. `ubuntu-latest` has Chrome.
- **`doctor`** fails the job before the tests when a credential is wrong or a session has expired.
- **Stopping the server** closes what its sessions still hold; then `sweep --check` fails the job if anything of this run is left, and `sweep` removes it.

Tests that use fake-only features skip with the reason, and a setup that can't work fails, so a broken setup turns the job red instead of skipping everything ([what skips and what fails](https://channelstack.dev/docs/guide/live-mode#what-skips-and-what-fails)).

## If the job hangs

A matcher waits 10 seconds by default and a negative check waits for the app to be quiet, so a single test doesn't hang for long. When the job does, look at these in order:

- **The server never came up.** The `until` loop times out after 30 seconds. `channelstack.log` says why, for example that a server is already running from the same `server.json`, or that portless isn't installed (start with `--no-portless`).
- **The app never came up, or crashed.** Its wait loop times out, or every test fails with no reply. Read `agent.log`: a missing variable usually means the `channelstack env` step ran in a different job, or before the server was up.
- **Events don't reach the app.** `CHANNELSTACK_SLACK_WEBHOOK_URL` wasn't set on the step that started the server, or it doesn't match the app's port and path.
- **Every test waits its full timeout.** Start the server with `--log-level debug`: it logs each delivery attempt and how it ended (`unreachable`, `timed_out`, a status code). Logs are redacted, so they can go into an artifact.
- **Live runs are slower.** A real model and real rate limits take longer: give positive matchers a longer `timeout`. The first browser actor start on a fresh runner can take a minute or two while the matching Chrome driver is fetched.

[Troubleshooting](https://channelstack.dev/docs/guide/troubleshooting) has the symptoms in more detail.
