# GitHub Actions

> A copy-paste GitHub Actions workflow that runs only affected tests on pull requests and everything on main and nightly.

Pull requests run only the affected tests; `main` and a nightly schedule run everything as the safety net.

```yaml
name: tests
on:
  pull_request:
  push:
    branches: [main]
  schedule:
    - cron: "0 3 * * *"

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      # The base ref must be in the history, so fetch it all.
      - uses: actions/checkout@v6
        with: { fetch-depth: 0 }

      # Installs the latest release to ~/.local/bin. Pin it with TRIMTEST_VERSION=v0.1.0.
      - name: Install trimtest
        run: |
          curl -fsSL https://raw.githubusercontent.com/RabinApps/trimtest/main/install.sh | sh
          echo "$HOME/.local/bin" >> "$GITHUB_PATH"

      # ...your toolchain setup here (actions/setup-python, actions/setup-node, ...)...

      - name: Affected tests (pull request)
        if: github.event_name == 'pull_request'
        run: trimtest run --base origin/${{ github.base_ref }} --timeout 900

      - name: Full run (main and nightly)
        if: github.event_name != 'pull_request'
        run: trimtest run --full --timeout 1800
```

## Notes

- **`fetch-depth: 0`** is required: `--base origin/<branch>` compares against git, so the base commit must exist.
- **`--base` needs no recorded map**, so there is nothing to cache between runs. Recordings from `record --coverage` are
  tied to the commit they were made at and are ignored (with a warning) against another `--base`.
- **Keep the nightly full run.** It is the safety net behind every selection.
- **Always set a timeout.** See [Timeouts](/guides/timeouts/).
- To split a large suite across jobs see [Sharding](/guides/sharding/), and for what the exit codes mean see
  [Exit codes](/reference/exit-codes/).

## Skipping the job when nothing is affected

Run `select` first and gate later jobs on its output:

```yaml
  select:
    runs-on: ubuntu-latest
    outputs:
      any: ${{ steps.sel.outputs.any }}
      count: ${{ steps.sel.outputs.count }}
    steps:
      - uses: actions/checkout@v6
        with: { fetch-depth: 0 }
      - run: curl -fsSL https://raw.githubusercontent.com/RabinApps/trimtest/main/install.sh | sh
      - id: sel
        run: ~/.local/bin/trimtest select --base origin/${{ github.base_ref }} --format github
  test:
    needs: select
    if: needs.select.outputs.any == 'true'
    # ...
```

`--format github` appends `any`, `count` and a `matrix` of `{adapter, cwd, command}` to `$GITHUB_OUTPUT`.
