Gitlab Deploy To Digitalocean

Gitlab Deploy To Digitalocean

Reading time1 min
#DevOps#Cloud#Automation#GitLab#DigitalOcean#CI/CD

How to Automate Deployments to DigitalOcean Using GitLab CI/CD Pipelines

Stop wasting time with manual server deployments—leverage GitLab pipelines to create a seamless, repeatable process that scales with your app and team.


Manual deployments to DigitalOcean droplets can be tedious, error-prone, and slow—especially as your team or application grows. Automating this workflow with GitLab CI/CD not only streamlines the deployment process but also reduces human errors and accelerates your release cycle.

In this post, I’ll guide you through setting up an automated deployment pipeline that pushes your code directly from GitLab to your DigitalOcean droplet using GitLab’s powerful CI/CD features.


Why Automate Deployments to DigitalOcean?

  • Consistency: Automation ensures every deployment follows the same exact steps.
  • Speed: No more manual SSH commands; one git push triggers a full deploy.
  • Reliability: Reduced chance of human error during deployments.
  • Scalability: Easily extendable for multiple environments or droplets.
  • Visibility: Track deployment progress in GitLab’s UI with pipeline logs.

Prerequisites

Before you start, make sure you have:

  • A running DigitalOcean droplet (Ubuntu is commonly used).
  • SSH access set up for your user on the droplet.
  • A GitLab repository containing your application code.
  • Basic familiarity with SSH keys and .gitlab-ci.yml configuration.

Step 1: Generate SSH Keys for GitLab Runner

GitLab CI jobs need SSH access to your droplet to perform the deployment. For that:

  1. On your local machine or a secure environment, generate an SSH key pair dedicated for your CI pipeline:
ssh-keygen -t rsa -b 4096 -C "gitlab-ci-deploy@yourdomain.com" -f deploy_key -N ""
  1. This generates two files:

    • deploy_key: Private key (keep this secret!).
    • deploy_key.pub: Public key.
  2. Add the public key (deploy_key.pub) to the ~/.ssh/authorized_keys file on your DigitalOcean droplet under the user you will deploy as:

cat deploy_key.pub >> ~/.ssh/authorized_keys
  1. Test login from your machine using the private key:
ssh -i ./deploy_key user@your_droplet_ip

Once confirmed, proceed to the next step.


Step 2: Add SSH Private Key to GitLab CI/CD Variables

To allow GitLab CI access during pipeline runs:

  1. In your GitLab project, go to Settings > CI/CD > Variables.

  2. Add a new variable named DEPLOY_SSH_KEY and paste the contents of your private key (deploy_key) into it.

  3. Set this variable as Protected and Masked for security best practice if deploying only from protected branches/tags.


Step 3: Write Your .gitlab-ci.yml Pipeline Configuration

In the root of your repo, add or update .gitlab-ci.yml. Here is a basic example deploying a Node.js app:

stages:
  - deploy

deploy_to_digitalocean:
  stage: deploy
  image: ubuntu:20.04
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
    - eval $(ssh-agent -s)
    - echo "$DEPLOY_SSH_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    # Adding known hosts entries for DigitalOcean IP or github.com repo servers if necessary — adjust accordingly:
    - ssh-keyscan -H your_droplet_ip >> ~/.ssh/known_hosts
  script:
    # Example commands — customize for your app setup!
    # Sync code using rsync or git pull remotely
    - ssh user@your_droplet_ip "cd /var/www/myapp && git pull origin main && npm install && pm2 restart all"
  only:
    - main

How This Works:

  • The pipeline runs on Ubuntu docker image.
  • Installs ssh-agent tools if missing.
  • Injects the SSH private key dynamically.
  • Adds droplet IP fingerprint so SSH doesn’t prompt for confirmation.
  • Connects over SSH and triggers application update commands (pull latest code, install dependencies, then restart app).

Step 4: Configure Your Droplet Repository & App

Make sure:

  • Your app folder is a valid git repo cloned from the same repo as GitLab.

Example on Droplet:

cd /var/www/myapp
git init
git remote add origin git@gitlab.com:your_username/your_repo.git
git fetch origin
git checkout main

Install any necessary runtimes (Node.js, Python, etc.) and process managers like PM2 if you want zero-downtime restarts.


Optional Enhancements

Using Rsync Instead of Git Pull

If you don’t want to clone/pull on the server itself, use rsync in .gitlab-ci.yml script like:

script:
  - rsync -avz --delete ./ user@your_droplet_ip:/var/www/myapp/
  - ssh user@your_droplet_ip "npm install && pm2 restart all"

This copies changes directly from GitLab runner workspace.

Multi-environment Deploys

For staging/production droplets differing by host/IP or branch deploy trigger, create separate jobs or use environment variables.


Wrapping Up

By following these steps and adjusting scripts as per your app’s needs, you unlock automated deployments straight from GitLab to your DigitalOcean droplets—making releases faster, safer and easier to manage.

No more manual headaches typing SSH commands or copying files! Every push can now trigger reliable deployments that scale with you and your development team.

Start automating today and spend more time coding great software instead of managing server updates!

Happy deploying 🚀


If you found this guide helpful or have questions about specific setups (Docker containers, Kubernetes with DigitalOcean), drop a comment below—I’m happy to help!