Gitlab CI/CD deploy the website to VPS

#Introduction

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 deploy the website to VPS.

Prerequisites

  • Domain: https://devopsroles.com
  • 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

On VPS

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 "pvhuu285@gmail.com" -P "" -q -f ~/.ssh/gitlab

It will gen two files is gitlab and gitlab.pub

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

cat gitlab.pub

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

Gitlab CI/CD deploy the website to VPS

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

Gitlab CI/CD deploy the website to VPS

Copy public key into authorized_keys file.

touch .ssh/authorized_keys
cat .ssh/gitlab.pub > .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 https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

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 https://gitlab.com/ --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 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
variables:
  PUBLIC_URL: /
# Cache node modules - speeds up future builds
#cache:
#  paths:
#     - node_modules
stages:
     - build
     - deploy
build:
  stage: build
  script:
     - 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!"
  artifacts:
    paths:
      - build 
  only:
    - master # Only run on master branch
deploy_production:
  stage: deploy
  image: ubuntu
  before_script:
    - '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 'gitlab.com' >> ~/.ssh/known_hosts
    - apt-get install rsync -y -qq
    - apt-get install curl -y -qq

  script:
     - 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"
  environment:
    name: production
  only:
     - master # Only run on master branch

After click commit and check the result in CI/CD

Gitlab CI/CD deploy the website to VPS

Conclusion

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 DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

1 thought 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.

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.