← logbuild #5 passede72bcaa
GitLab Runner Performance Optimization: From Slow Pipelines to Speed
A practical guide to make GitLab Runner faster: direct internal connections, Docker executor tuning, caching, and runner configuration.
· 14 min read
A simple lint check takes 5 minutes, not 30 seconds. Your runner uses almost no CPU. (A GitLab Runner is the program that runs your pipeline jobs.) The problem isn’t hardware. It’s configuration.
These changes cut our pipeline times by 60%. We did not need new hardware.
In Simple Words#
- The problem
- A simple lint check takes 5 minutes, not 30 seconds. The runner VM shows almost no CPU load.
- The root cause
- The runner reaches GitLab through Cloudflare on the public internet. But both VMs are on the same local network. Every job also installs all packages again.
- The solution
- Point the runner at the internal IP of GitLab. Cache
node_modulesand run independent jobs in parallel withneeds. Tune the runner settings. - Think of it like this
- Your colleague sits in the next room, but you send every letter through a post office in another city. Walk to the next room and hand over the letter. Also keep your tools on your desk, so you do not go to the shop before every task.
Why Pipelines Are Slow (It’s Not Your CPU)#
Our first runner setup was very slow. Both VMs showed almost no load in htop. The real bottlenecks (the slowest parts) were:
- Network latency through Cloudflare - The runner used the public internet to reach GitLab
- No caching - Every job ran
npm ciand installed everything again - Default runner config - Careful default settings made the runner slower than it can be
- DNS resolution delays - Docker containers couldn’t resolve internal hostnames
Fix these and you get:
- 50-70% faster pipelines - Direct connections + caching
- No more “Pending” jobs - The runner picks up work at once
- Predictable build times - Cache hits = the same speed every time
- Lower resource usage - Less network traffic, fewer repeated operations
Our Infrastructure Setup#
Two Proxmox VMs on the same local network. Access from outside goes through Cloudflare Tunnel.
| Component | Specs | IP Address |
|---|---|---|
| GitLab Server | 4 vCPUs, 12GB RAM, 80GB SSD | 192.168.1.10 |
| GitLab Runner | 4 vCPUs, 8GB RAM, 80GB SSD | 192.168.1.11 |
| External Access | Cloudflare Tunnel | gitlab.example.com |
- The runner and the GitLab server are two VMs on one local network. People outside reach GitLab through Cloudflare.
- The runner uses the public name
gitlab.example.com. This name points to Cloudflare, so the traffic goes out to the internet and comes back. - Set
urlandclone_urlinconfig.tomltohttp://192.168.1.10/. The runner now talks to GitLab directly. - Job containers still use the public name.
extra_hostsmakes them resolvegitlab.example.comto192.168.1.10too. - All traffic stays on the local network. A git clone takes 2-5 seconds, not 15-30.
The problem: The runner resolved gitlab.example.com to Cloudflare IPs. Traffic went out to the internet and back, even though both VMs sit on the same network.
Optimization 1: Direct Internal Connection#
This change helps the most. Send runner traffic directly to GitLab over the local network.
The Problem#
Check your runner logs:
sudo journalctl -u gitlab-runner -n 20 --no-pagerIf you see Cloudflare IPs, your traffic takes the long path through the internet:
dial tcp 104.21.9.142:443: i/o timeoutThose IPs (104.21.x.x, 172.67.x.x) are Cloudflare, not your GitLab server.
The Fix#
Step 1: Find what port GitLab is listening on
On your GitLab server:
ss -tlun | grep -E '80|443|5443'Typical output:
tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:*Inside your network, GitLab listens on port 80 (HTTP).
Step 2: Update runner config.toml
On the runner VM, edit /etc/gitlab-runner/config.toml:
[[runners]]
name = "gitlab-runner-01"
url = "http://192.168.1.10/"
clone_url = "http://192.168.1.10/"
# ... rest of configThe important changes:
url- Internal IP with HTTP (not HTTPS)clone_url- Makes git operations use the internal network too
Step 3: Configure Docker containers to resolve hostnames
Containers need to reach GitLab too. Add extra_hosts:
[runners.docker]
extra_hosts = ["gitlab.example.com:192.168.1.10", "registry.example.com:192.168.1.10"]This adds /etc/hosts entries to every container, so gitlab.example.com resolves to your internal IP.
Step 4: Restart the runner
sudo gitlab-runner restart
sudo journalctl -u gitlab-runner -fJob checks should now succeed without timeouts.
Before vs After#
| Metric | Before (via Cloudflare) | After (Direct) |
|---|---|---|
| Git clone | 15-30 seconds | 2-5 seconds |
| Artifact upload | 10-20 seconds | 1-3 seconds |
| Cache restore | 20-40 seconds | 5-10 seconds |
| Total pipeline | 5-8 minutes | 2-3 minutes |
Optimization 2: Runner Resource Configuration#
The defaults are careful and slow. Tune them.
Recommended config.toml#
concurrent = 2
check_interval = 3
connection_max_age = "15m0s"
shutdown_timeout = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "gitlab-runner-01"
url = "http://192.168.1.10/"
clone_url = "http://192.168.1.10/"
executor = "docker"
request_concurrency = 2
[runners.cache]
MaxUploadedArchiveSize = 0
[runners.docker]
tls_verify = false
image = "alpine:latest"
privileged = true
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache"]
shm_size = 536870912
network_mtu = 0
cpus = "1.5"
memory = "2560m"
pull_policy = ["if-not-present"]
extra_hosts = ["gitlab.example.com:192.168.1.10", "registry.example.com:192.168.1.10"]Key Settings Explained#
| Setting | Value | Why |
|---|---|---|
concurrent |
2 | Run 2 jobs at the same time (adjust based on RAM) |
check_interval |
3 | Poll for jobs every 3 seconds |
request_concurrency |
2 | Fixes “long polling” warning |
cpus |
”1.5” | Give 1.5 CPUs to each container |
memory |
”2560m” | 2.5GB per container |
shm_size |
536870912 | 512MB shared memory (enough for Node.js) |
pull_policy |
”if-not-present” | Don’t re-pull images every time |
Memory Budget Calculation#
For a runner with 8GB RAM:
System/Docker overhead: ~1.5GB
Runner process: ~0.5GB
Container 1: 2.5GB
Container 2: 2.5GB
Buffer: 1.0GB
─────────────────────────────────
Total: 8.0GB ✓Optimization 3: Pipeline Caching Strategy#
npm ci on every job wastes 30-60 seconds. Cache it. A cache keeps files from one job, so the next job can use them again.
The Problem#
Without caching, every job:
- Downloads packages from npm registry
- Installs all dependencies again
- Repeats this even when
package-lock.jsonhasn’t changed
The Solution: Dedicated Install Stage#
image: node:24.12.0-trixie-slim
stages:
- install
- lint
- build
- test
- deploy
variables:
NPM_CONFIG_CACHE: .npm
npm_config_prefer_offline: 'true'
npm_config_audit: 'false'
npm_config_fund: 'false'
# Global cache - all jobs can pull from this
cache:
key:
files:
- package-lock.json
paths:
- .npm/
- node_modules/
policy: pull # Most jobs only read cache
# ===============================
# STAGE: INSTALL
# ===============================
install_deps:
stage: install
cache:
key:
files:
- package-lock.json
paths:
- .npm/
- node_modules/
policy: pull-push # This job updates the cache
script:
- npm ci
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCHHow Other Jobs Use the Cache#
lint_code:
stage: lint
needs:
- job: install_deps
optional: true
script:
- '[ -d node_modules ] || npm ci' # Fallback if cache miss
- npm run lint
- npm run format:check
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH =~ /^(develop|main)$/
build_site:
stage: build
needs:
- job: install_deps
optional: true
script:
- '[ -d node_modules ] || npm ci'
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 day
rules:
- if: $CI_COMMIT_BRANCHCache Key Strategy#
The cache key is based on package-lock.json:
cache:
key:
files:
- package-lock.json- Same
package-lock.json= cache hit (the cache is found) = fast - Changed
package-lock.json= cache miss (no cache is found) = full install (expected)
Cache Hit vs Miss Performance#
| Scenario | npm ci Time | Total Job Time |
|---|---|---|
| Cache miss (first run) | 45-60 seconds | 70-90 seconds |
| Cache hit (later runs) | 0 seconds | 15-25 seconds |
| Partial cache hit | 10-20 seconds | 30-45 seconds |
Optimization 4: Use needs for Parallel Execution#
By default, GitLab runs stages one after another. With needs, jobs can run in parallel (at the same time).
Without needs (Sequential)#
- Without
needs, the five jobs run one after another. The pipeline takes 180 seconds. - With
needs,lintandbuildboth start wheninstallis done. They run at the same time. deploystarts whenlintandtestare done. The pipeline ends sooner.
With needs (Parallel)#
lint_code:
needs:
- job: install_deps
optional: true # Don't fail if install_deps was skipped
build_site:
needs:
- job: install_deps
optional: true
test_build:
needs:
- job: build_site
artifacts: true # Download artifacts from build_site- Without
needs, the five jobs run one after another. The pipeline takes 180 seconds. - With
needs,lintandbuildboth start wheninstallis done. They run at the same time. deploystarts whenlintandtestare done. The pipeline ends sooner.
Jobs that do not depend on each other run in parallel. The total time drops.
Optimization 5: Sync Develop After Production Deploy#
This prevents the “source branch is X commits behind target” message in future merge requests.
# ===============================
# STAGE: POST-DEPLOY
# ===============================
# Add GITLAB_INTERNAL_IP as a CI/CD variable (e.g., 192.168.1.10)
sync_develop:
stage: post-deploy
image: alpine:latest
variables:
GIT_STRATEGY: clone
GIT_DEPTH: 0
before_script:
- apk add --no-cache git
- git config user.email "[email protected]"
- git config user.name "GitLab CI"
- git remote set-url origin "http://oauth2:${PUSH_TOKEN}@${GITLAB_INTERNAL_IP}/${CI_PROJECT_PATH}.git"
script:
- git fetch origin develop
- git checkout develop
- git merge origin/main --no-edit
- git push origin develop
rules:
- if: $CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE == "push"
allow_failure: trueWhy Use Internal IP in sync_develop?#
CI_SERVER_HOST resolves to gitlab.example.com (external URL) on port 443 (Cloudflare HTTPS). Inside a Docker container, that traffic goes through Cloudflare, which is slow and unreliable. The internal IP keeps git operations on the local network.
Complete Optimized .gitlab-ci.yml#
Everything together:
image: node:24.12.0-trixie-slim
stages:
- install
- lint
- build
- test
- deploy
- post-deploy
default:
interruptible: true
variables:
NPM_CONFIG_CACHE: .npm
npm_config_prefer_offline: 'true'
npm_config_audit: 'false'
npm_config_fund: 'false'
cache:
key:
files:
- package-lock.json
paths:
- .npm/
- node_modules/
policy: pull
# ===============================
# STAGE: INSTALL
# ===============================
install_deps:
stage: install
cache:
key:
files:
- package-lock.json
paths:
- .npm/
- node_modules/
policy: pull-push
script:
- npm ci
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH
# ===============================
# STAGE: LINT
# ===============================
lint_code:
stage: lint
needs:
- job: install_deps
optional: true
script:
- '[ -d node_modules ] || npm ci'
- npm run lint
- npm run format:check
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH =~ /^(develop|main)$/
lint_commit:
stage: lint
needs:
- job: install_deps
optional: true
variables:
GIT_DEPTH: 0
script:
- '[ -d node_modules ] || npm ci'
- npx commitlint --from $CI_MERGE_REQUEST_DIFF_BASE_SHA --to $CI_MERGE_REQUEST_DIFF_HEAD_SHA
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
# ===============================
# STAGE: BUILD
# ===============================
build_site:
stage: build
needs:
- job: install_deps
optional: true
script:
- '[ -d node_modules ] || npm ci'
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 day
rules:
- if: $CI_COMMIT_BRANCH
# ===============================
# STAGE: TEST
# ===============================
test_build:
stage: test
needs:
- job: build_site
artifacts: true
script:
- test -d dist
- test "$(ls -A dist)"
rules:
- if: $CI_COMMIT_BRANCH
# ===============================
# STAGE: DEPLOY
# ===============================
deploy_develop:
stage: deploy
needs:
- job: build_site
artifacts: true
variables:
NODE_ENV: production
before_script:
- npm install -g wrangler
script:
- wrangler pages deploy dist --project-name=my-project --branch=develop
environment:
name: develop
url: https://develop.my-project.pages.dev
rules:
- if: $CI_COMMIT_BRANCH == "develop"
deploy_production:
stage: deploy
needs:
- job: build_site
artifacts: true
variables:
NODE_ENV: production
before_script:
- npm install -g wrangler
script:
- wrangler pages deploy dist --project-name=my-project --branch=main
environment:
name: production
url: https://my-project.example.com
rules:
- if: $CI_COMMIT_BRANCH == "main"
# ===============================
# STAGE: POST-DEPLOY
# ===============================
sync_develop:
stage: post-deploy
image: alpine:latest
variables:
GIT_STRATEGY: clone
GIT_DEPTH: 0
before_script:
- apk add --no-cache git
- git config user.email "[email protected]"
- git config user.name "GitLab CI"
- git remote set-url origin "http://oauth2:${PUSH_TOKEN}@${GITLAB_INTERNAL_IP}/${CI_PROJECT_PATH}.git"
script:
- git fetch origin develop
- git checkout develop
- git merge origin/main --no-edit
- git push origin develop
rules:
- if: $CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE == "push"
allow_failure: trueTroubleshooting Common Issues#
Issue 1: Pipeline Stuck in “Pending”#
Symptoms: Jobs show “Pending” forever, but the runner appears online.
Check runner logs:
sudo journalctl -u gitlab-runner -fCommon causes:
- Runner URL mismatch - The URL in config.toml is not the URL that GitLab expects
- Network timeout - The runner can’t reach GitLab
- Tag mismatch - Jobs need tags that the runner doesn’t have
Fix: Verify url in config.toml:
sudo gitlab-runner verifyIssue 2: “connection refused” Errors#
dial tcp 192.168.1.10:443: connect: connection refusedCause: Wrong port. GitLab listens on port 80 (HTTP), not 443 (HTTPS).
Fix: Use the HTTP URL:
url = "http://192.168.1.10/"Issue 3: “unauthorized” in Container Jobs#
fatal: unable to access 'https://gitlab.example.com/...':
Failed to connect to gitlab.example.com port 443Cause: The container can’t resolve the hostname, or it uses the wrong port.
Fix: Use the internal IP in job scripts:
git remote set-url origin "http://oauth2:${TOKEN}@${GITLAB_INTERNAL_IP}/${CI_PROJECT_PATH}.git"Issue 4: Cache Never Hits#
Symptoms: npm ci runs every time, “No cache found” in logs.
Common causes:
- Cache key changed (check
package-lock.json) - Cache expired (default 2 weeks)
- A different runner picked up the job
Check cache status:
# In job log, look for:
Checking cache for <key>...
Successfully extracted cache
# or
No URL provided, cache will not be downloadedPerformance Checklist#
- Runner connects to internal IP (e.g.,
http://192.168.1.10/) -
clone_urlis set to internal URL -
extra_hostsconfigured for Docker containers -
pull_policyset toif-not-present -
request_concurrencyset to 2+ - npm cache includes both
.npm/andnode_modules/ -
install_depsjob usespolicy: pull-push - Other jobs use
policy: pull -
needsconfigured for parallel execution -
interruptible: trueset in defaults
Summary#
Default configs choose compatibility over speed. The changes that help the most:
- Direct internal connections - Do not send runner-to-GitLab traffic through Cloudflare
- More caching - Cache
node_modules, not only.npm - Parallel execution - Use
needsfor jobs that do not depend on each other - The right resource limits - Tune
concurrent,cpus, andmemory
Our pipeline went from 5-8 minutes to under 2 minutes. Expect 50-70% improvement on most setups.
Start with the internal connection fix. It is the least work and gives the biggest result.
Next Steps#
Fast changes (30 minutes):
- Update
config.tomlwith the GitLab server’s internal IP and addextra_hostsfor Docker containers. Then runsudo gitlab-runner restart - Add an
install_depsstage with a cache key based onpackage-lock.json, and setpull_policy: if-not-present
Advanced tuning (1–2 hours):
- Tune
concurrent,cpus,memorybased on your workload - Set up a
sync_developjob withPUSH_TOKEN - Add
needsdependencies for parallel execution - Configure artifact expiration policies
Related reading:
- Fix a Slow Self-Hosted GitLab: The RAM Trap During Pipelines — tuning the runner does not help much if the server itself is swapping
- Self-Host Harbor Image Registry on Debian — pull base images from your LAN, not from Docker Hub, on every job