Skip to content

Runners ​

A is a program installed on a machine. It regularly asks GitLab "do you have a job for me?", runs it and sends back the logs. The project uses its own runners, installed on its two .

The three runners ​

RunnerMachineTagRuns
trinity-dev-runnertrinity-devdocker, privilegednoneapi:*, web:* (lint, tests, build), docs:build
trinity-dev-deploytrinity-devshelldevdeploy-dev
trinity-prod-deploytrinity-prodshellproddeploy-prod

The arrows start from the runners: they contact GitLab, over outgoing HTTPS. No port is opened for GitLab on the servers.

Why two kinds of executor ​

CI runner (docker)Deploy runners (shell)
Where the job runsin a throwaway containerdirectly on the machine
Whyeach job starts from a clean environment (node:22-alpine, python:3.11-slim)deployment must drive the machine's Docker
Privilegedyes, for in the *:build jobsnot applicable
Docker accessthe job's dind daemonthe gitlab-runner user is in the docker group

Why a shell runner on each server, rather than a job that connects over SSH?

With SSH, a private key granting access to the server would have to be stored in GitLab's variables. Here, the runner is already on the machine: no deploy key exists, so none can leak.

Installing and configuring the runners ​

Everything is automated by the gitlab-runner Ansible role. The steps, in order:

1. Create the runners in GitLab ​

In the project: Settings → CI/CD → Runners → New project runner.

RunnerTags"Run untagged jobs"
CInonechecked
deploy devdevunchecked
deploy prodprodunchecked

GitLab shows a glrt-… token for each. Copy it right away.

A runner token is a secret

With this token, anyone can plug in a machine that will receive the project's jobs, and therefore its secret variables. It goes only into Ansible Vault.

2. Store the tokens in the Vault ​

sh
cd infra/ansible
ansible-vault edit group_vars/all/vault.yml

The decrypted file contains:

yaml
gitlab_runner_token: glrt-...          # CI runner
deploy_runner_tokens:
  trinity-dev: glrt-...                # dev deploy runner
  trinity-prod: glrt-...               # prod deploy runner

3. Run the role ​

sh
ansible-playbook playbook.yml --tags gitlab-runner --ask-vault-pass

The role:

yaml
---
- name: Add GitLab Runner GPG key
  get_url:
    url: https://packages.gitlab.com/runner/gitlab-runner/gpgkey
    dest: /etc/apt/keyrings/gitlab-runner.asc
    mode: '0644'

- name: Add GitLab Runner repository
  apt_repository:
    repo: "deb [signed-by=/etc/apt/keyrings/gitlab-runner.asc] https://packages.gitlab.com/runner/gitlab-runner/ubuntu/ {{ ansible_distribution_release }} main"
    state: present
    filename: gitlab-runner
    update_cache: yes

- name: Install GitLab Runner
  apt:
    name: gitlab-runner
    state: present

- name: Let deploy jobs use Docker
  user:
    name: gitlab-runner
    groups: docker
    append: yes

- name: Remove the logout script that makes shell jobs fail
  file:
    path: /home/gitlab-runner/.bash_logout
    state: absent

- name: Read the registered runners
  command: cat /etc/gitlab-runner/config.toml
  register: gitlab_runner_config
  changed_when: false
  failed_when: false

- name: Register the CI runner
  command: >
    gitlab-runner register
    --non-interactive
    --url "https://gitlab.com"
    --token "{{ gitlab_runner_token }}"
    --executor "docker"
    --docker-image "alpine:3.21"
    --docker-privileged
    --docker-volumes "/certs/client"
    --description "{{ gitlab_runner_name }}"
  when: "'dev' in group_names and gitlab_runner_name not in gitlab_runner_config.stdout"
  no_log: true

- name: Register the deploy runner
  command: >
    gitlab-runner register
    --non-interactive
    --url "https://gitlab.com"
    --token "{{ deploy_runner_tokens[inventory_hostname] }}"
    --executor "shell"
    --description "{{ inventory_hostname }}-deploy"
  when: (inventory_hostname + '-deploy') not in gitlab_runner_config.stdout
  no_log: true

- name: Start and enable GitLab Runner
  systemd:
    name: gitlab-runner
    state: started
    enabled: yes
TaskWhy
Official APT repositoryTo get GitLab's version, not Ubuntu's
gitlab-runner in the docker groupThe shell runner runs docker compose and docker stack
Removing .bash_logoutOn Ubuntu, this file makes every shell job fail at the end ("prepare environment")
Reading config.toml before registeringMakes the task : re-running the playbook does not register a duplicate runner
--docker-privileged and --docker-volumes /certs/clientNeeded for Docker-in-Docker with TLS in the *:build jobs
no_log: trueThe token does not appear in Ansible's output

4. Check the connection ​

On the server:

sh
sudo gitlab-runner verify
sudo gitlab-runner list

In GitLab, Settings → CI/CD → Runners: all three runners must have a green dot ("online").

For end-to-end proof, push a commit to a feature/* branch that changes web/: web:lint and web:test must run on trinity-dev-runner (the runner name is written at the top of the job log).

Disable GitLab's shared runners

In Settings → CI/CD → Runners, disable "Instance runners". Otherwise, an untagged job can run on a GitLab.com runner, and the proof that our runners work is no proof at all.

Runner troubleshooting is in Troubleshooting.