Self-Hosted GitLab Runner
GitLab CI/CD uses runners to execute pipeline jobs. You can host a runner on your own VPS for faster builds and more control.
Install GitLab Runner
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
sudo apt install -y gitlab-runnerRegister the Runner
sudo gitlab-runner registerYou will need:
- GitLab URL (e.g.,
https://gitlab.com) - Registration token (from project Settings → CI/CD → Runners)
- Executor type (shell, docker, or docker+machine)
Example .gitlab-ci.yml
stages:
- test
- build
- deploy
test:
stage: test
script:
- npm ci
- npm test
build:
stage: build
script:
- docker build -t myapp:$CI_COMMIT_SHA .
- docker push registry.example.com/myapp:$CI_COMMIT_SHA
deploy:
stage: deploy
only:
- main
script:
- ssh deploy@production "docker pull registry.example.com/myapp:$CI_COMMIT_SHA"
- ssh deploy@production "docker-compose up -d"Docker Executor
The Docker executor runs each job in a fresh container:
[[runners]]
executor = "docker"
[runners.docker]
image = "node:20"
privileged = false
volumes = ["/cache"]Security Tips
- Use the Docker executor for isolation between jobs
- Store secrets in GitLab CI/CD Variables (Settings → CI/CD → Variables)
- Never log sensitive data in CI output
- Restrict runner to specific projects if hosting multiple