CraftedTemplate
Blog How to Set Up Continuous Deployment with GitLab CI/CD (Complete Beginner’s Tutorial)

How to Set Up Continuous Deployment with GitLab CI/CD (Complete Beginner’s Tutorial)

11/1/2025 • Festus Ayomike
How to Set Up Continuous Deployment with GitLab CI/CD (Complete Beginner’s Tutorial)

If you’ve used GitHub Actions or Jenkins for automation, you already understand the power of CI/CD — but GitLab CI/CD takes it a step further by integrating pipelines directly into your repository, with no extra setup or external tools required.

In this guide, you’ll learn how to create a GitLab CI/CD pipeline that builds and automatically deploys your website or app whenever you push new changes.

See Also: How to Host and Scale Containers with Kubernetes (Beginner’s DevOps Tutorial)

What Is GitLab CI/CD?

GitLab CI/CD (Continuous Integration and Continuous Deployment) is a built-in system for:

  • Building code on every push
  • Testing automatically
  • Deploying to servers or cloud providers

It uses a simple YAML configuration file — .gitlab-ci.yml — to define each stage of your workflow.

Step 1: Create a GitLab Repository

If you don’t already have one:

  1. Go to GitLab.com.
  2. Create a new repository.
  3. Push your local project:
Code · batchfile
git init
git add .
git commit -m "Initial commit"
git remote add origin https://gitlab.com/username/project.git
git push -u origin main

Step 2: Understand the Pipeline Structure

A pipeline in GitLab is made up of stages (e.g., build, test, deploy).
Each stage runs jobs (commands) that perform specific tasks.

Example stages:

Code · markdown
stages:
  - build
  - test
  - deploy

Step 3: Create a .gitlab-ci.yml File

In your project root, add a file named .gitlab-ci.yml.

Here’s a simple example for a Node.js website:

Code · yaml
image: node:18

stages:
  - build
  - deploy

cache:
  paths:
    - node_modules/

build-job:
  stage: build
  script:
    - npm ci
    - npm run build
  artifacts:
    paths:
      - dist/

deploy-job:
  stage: deploy
  script:
    - echo "Deploying to server..."
    - scp -r ./dist/* user@yourserver:/var/www/html/
  only:
    - main

What this does:

  • Uses Node.js to build your project.
  • Deploys automatically when changes are pushed to main.

See Also: How to Deploy Containerized Apps with Docker Compose (Step-by-Step Guide)

Step 4: Add SSH Keys for Deployment

To deploy to your own server or VPS securely:

  1. Generate SSH keys on your local machine:
Code · batchfile
ssh-keygen -t rsa -b 4096 -C "gitlab-ci"
  1. Add the private key in GitLab → Settings → CI/CD → Variables → SSH_PRIVATE_KEY.
  2. Add the public key to your server’s ~/.ssh/authorized_keys.

Now your GitLab runner can deploy without needing a password.

Step 5: Set Up a GitLab Runner

GitLab Runners are agents that execute your pipelines.
You can use:

  • Shared runners (default, provided by GitLab)
  • Custom runners on your own server

To register your own:

Code · batchfile
sudo gitlab-runner register

Enter:

  • GitLab URL (e.g., https://gitlab.com)
  • Registration token (found in project → Settings → CI/CD)
  • Executor type (usually docker or shell)

Step 6: Test the Pipeline

Commit and push your .gitlab-ci.yml:

Code · batchfile
git add .gitlab-ci.yml
git commit -m "Add GitLab CI/CD pipeline"
git push

Then go to your GitLab → CI/CD → Pipelines tab.
You’ll see it run automatically!

Bonus: Advanced Example (Deploy to Netlify)

If you’re deploying static sites to Netlify, use their CLI:

Code · yaml
deploy-job:
  stage: deploy
  script:
    - npm install netlify-cli -g
    - netlify deploy --prod --dir=dist --auth=$NETLIFY_TOKEN
  only:
    - main

Add NETLIFY_TOKEN to GitLab CI/CD variables for security.

Conclusion

GitLab CI/CD makes automated deployment simple — everything runs within your repo, no third-party tools required.

With one YAML file, you can automate builds, tests, and deployments — making your workflow faster, cleaner, and more reliable.

Next up in Hosting Academy: how to automate deployments using Bitbucket Pipelines.