Home DevOpsGitLab CI/CD Pipeline Tutorial: From First YAML to Automated Deploy

GitLab CI/CD Pipeline Tutorial: From First YAML to Automated Deploy

by Knowledge Base Linux
GitLab CI CD Pipeline Tutorial — GitLab CI/CD Pipeline Tutorial: From First YAML to Automated Deploy

If your code already lives on GitLab, the shortest path to continuous integration is the one built into the platform itself. A GitLab CI/CD pipeline is defined by a single YAML file in your repository, runs on every push, and can build, test, and deploy your application without any external CI server. This tutorial creates a complete pipeline for a Node.js application and explains every piece as we go.

How GitLab CI/CD Is Structured – GitLab CI CD Pipeline Tutorial

  • Pipeline: the full set of jobs that runs for one commit or merge request.
  • Stages: ordered groups such as build, test, deploy. Jobs in the same stage run in parallel; stages run in sequence.
  • Jobs: individual units of work, each executing in a fresh environment.
  • Runners: agents that execute jobs. GitLab.com provides shared runners; self-managed instances often register their own.

Prerequisites – GitLab CI CD Pipeline Tutorial

  • A GitLab account or self-managed GitLab instance, and a project repository.
  • A simple Node.js application with package.json build and test scripts (any language works; the structure is the same).

Step 1: Write Your First .gitlab-ci.yml

Create .gitlab-ci.yml in the repository root:

image: node:20

stages:
  - build
  - test
  - deploy

cache:
  key: "$CI_COMMIT_REF_SLUG"
  paths:
    - node_modules/

build_job:
  stage: build
  script:
    - npm install
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 day

test_job:
  stage: test
  script:
    - npm install
    - npm test

deploy_job:
  stage: deploy
  script:
    - echo "Deploying build output from dist/ ..."
  environment:
    name: production
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

Commit and push. Under Build > Pipelines in GitLab you can watch the three stages run. Key concepts in this file:

  • Artifacts pass files between stages: test_job and deploy_job automatically receive the dist/ folder produced by the build.
  • Cache speeds up repeated work by restoring node_modules between pipelines. Cache is an optimization; artifacts are a contract.
  • Rules control when jobs run. Here the deploy only happens on the main branch, which is the core safety property of a GitLab CI/CD pipeline.
GitLab CI/CD pipeline - runners executing jobs in parallel illustration

Step 2: Deploy for Real with SSH

Replace the placeholder deploy script with an actual deployment. Store the server’s SSH private key as a masked, protected CI/CD variable named SSH_PRIVATE_KEY under Settings > CI/CD > Variables, then:

deploy_job:
  stage: deploy
  image: alpine:latest
  before_script:
    - apk add --no-cache openssh-client rsync
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh && chmod 700 ~/.ssh
    - ssh-keyscan -H "$DEPLOY_SERVER" >> ~/.ssh/known_hosts
  script:
    - rsync -avz --delete dist/ deploy@"$DEPLOY_SERVER":/var/www/myapp/
  environment:
    name: production
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

DEPLOY_SERVER is another CI/CD variable holding the hostname. Marking the branch main as protected in GitLab ensures only maintainers can trigger this job.

Step 3: Register Your Own Runner (Self-Managed GitLab)

On self-managed instances, or when shared runners are not an option, register a Runner on any Linux server:

curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
sudo apt-get install -y gitlab-runner
sudo gitlab-runner register

The registration wizard asks for your instance URL and a registration token (found under the project’s Settings > CI/CD > Runners), then an executor. Choose docker so every job runs in a clean container matching the image: in your YAML. Jobs can also target specific runners with tags.

Step 4: Improve the Pipeline

  • Merge request pipelines: add workflow: rules so pushes to branches with open merge requests run one pipeline, not two.
  • Review the pipeline graph: GitLab visualizes stages and job dependencies; use needs: to start jobs as soon as their specific inputs are ready instead of waiting for a whole stage.
  • Schedules and triggers: nightly pipelines can run heavier test suites that are too slow for every push. This flexibility is why the GitLab CI/CD pipeline model scales from side projects to monorepos.

Troubleshooting Common Problems

Pipeline is stuck in “pending”

No available runner matches the job’s tags, or all runners are busy or paused. Check Settings > CI/CD > Runners and confirm a runner is online and allowed to run untagged jobs if your jobs have no tags.

“This job is stuck because you don’t have any active runners”

On self-managed installs, verify the service with sudo gitlab-runner status and sudo gitlab-runner verify.

Variables are empty in jobs

Variables defined for protected branches only appear on protected refs, and masked variables must meet GitLab’s character rules. Also confirm the variable scope (project vs group vs instance) covers your project.

Make Production Deploys Deliberate

Automation should make production changes easy, not accidental. A common pattern is a manual gate on the deploy job: add when: manual to deploy_job so the pipeline builds and tests every merge to main, but a human clicks play before anything reaches users. Combine this with protected environments in GitLab, which can require specific approvers and record who deployed what, when, from the environment page. For larger teams, split the pipeline with include: so shared templates live in one central project and every repository inherits the same build, test, and security stages instead of copy-pasting YAML that drifts. Finally, review the pipeline efficiency page in GitLab analytics occasionally; the slowest job usually hides a missing cache or an oversized artifact, and shaving minutes off every run compounds into hours saved each week.

Conclusion

You have built a complete GitLab CI/CD pipeline: build and test stages with caching and artifacts, a protected production deploy over SSH triggered only from main, and the option of your own runner. If your team hosts code on GitHub instead, our guide to building a CI/CD pipeline with GitHub Actions and Docker covers the equivalent workflow. For the full YAML reference, keywords, and examples, see the GitLab CI/CD official documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.