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.
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:
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:
- buildThis 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
JSON Formatter
The fastest free JSON formatter and validator for developers. Pretty-print API responses, fix broken JSON syntax, and validate JSON without uploading your data.
Open toolRegex Tester
Validate regular expressions instantly, highlight matches, and debug regex groups in real time.
Open toolAI Code Explainer
Paste any code snippet and get a plain-English breakdown with bug warnings, logic explanation, and improvement suggestions.
Open toolRelated blog posts
Setting Up MinIO for S3-Compatible Local File Storage
Run a local MinIO object store, create a bucket, connect with the AWS SDK, and understand production deployment safeguards.
Read postSetting Up Docker for Local Development
To set up Docker for local development, you'll need a computer with a 64-bit operating system (Windows 10 or macOS 10.13 or later), at least 4 GB of RAM, and a compatible processor. You'll…
Read postSetting Up a GitHub Actions CI/CD Pipeline
To set up a GitHub Actions CI/CD pipeline, you'll need a GitHub account, a repository with your project code, and Node.js (version 14 or higher) installed on your machine. You'll also need…
Read postSetting up Tailwind CSS with a Custom Theme
To set up Tailwind CSS with a custom theme, you'll need Node.js (version 16.14.2 or higher) and npm (version 8.5.5 or higher). You'll also need a code editor and a terminal.
Read postSetting up Tailwind CSS with a Custom Theme
To set up Tailwind CSS with a custom theme, you'll need Node.js (version 16.14.2 or higher) and npm (version 8.5.5 or higher) installed on your machine. You'll also need a code editor and a…
Read post