Gitlab CI/CD deploy the website to VPS


In this tutorial, How to deploy the website to VPS use Gitlab CI/CD. You need a little knowledge about VPS, SSH, Public key, private key, Nginx…


  • Domain:
  • VPS on Linode: Have installed Nginx and add 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 at the root folder on Gitlab


Create a public key and Private key for CI/CD

Type command on VPS as 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 code public key, and paste into SSH of account Gitlab

Type command below to get the private key

cat gitlab2

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

Copy public key into 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 permissions 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 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

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

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 click commit and check the result in CI/CD


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 and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

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

Comment moderation is enabled. Your comment may take some time to appear.

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