Physical Address
Metro Manila, Philippines
Physical Address
Metro Manila, Philippines
GitLab CI/CD Deployment for Beginners is your step-by-step guide to pushing code to a remote staging server using secure SSH automation. Whether you’re new to GitLab or DevOps, this tutorial will help you automate your deployment pipeline quickly and safely.
Deploy to remote server with GitLab CI/CD and simplify your staging or production workflow. In this guide, youβll learn how to set up a secure, automated CI/CD pipeline to deliver your code efficiently. Whether you’re new to DevOps or just starting with GitLab, this tutorial will walk you through the essential steps β from SSH setup to deployment execution.
π Note: Every project and server environment can have its own unique requirements and configurations. This tutorial provides a general deployment process, not a one-size-fits-all setup. Adjust scripts and paths based on your infrastructure.
Before setting up automated deployments, itβs important to understand how to commit and push your changes properly. Learn more in our guide: Basic Git Commands for GitLab Developers.
| Requirement | Description |
|---|---|
| π§βπ» GitLab Project | GitLab CI/CD must be enabled in your project. |
| π Remote Staging Server | – Public IP or DNS – SSH access – Git and build tools installed |
| π Deploy User | Non-root user with write access to deployment folder (e.g. /home/deploy/app). |
| π€ GitLab Runner | Shared or self-hosted GitLab Runner connected to your project. |
ssh-keygen -t rsa -b 4096 -C "gitlab-staging-deploy"
This creates:
~/.ssh/id_rsa β your private key~/.ssh/id_rsa.pub β your public keyssh-copy-id -i ~/.ssh/id_rsa.pub deploy@staging.yourdomain.com
Or manually add the contents of id_rsa.pub to ~/.ssh/authorized_keys.
STAGING_SSH_KEYid_rsa (private key).gitlab-ci.yml to Deploy Code via GitLab CI/CDstages:
- build
- deploy
variables:
STAGING_USER: "deploy"
STAGING_HOST: "staging.yourdomain.com"
STAGING_PATH: "/home/${STAGING_USER}/app"
before_script:
- 'which ssh-agent || (apt-get update -y && apt-get install openssh-client -y)'
- eval $(ssh-agent -s)
- echo "$STAGING_SSH_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- ssh-keyscan -H $STAGING_HOST >> ~/.ssh/known_hosts
build_app:
stage: build
script:
- echo "Running build..."
- mkdir -p dist
- echo "Sample Build Output" > dist/index.html
artifacts:
paths:
- dist/
deploy_staging:
stage: deploy
only:
- development
script:
- ssh $STAGING_USER@$STAGING_HOST "mkdir -p $STAGING_PATH"
- rsync -avz --delete dist/ $STAGING_USER@$STAGING_HOST:$STAGING_PATH
- ssh $STAGING_USER@$STAGING_HOST "sudo systemctl reload nginx"
environment:
name: staging
url: http://staging.yourdomain.com
when: manual
sudo apt update
sudo apt install git rsync nginx -y
sudo adduser deploy
sudo mkdir -p /home/deploy/app
sudo chown -R deploy:deploy /home/deploy/app
This ensures your deployment user has full access to the target directory.
sudo visudo
Add this line:
deploy ALL=NOPASSWD: /bin/systemctl reload nginx
chmod o+x /home/deploy
chmod -R o+r /home/deploy/app
Optional: Create a symlink if Nginx is pointing to /var/www/html:
ln -s /home/deploy/app /var/www/html/app
If your server has the repo cloned, you can update code with:
script:
- ssh $STAGING_USER@$STAGING_HOST "cd $STAGING_PATH && git pull origin development && ./build.sh"
when: manual is used| Practice | Details |
|---|---|
| π Use SSH Keys | Never use passwords. Store private keys as protected CI/CD variables. |
| π§Ύ Use Artifacts | Only deploy what you build. Avoid syncing entire repos. |
| π« Avoid Root Access | Always deploy as a non-root user for better security. |
| π§ Track Your Deployments | Use GitLab environments to monitor and manage releases. |
πΉοΈ Use when: manual |
Control when deploys happen β ideal for QA teams. |
Now you know how to deploy to remote server with GitLab CI/CD using a clean, beginner-friendly pipeline. By following this guide, youβve learned how to GitLab CI/CD deploy code efficiently and securely using SSH and automation. With secure SSH keys, build artifacts, and deployment automation, your DevOps workflow becomes faster, safer, and more consistent.