Gitlab CI/CD deploy the website to VPS


In this tutorial, How to deploy the website to VPS using Gitlab CI/CD. You need a little knowledge about VPS, SSH, Public key, private key, Nginx… Now let’s go Gitlab CI/CD and deploy the website to VPS.


  • Domain:
  • VPS on Linode: Have installed Nginx and added the domain into VPS.
  • Source code: For example, is React
  • Create SSH key on VPS
  • Add SSH key into GitLab, CI/CD of Project
  • Install Gitlab Runner
  • Add .gitlab-ci.yml to the root folder on Gitlab


Create a public key and Private key for CI/CD

Type the command on VPS below:

ssh-keygen -t rsa -b 4096 -m pem -C "" -P "" -q -f ~/.ssh/gitlab

It will gen two files is gitlab and

Convert PKCS #1 to PKCS #8 as command below:

openssl pkcs8 -in gitlab -topk8 -out gitlab2 -nocrypt
#openssl pkcs8 -in gitlab -topk8 -out gitlab2

If you use gitlab key PKCS #1 is the error “Error loading key “/root/.ssh/id_rsa”: invalid format”. I will convert the gitlab2 private key.

On terminal type command


Copy the code public key, and paste it into SSH of the account Gitlab

Gitlab CI/CD deploy the website to VPS

Type the command below to get the private key

cat gitlab2

This private key assign to Settings CI/CD of Repository. Variables are SSH_PRIVATE_KEY

Gitlab CI/CD deploy the website to VPS

Copy the public key into the authorized_keys file.

touch .ssh/authorized_keys
cat .ssh/ > .ssh/authorized_keys

Create a new .ssh/config file with the content below:

  IgnoreUnknown AddKeysToAgent,UseKeychain
  #UseKeychain yes
  AddKeysToAgent yes
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/gitlab

Install Gitlab Runner on VPS

My example, Install Gitlab Runner on Ubuntu VPS

Download and install binary

sudo curl -L --output /usr/local/bin/gitlab-runner

Give it permission to execute

sudo chmod +x /usr/local/bin/gitlab-runner

Create a GitLab CI user

sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

Install and run as a service

sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start

Command to register the runner

sudo gitlab-runner register --url --registration-token $REGISTRATION_TOKEN

REGISTRATION_TOKEN the get on repository setting, In CI/CD

Gitlab CI/CD deploy the website to VPS

Create new .gitlab-ci.yml file at the folder root on Gitlab

Gitlab CI/CD deploy the website to VPS

The content is as below:

# Using the node image to build the React app

image: node:latest
# Cache node modules - speeds up future builds
#  paths:
#     - node_modules
     - build
     - deploy
  stage: build
     - echo "Start building App"
     - chmod +x node_modules/.bin/react-scripts
     - npm install # Install all dependencies
     - npm run build #--prod  Build for prod
     - echo "Build successfully!"
      - build 
    - master # Only run on master branch
  stage: deploy
  image: ubuntu
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - mkdir -p ~/.ssh
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/gitlab
    - chmod 700 ~/.ssh/gitlab
    - eval "$(ssh-agent -s)"
    - ssh-add ~/.ssh/gitlab
    - ssh-keyscan -H '' >> ~/.ssh/known_hosts
    - apt-get install rsync -y -qq
    - apt-get install curl -y -qq

     - echo "Deploying to server"
     - ssh -i ~/.ssh/gitlab -o StrictHostKeyChecking=no huupv@SERVER_IP -p PORT
     - rsync -avz --progress -a -e "ssh -p PORT" build/ huupv@SERVER_IP:/var/www/YOUR_DOMAIN/public_html
     - echo "Deployed"
    name: production
     - master # Only run on master branch

After clicking commit and checking the result in CI/CD

Gitlab CI/CD deploy the website to VPS


You have Gitlab CI/CD deploy the website to VPS. I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

3 thoughts on “Gitlab CI/CD deploy the website to VPS

  1. What kind of gitlab-runner executor is recommended for this kind of project when registering the gitlab-runner on the VPS? It seems that registering the gitlab runner seems to have missing steps in terms of clarity.

  2. Hi,

    clould U help:
    >$ cat ~/.ssh/gitlab
    >$ chmod 700 ~/.ssh/gitlab
    >$ eval “$(ssh-agent -s)”
    >Agent pid 388
    >$ ssh-add ~/.ssh/gitlab
    >Error loading key “/root/.ssh/gitlab”: error in libcrypto

    1. Hi Dominik
      Cause: The most likely cause of that is the SSH key in the variable doesn’t have a line break at the end. Go back to the CI variables part and check. Add a new, blank like at the end if needed.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.