14/10/2026

GitLab CI Tutorial: Zero to Deploy on a VPS

Still deploying by hand? You SSH into the server, run `git pull`, build, restart, and hope nothing breaks? That works for a while. But once the team grows and several people push at the same time, it stops working. Someone forgets a step and production goes down. This GitLab CI tutorial starts right there.

GitLab CI automates this. Every time you push, GitLab reads .gitlab-ci.yml from the root of the repo and creates a pipeline. A pipeline has stages, like test, build and deploy. Each stage has jobs. Stages run in order, and if a job fails, the pipeline stops there.

GitLab itself doesn't run your code, though. A separate program called GitLab Runner does. You install the Runner on a server, connect it to GitLab, and it picks up jobs and runs them.

In this post we'll build a real pipeline that does three things:

1. Runs the tests on every push. 2. On the main branch, builds a Docker image and pushes it to GitLab's own Container Registry. 3. With one click, SSHes into the VPS and brings up the new version with docker compose.

The examples use a self-hosted GitLab at gitlab.example.com. On GitLab.com it's the same, only the URL changes. The sample app is Node.js, but the structure is the same for any language.

First, set up the Runner

Put the Runner on its own server, not on the production box. To build images we'll use Docker-in-Docker, so every job gets its own isolated Docker daemon. That needs privileged mode.

Install it on Debian or Ubuntu:

bash@itcorv:~
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
sudo apt install gitlab-runner

You also need Docker on this server, since our executor is docker.

In GitLab, go to Settings > CI/CD > Runners and create a new project runner. Give it a tag there, for example docker. You'll get a token that starts with glrt-. Then on the server:

bash@itcorv:~
sudo gitlab-runner register \
  --non-interactive \
  --url "https://gitlab.example.com" \
  --token "glrt-xxxxxxxxxxxxxxxx" \
  --executor "docker" \
  --docker-image "docker:28-cli" \
  --docker-privileged \
  --docker-volumes "/certs/client"

/certs/client lets the job and the dind service share the TLS certificates. Skip it and the Docker client inside the job can't reach dind.

One more thing. The old way of registering with a registration token is deprecated, and GitLab plans to remove it in 20.0. So start with the glrt- token from day one. With this new flow you don't pass tags with --tag-list anymore; you set them in the UI when you create the runner.

Prepare the target server

On the production VPS you need three things.

Create a separate deploy user and add it to the docker group:

bash@itcorv:~
sudo adduser --disabled-password --gecos "" deploy
sudo usermod -aG docker deploy

Keep in mind that the docker group is basically root access. Use this user for deploys only.

Then create an SSH key pair just for CI. Put the public key in ~deploy/.ssh/authorized_keys. The private key goes to GitLab later.

bash@itcorv:~
ssh-keygen -t ed25519 -C "gitlab-ci-deploy" -f ./ci_deploy_key -N ""

Finally, the server has to be able to pull from GitLab's registry. Create a Deploy Token in the project with the read_registry scope, and log in once on the server as the deploy user:

bash@itcorv:~
docker login registry.example.com -u <deploy-token-username>

Add the variables in GitLab

In Settings > CI/CD > Variables, add these:

| Name | Type | Value | |---|---|---| | SSH_PRIVATE_KEY | File | contents of ci_deploy_key | | SSH_KNOWN_HOSTS | File | output of ssh-keyscan for the server | | DEPLOY_HOST | Variable | server address or IP |

Make the key a File variable. That keeps the line breaks intact, and inside the job GitLab gives you a path to a file instead of the key text. Also press Enter after the -----END OPENSSH PRIVATE KEY----- line. Without that newline, ssh-add fails.

Grab SSH_KNOWN_HOSTS once from your own machine:

bash@itcorv:~
ssh-keyscan -t ed25519 your-server-ip

Why not run that inside the job? Because if the server's host key ever changes, you want the deploy to fail so you notice something's wrong.

Mark the variables as Protected too. Protected means they're only available on protected branches like main, so nobody can read the production key from some other branch.

The sample .gitlab-ci.yml

Here's the whole file. Put it in the repo root:

yaml@itcorv:~
stages:
  - test
  - build
  - deploy

variables:
  IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA

test:
  stage: test
  image: node:22-alpine
  tags: [docker]
  cache:
    key:
      files:
        - package-lock.json
    paths:
      - .npm/
  script:
    - npm ci --cache .npm --prefer-offline
    - npm test

build:
  stage: build
  image: docker:28-cli
  tags: [docker]
  services:
    - name: docker:28-dind
      command: ["--registry-mirror", "https://docker.arvancloud.ir"]
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
    - docker build -t "$IMAGE" .
    - docker push "$IMAGE"
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

