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:
- Go to GitLab.com.
- Create a new repository.
- Push your local project:
git init
git add .
git commit -m "Initial commit"
git remote add origin https://gitlab.com/username/project.git
git push -u origin mainStep 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:
stages:
- build
- test
- deployStep 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:
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:
- mainWhat 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:
- Generate SSH keys on your local machine:
ssh-keygen -t rsa -b 4096 -C "gitlab-ci"- Add the private key in GitLab → Settings → CI/CD → Variables →
SSH_PRIVATE_KEY. - 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:
sudo gitlab-runner registerEnter:
- GitLab URL (e.g., https://gitlab.com)
- Registration token (found in project → Settings → CI/CD)
- Executor type (usually
dockerorshell)
Step 6: Test the Pipeline
Commit and push your .gitlab-ci.yml:
git add .gitlab-ci.yml
git commit -m "Add GitLab CI/CD pipeline"
git pushThen 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:
deploy-job:
stage: deploy
script:
- npm install netlify-cli -g
- netlify deploy --prod --dir=dist --auth=$NETLIFY_TOKEN
only:
- mainAdd 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.

