# Run CI jobs in throwaway clones

GitHub Actions and GitLab CI jobs run in fresh clones of a Linux, Windows, or macOS template, one clone per job.

Requires [OSMates Pro](https://osmates.com/docs/editions#what-pro-adds).

## Prepare a template for GitHub

1. Set up a machine with the tools your jobs need, such as git.
2. Download GitHub's arm64 runner into `actions-runner` in the machine's home folder, as GitHub's download steps show. On Windows, use `C:\actions-runner`.
3. Skip `config.sh`. Each clone registers its own runner.
4. Shut the machine down, then make it a template, with its name for `<machine>`:

```sh
osmates template make <machine>
```

## Add a GitHub runner pool

1. On GitHub, create a fine-grained token with the repository's **Administration** permission set to **Read and write**. For an organization's runners, use **Self-hosted runners** instead.
2. Save the token as a credential, and paste it when asked:

```sh
osmates credential add github-runners --host api.github.com --header Authorization --prefix "Bearer "
```

3. Add the pool, with your template and repository:

```sh
osmates runner add linux-ci --from <template> --github <owner>/<repository> --credential github-runners --slots 2
```

Each slot is a clone with a runner waiting for a job. When the job ends, the clone and its runner are deleted, and a new clone takes the slot.

In your workflow, choose the runners by their labels:

```yaml
runs-on: [self-hosted, Linux, ARM64]
```

Windows runners have the label `Windows`, and macOS runners `macOS`. Add your own labels with `--label`.

## Remove a pool

```sh
osmates runner list
osmates runner remove linux-ci
```

`runner list` shows each pool and its slots. Removing a pool cancels its running jobs, which GitHub marks as failed.

## Run GitLab jobs

GitLab Runner runs on your Mac and calls OSMates for a clone for each job.

1. Install git and `gitlab-runner` in the machine. Artifacts and caches need `gitlab-runner` on the `PATH`, and Windows also needs PowerShell 7.
2. Make the machine a template, as for GitHub.
3. Install GitLab Runner on your Mac, and register a runner with the `custom` executor.
4. In GitLab Runner's `config.toml`, point the runner's stages at OSMates:

```toml
[[runners]]
  executor = "custom"
  builds_dir = "/builds"
  cache_dir = "/cache"
  [runners.custom]
    config_exec = "/Applications/OSMates.app/Contents/Helpers/osmates"
    config_args = ["runner", "gitlab", "config", "--from", "<template>"]
    prepare_exec = "/Applications/OSMates.app/Contents/Helpers/osmates"
    prepare_args = ["runner", "gitlab", "prepare"]
    run_exec = "/Applications/OSMates.app/Contents/Helpers/osmates"
    run_args = ["runner", "gitlab", "run"]
    cleanup_exec = "/Applications/OSMates.app/Contents/Helpers/osmates"
    cleanup_args = ["runner", "gitlab", "cleanup"]
```

5. Restart GitLab Runner.

The clone is deleted when the job ends, fails, or is canceled. If GitLab Runner stops first, OSMates deletes the clone itself. For a macOS template, add `limit = 2` to the runner's section, so a third job waits in GitLab.

## Limits

- Your Mac runs two macOS clones at once, across every pool and job.
- A pool has 1 to 16 slots, and a macOS pool at most 2.
- A job waits for its clone to start, unless a slot was ready before the job came.
- An outdated runner in the template updates itself in each clone, before every job.
- GitHub's runner for Windows on arm64 is a preview at GitHub.
