Skip to content
subuilds.dev

← 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_modules and run independent jobs in parallel with needs. 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 ci and 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
  1. The runner and the GitLab server are two VMs on one local network. People outside reach GitLab through Cloudflare.
  2. 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.
  3. Set url and clone_url in config.toml to http://192.168.1.10/. The runner now talks to GitLab directly.
  4. Job containers still use the public name. extra_hosts makes them resolve gitlab.example.com to 192.168.1.10 too.
  5. All traffic stays on the local network. A git clone takes 2-5 seconds, not 15-30.
5 / 5

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:

bash
sudo journalctl -u gitlab-runner -n 20 --no-pager

If you see Cloudflare IPs, your traffic takes the long path through the internet:

bash
dial tcp 104.21.9.142:443: i/o timeout

Those 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:

bash
ss -tlun | grep -E '80|443|5443'

Typical output:

bash
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:

toml
[[runners]]
  name = "gitlab-runner-01"
  url = "http://192.168.1.10/"
  clone_url = "http://192.168.1.10/"
  # ... rest of config

The 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:

toml
[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

bash
sudo gitlab-runner restart
sudo journalctl -u gitlab-runner -f

Job 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.

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:

bash
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:

  1. Downloads packages from npm registry
  2. Installs all dependencies again
  3. Repeats this even when package-lock.json hasn’t changed

The Solution: Dedicated Install Stage#

yaml
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_BRANCH

How Other Jobs Use the Cache#

yaml
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_BRANCH

Cache Key Strategy#

The cache key is based on package-lock.json:

yaml
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)#

  1. Without needs, the five jobs run one after another. The pipeline takes 180 seconds.
  2. With needs, lint and build both start when install is done. They run at the same time.
  3. deploy starts when lint and test are done. The pipeline ends sooner.
3 / 3

With needs (Parallel)#

yaml
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
  1. Without needs, the five jobs run one after another. The pipeline takes 180 seconds.
  2. With needs, lint and build both start when install is done. They run at the same time.
  3. deploy starts when lint and test are done. The pipeline ends sooner.
3 / 3

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.

yaml
# ===============================
# 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: true

Why 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:

yaml
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: true

Troubleshooting Common Issues#

Issue 1: Pipeline Stuck in “Pending”#

Symptoms: Jobs show “Pending” forever, but the runner appears online.

Check runner logs:

bash
sudo journalctl -u gitlab-runner -f

Common causes:

  1. Runner URL mismatch - The URL in config.toml is not the URL that GitLab expects
  2. Network timeout - The runner can’t reach GitLab
  3. Tag mismatch - Jobs need tags that the runner doesn’t have

Fix: Verify url in config.toml:

bash
sudo gitlab-runner verify

Issue 2: “connection refused” Errors#

bash
dial tcp 192.168.1.10:443: connect: connection refused

Cause: Wrong port. GitLab listens on port 80 (HTTP), not 443 (HTTPS).

Fix: Use the HTTP URL:

toml
url = "http://192.168.1.10/"

Issue 3: “unauthorized” in Container Jobs#

bash
fatal: unable to access 'https://gitlab.example.com/...':
Failed to connect to gitlab.example.com port 443

Cause: The container can’t resolve the hostname, or it uses the wrong port.

Fix: Use the internal IP in job scripts:

yaml
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:

  1. Cache key changed (check package-lock.json)
  2. Cache expired (default 2 weeks)
  3. A different runner picked up the job

Check cache status:

bash
# In job log, look for:
Checking cache for <key>...
Successfully extracted cache
# or
No URL provided, cache will not be downloaded

Performance Checklist#

  • Runner connects to internal IP (e.g., http://192.168.1.10/)
  • clone_url is set to internal URL
  • extra_hosts configured for Docker containers
  • pull_policy set to if-not-present
  • request_concurrency set to 2+
  • npm cache includes both .npm/ and node_modules/
  • install_deps job uses policy: pull-push
  • Other jobs use policy: pull
  • needs configured for parallel execution
  • interruptible: true set in defaults

Summary#

Default configs choose compatibility over speed. The changes that help the most:

  1. Direct internal connections - Do not send runner-to-GitLab traffic through Cloudflare
  2. More caching - Cache node_modules, not only .npm
  3. Parallel execution - Use needs for jobs that do not depend on each other
  4. The right resource limits - Tune concurrent, cpus, and memory

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.toml with the GitLab server’s internal IP and add extra_hosts for Docker containers. Then run sudo gitlab-runner restart
  • Add an install_deps stage with a cache key based on package-lock.json, and set pull_policy: if-not-present

Advanced tuning (1–2 hours):

  • Tune concurrent, cpus, memory based on your workload
  • Set up a sync_develop job with PUSH_TOKEN
  • Add needs dependencies for parallel execution
  • Configure artifact expiration policies

Related reading:

More Resources#