# Run your CI runner on a LiveTerminal VPS

A VPS can run the CI jobs of your GitLab or GitHub projects. You install the runner program from
GitLab or GitHub on the VPS; it connects out to GitLab or GitHub, picks up jobs and runs them. It
needs no open ports.

:::note
The GitLab commands below were run on a LiveTerminal VPS, with a tagged job that passed. The
GitHub steps follow GitHub's documentation; we checked that the runner downloads and starts on a
VPS, but have not registered it with a GitHub repository yet.
:::

## Create a VPS for the runner

Use a VPS only for CI (see [Security](#security)). 2 vCPUs, 4 GiB of RAM and 40 GiB of disk is a
good start for Docker jobs; the disk fills with Docker images, so give it more if your jobs use
large images.

```sh
lt vps create --name ci-runner --vcpus 2 --memory-gib 4 --root-disk-gib 40 --wait
lt ssh-config
ssh ci-runner.lt.internal
```

The commands in the rest of this page run on the VPS.

## Install Docker

Both runners below run jobs in Docker containers. Install Docker Engine from Docker's repository
([Docker's instructions for Ubuntu](https://docs.docker.com/engine/install/ubuntu/)):

```sh
sudo apt-get update
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo docker run --rm hello-world
```

## GitLab Runner

### Create the runner in GitLab

In your project, open **Settings > CI/CD > Runners** and choose **New project runner** (for a
group: the group's **Build > Runners**, **New group runner**). Enter the tags the runner should
take jobs for, for example `liveterminal`, or tick **Run untagged jobs**. Choose **Create
runner**. GitLab shows a runner authentication token that starts with `glrt-`; you need it in
the next step. Tags are set here in GitLab, not on the VPS.

### Install and register it

Install GitLab Runner from GitLab's package repository
([GitLab's instructions](https://docs.gitlab.com/runner/install/linux-repository/)):

```sh
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" -o script.deb.sh
less script.deb.sh   # read it before you run it
sudo bash script.deb.sh
sudo apt-get install -y gitlab-runner
```

The package is large (it includes helper images); the install takes about a minute. It starts the
`gitlab-runner` service.

Register the runner with the token from GitLab, with the Docker executor
([GitLab's instructions](https://docs.gitlab.com/runner/register/)):

```sh
sudo gitlab-runner register \
  --non-interactive \
  --url "https://gitlab.com" \
  --token "glrt-..." \
  --executor docker \
  --docker-image alpine:latest \
  --description "ci-runner"
sudo gitlab-runner verify
```

For GitLab Self-Managed, use your instance's URL instead of `https://gitlab.com`. The runner then
shows as online in GitLab. `--docker-image` is the image for jobs that don't name one.

### Send a job to it

A job runs on the runner when its tags match:

```yaml
# .gitlab-ci.yml
hello:
  tags: [liveterminal]
  image: alpine:latest
  script:
    - echo "Running on $(hostname)"
```

### Remove it

```sh
sudo gitlab-runner unregister --name ci-runner
```

This removes the runner from the VPS. The runner stays listed in GitLab; delete it there too, under
**Settings > CI/CD > Runners**.

## GitHub Actions runner

:::caution
We have not run these steps against a GitHub repository yet. They follow GitHub's documentation:
[Adding self-hosted runners](https://docs.github.com/en/actions/how-tos/manage-runners/self-hosted-runners/add-runners),
[Configuring the runner as a service](https://docs.github.com/en/actions/how-tos/manage-runners/self-hosted-runners/configure-the-application),
[Labels](https://docs.github.com/en/actions/how-tos/manage-runners/self-hosted-runners/apply-labels).
:::

### Get a registration token

In your repository, open **Settings > Actions > Runners** and choose **New self-hosted runner**
(for an organization: the organization's **Settings > Actions > Runners**). Pick **Linux** and
**x64**. GitHub shows the commands to download the runner and a registration token; the token
expires after one hour.

### Install and register it

Run the download commands GitHub shows, as the user `dev` (the runner refuses to be set up as
root). For version 2.338.2 they are:

```sh
mkdir actions-runner && cd actions-runner
curl -o actions-runner-linux-x64-2.338.2.tar.gz -L https://github.com/actions/runner/releases/download/v2.338.2/actions-runner-linux-x64-2.338.2.tar.gz
tar xzf ./actions-runner-linux-x64-2.338.2.tar.gz
```

Then register it. `--labels` adds your own labels to the default `self-hosted`, `linux` and `x64`:

```sh
./config.sh --unattended \
  --url https://github.com/OWNER/REPO \
  --token REGISTRATION_TOKEN \
  --name ci-runner \
  --labels liveterminal
```

Let the runner's jobs use Docker, then run the runner as a systemd service, so it keeps running
after you disconnect and starts again after a restart:

```sh
sudo usermod -aG docker dev
sudo ./svc.sh install dev
sudo ./svc.sh start
sudo ./svc.sh status
```

### Send a job to it

```yaml
# .github/workflows/ci.yml
on: push
jobs:
  hello:
    runs-on: [self-hosted, liveterminal]
    steps:
      - run: echo "Running on $(hostname)"
```

### Remove it

In **Settings > Actions > Runners**, open the runner and choose **Remove**; GitHub shows a removal
token. On the VPS, in `actions-runner`:

```sh
sudo ./svc.sh stop
sudo ./svc.sh uninstall
./config.sh remove --token REMOVAL_TOKEN
```

GitHub removes a runner by itself when it has not connected for 14 days.

## Keep them updated

- **GitLab Runner** updates with the system packages: `sudo apt-get update && sudo apt-get install
-y gitlab-runner`. Keep it close to your GitLab version.
- **GitHub Actions runner** updates itself when a new version comes out, unless you registered it
  with `--disableupdate`. A runner that is too old stops getting jobs.
- **Docker and Ubuntu**: run `sudo apt-get update && sudo apt-get upgrade -y` from time to time.

## Security

A CI job runs whatever code is in the commit it builds, and on this VPS it can reach everything
the runner can.

- **Use a VPS only for CI.** Don't keep other work, keys or agent logins on it. A job in a Docker
  container can get root on the VPS through Docker.
- **Don't run jobs from people you don't trust.** Don't attach the runner to a public project or
  repository, and don't let it run jobs for merge requests or pull requests from forks: anyone who
  can open one can run code on your VPS and read what it can read. GitHub says self-hosted runners
  should almost
  [never be used for public repositories](https://docs.github.com/en/actions/reference/security/secure-use).
- **Secrets.** CI variables and secrets reach the jobs that run on the VPS, and files the jobs
  write stay on its disk. Use protected or environment-scoped secrets, and delete the VPS (with
  `lt vps delete --now`) when you stop using it.
- **Tokens.** The `glrt-` token and the GitHub registration token let a machine join as your
  runner. Don't commit them; GitLab Runner keeps its token in `/etc/gitlab-runner/config.toml`.

## What it costs

A runner VPS is billed like any other VPS: its vCPUs and RAM for the time it runs, whether jobs
run or not, its disk for as long as it exists, and the traffic it sends (for example artifacts and
caches it uploads). Nothing extra is charged for CI. A stopped VPS (`lt vps stop ci-runner`) runs
no jobs and is billed only for its disk; `lt vps start ci-runner` brings the runner back.