deploy:
  stage: deploy
  image: alpine:3.22
  tags: [docker]
  environment:
    name: production
    url: https://app.example.com
  resource_group: production
  before_script:
    - apk add --no-cache openssh-client
    - eval $(ssh-agent -s)
    - chmod 400 "$SSH_PRIVATE_KEY"
    - ssh-add "$SSH_PRIVATE_KEY"
    - mkdir -p ~/.ssh && chmod 700 ~/.ssh
    - cp "$SSH_KNOWN_HOSTS" ~/.ssh/known_hosts && chmod 644 ~/.ssh/known_hosts
  script:
    - ssh deploy@"$DEPLOY_HOST" "cd /opt/app && IMAGE_TAG=$CI_COMMIT_SHORT_SHA docker compose pull && IMAGE_TAG=$CI_COMMIT_SHORT_SHA docker compose up -d"
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: manual

Let's go through it.

$CI_REGISTRY_IMAGE, $CI_COMMIT_SHORT_SHA, $CI_REGISTRY_USER and $CI_REGISTRY_PASSWORD come from GitLab itself, so you don't define them anywhere. The image tag is the commit SHA, not latest. That way you always know exactly which commit is running on the server.

The test job runs on every branch. build and deploy only run on the main branch. The cache keeps the npm folder around, so it doesn't download everything again until package-lock.json changes.

when: manual means the deploy doesn't start on its own. The pipeline waits until someone hits play. For production, that's what you want. For a staging deploy that should go out automatically, just drop that line.

resource_group: production stops two deploys from running at the same time. If two merges land back to back, the second one waits for the first to finish.

environment gives you the project's Environments page, where you can see what was deployed and when.

The --registry-mirror on dind is for the FROM lines in your Dockerfile. If the server is in Iran, Docker Hub is blocked, so the build gets stuck without a mirror. We covered mirrors, and when to use Chinese ones versus Iranian ones, in the Docker Hub post.

compose on the server

On the VPS, put this in /opt/app/compose.yml:

yaml@itcorv:~
services:
  app:
    image: registry.example.com/team/app:${IMAGE_TAG:?IMAGE_TAG is required}
    restart: unless-stopped
    ports:
      - "127.0.0.1:3000:3000"

The :? makes compose fail if IMAGE_TAG is empty, instead of quietly starting the wrong image. The port is only open on localhost. Put nginx with TLS in front of it, the same setup from the nginx post.

Test the GitLab CI pipeline

First, check that the Runner is connected:

bash@itcorv:~
sudo gitlab-runner verify
sudo gitlab-runner status

The Runner should also show as online in Settings > CI/CD > Runners.

Before you push, open the file in GitLab's Pipeline editor. It validates the YAML and tells you right there if you got a keyword wrong.

Now create a feature branch and push. Only test should run. Then merge into main. This time test and build run on their own, and deploy waits for you. Hit play.

Then check what's running on the server:

bash@itcorv:~
cd /opt/app && docker compose ps
curl -I http://127.0.0.1:3000

The image tag in docker compose ps should match the short commit SHA you see in GitLab.

Practical GitLab CI tips

Is a job stuck pulling node:22-alpine or docker:28-dind? That pull is done by Docker on the Runner host, not by dind. So set a mirror on the Runner host too, in /etc/docker/daemon.json. The mirror on dind only covers what happens inside the build.

Getting Cannot connect to the Docker daemon in the build job? It's almost always one of two things: the Runner isn't privileged, or /certs/client isn't in its volumes.

ssh-add complaining about the key format? It's that missing newline at the end of the key. Edit the variable and add an empty line at the end.

Rollback is easy. Every image has its own SHA tag, so open the pipeline of an older commit and re-run its deploy job. That version comes back.

Don't share a privileged Runner with projects you don't trust. Privileged means the job effectively has access to the host. Give each team or each sensitive environment its own Runner.

Protect the main branch and only let the right people merge. That also keeps the Protected variables limited to that branch.

As the project grows, add a staging deploy job that runs automatically, and keep production manual. If you want to scan the image before it ships, add a separate Trivy job after the build.

FAQ

Isn't the shell executor simpler?

Yes. You install the Runner on the server itself and the job runs docker compose right there. But then CI code runs directly on your production server, and jobs aren't isolated from each other. Maybe fine for a small personal project, not for team work.

Why not latest?

Because latest changes every time and you never know exactly what's running. Rollbacks get harder too. With the SHA it's always clear.

Should I mask the SSH key?

A multi-line key can't be masked. That's why it goes in as a File variable. File variables aren't shown in job logs.

I have several servers. What now?

Repeat the deploy job per server, or loop over a server list in the script. Once the list gets long, it's time to look at Ansible or Kubernetes.

So, to sum up: a separate Runner with Docker-in-Docker, a .gitlab-ci.yml with three stages, image tags set to the commit SHA, the SSH key as a File variable, and production deploys behind one click and a resource_group.

Want this pipeline running on your own infrastructure, from the Runner and registry to deploys across many servers? Check out our DevOps services, or book a free 30-minute review and we'll look at it together.