Skip to content
Back to all posts
Technologyset up self-hosted CI/CD

Self-Hosted CI/CD with GitLab Runner: A Safer Pipeline

Build a basic GitLab CI pipeline with reproducible Node.js installs, tests, build artifacts, and protected deployment, then secure the runner that executes it.

3 min readUpdated May 3, 2026AI-assisted

TechSilo

AI-assisted and human-curated

A self-hosted CI runner gives a team control over where build jobs execute. It does not make CI/CD free: you still operate the machine, update the runner, manage credentials, monitor capacity, and secure the environment that runs repository code.

This guide uses GitLab Runner and a Node.js project to show the shape of a small pipeline. The exact runner installation and executor configuration should follow the [official GitLab Runner documentation](https://docs.gitlab.com/runner/).

Choose the executor and isolate the machine

GitLab Runner supports different executors, including Shell and Docker. A Docker executor can give jobs a more consistent environment, but it is not automatically a complete security boundary. A runner with a mounted Docker socket or privileged access can expose the host to job code.

For a small team, use a dedicated machine or VM, keep the runner updated, restrict who can use its tags, and avoid running untrusted fork jobs on a persistent runner that has access to production secrets. For higher-risk workloads, use disposable runners or isolated VMs that are destroyed after a job.

Create a reproducible pipeline

A basic .gitlab-ci.yml for a Node.js project can install dependencies from the lockfile, run tests, and build the application:

yaml
stages:
  - verify
  - build
  - deploy

default:
  image: node:22
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - .npm/

before_script:
  - npm ci --cache .npm --prefer-offline

test:
  stage: verify
  script:
    - npm run test --if-present

build:
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - .next/
    expire_in: 1 day
  needs:
    - test

deploy_production:
  stage: deploy
  script:
    - ./scripts/deploy.sh
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
  environment:
    name: production
  needs:
    - build

This example assumes the project uses Next.js and defines the expected npm scripts. The deployment job also assumes you have created and reviewed scripts/deploy.sh; it is not a built-in GitLab command. Adapt the artifact paths and deployment process to your application rather than copying the example unchanged.

Protect deployment credentials

Store deployment keys and tokens in protected CI/CD variables, not in the repository or pipeline YAML. Restrict production variables to protected branches and environments. Do not expose production secrets to jobs that run code from untrusted merge requests or forks.

Use a dedicated deploy identity with the smallest permissions needed. Rotate credentials, log deployment activity, and make sure the runner cannot access unrelated production systems.

Keep the runner healthy

A self-hosted runner is another production-like service to maintain. Monitor disk space, memory, CPU, queue time, and job failures. Clean up old workspaces and artifacts, patch the operating system, and document how to restore the runner if its host fails.

Resource limits belong in the runner or container-executor configuration, not in arbitrary job-level YAML keys. Configure concurrency and container limits according to the host's actual capacity, then test what happens when multiple builds run at once.

Common mistakes to avoid

- Using npm install in CI: use npm ci with a committed lockfile for reproducible installs.

- Sharing a privileged runner with untrusted jobs: isolate workloads and keep secrets away from untrusted code.

- Assuming a successful build means a safe deployment: add health checks, rollback steps, and a clear approval policy for production.

- Ignoring storage growth: remove expired workspaces and artifacts according to a retention policy.

- Treating self-hosted as zero-cost: include hardware or VM cost, power, bandwidth, maintenance, and incident response.

Start with one runner and one small pipeline. Once the workflow is reliable, add parallel jobs, caching, artifact retention, and deployment automation based on measured bottlenecks. The goal is a repeatable and secure delivery process—not a second server that quietly becomes another full-time job.

Want to put this into practice?

Explore TechSilo's free developer tools for practical utilities you can use directly in your browser.

Try these free tools

Related blog posts