How to use Gitlab-ci to continuously deploy to remote machines (detailed tutorial)

How to use Gitlab-ci to continuously deploy to remote machines (detailed tutorial)

Long story short, today we will talk about using Gitlab-CI to automatically deploy to remote servers.

Friends who have read this article will notice that I automatically deployed the site on the Gitlab-Runner server. This time we will use ssh to deploy to a remote machine (separate the CI server and the deployment server to avoid resource grabbing).

SSH password-free login

Again, CI/CD is essentially a scripting of our manual integration and copy deployment methods. The important aspect of remote deployment is to require password-free control.

For Gitlab Runner to deploy to a remote machine, the remote machine must trust gitlab runner account.

  • First execute su gitlab-runner to switch to the gitlab-runner account
  • Use the ssh-keygen command on your CI machine (master) to create a public key. Use ssh-keygen -t rsa to create it. The program will ask you for the storage directory. If you do not need to modify it, just press Enter a few times to copy the id_rsa.pub file in the ~/.ssh directory to the ~/.ssh directory of the controlled machine, and then
  • Import the file contents into the ~/.ssh/authorized_keys file
Host:
scp /home/gitlab-runner/.ssh/id_rsa.pub
The party being controlled:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

4. Set permissions on the controlled machine:
~/.ssh permissions are set to 700;
~/.ssh/authorized_keys permissions are set to 600

After that, the master CI machine will have the ability to log in to the remote machine without a password.

How to deploy continuously?

Continuous deployment using image tag: GitLab project just needs to type the tag --> execute the image building job (with this git tag as the image tag) --> execute the deployment job, get the git tag --> deploy the tag image

  • CI_COMMIT_REF_NAME variable gets the branch or tag name for which project is built
  • Set image: ${DOCKER_REGISTRY}/eap/eap-front-end:${TAG} in docker-compose.yml to sense the tag variable inserted during deployment.
build_image:Front-end:
 stage: build_image
 script:
 - docker build -t $DOCKER_REGISTRY_HOST/eap/eap-front-end:$CI_COMMIT_REF_NAME .  
 - docker login $DOCKER_REGISTRY_HOST -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD
 - docker push $DOCKER_REGISTRY_HOST/eap/eap-front-end:$CI_COMMIT_REF_NAME    
 tags:  
 -my-tag
 only:  
 - tags
 
deploy:alpha:
 stage: deploy
 variables:
 deploy_path: "/home/eap/website"
 script:
 - ssh -t ***@10.202.42.252 "cd $deploy_path && export TAG=$CI_COMMIT_REF_NAME && docker-compose -f docker-compose.yml build && docker-compose -f docker-compose.yml up -d" 
 tags:
 -my-tag
 only:
 - tags

The yellow background line above describes the scripting method of ssh remote login-->switch to the deployment directory-->insert the git tag of this build--->execute container deployment.

That'all, this article records the process of gitlab-ci continuous deployment to remote machines: ssh password-free login is what I have recently mastered, and the continuous deployment method is simple and practical.

This concludes this article on how to use Gitlab-ci to continuously deploy to remote machines (detailed tutorial). For more information about Gitlab-ci continuously deploying to remote machines, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to quickly deploy Gitlab using Docker
  • Detailed explanation of Docker+Jenkins+Gitlab+Django application deployment practice
  • Docker-compose one-click deployment of gitlab Chinese version method steps
  • How to deploy gitlab using Docker-compose

<<:  A quick guide to MySQL indexes

>>:  React native ScrollView pull down refresh effect

Recommend

js version to realize calculator function

This article example shares the specific code of ...

js tag syntax usage details

Table of contents 1. Introduction to label statem...

MySQL deep paging (how to quickly paginate tens of millions of data)

Table of contents Preface Case optimization summa...

The use of FrameLayout in six layouts

Preface In the last issue, we explained LinearLay...

Win2008 Server Security Check Steps Guide (Daily Maintenance Instructions)

The document has been written for a while, but I ...

Implementation code for infinite scrolling with n container elements

Scenario How to correctly render lists up to 1000...

Installation and use of Ubuntu 18.04 Server version (picture and text)

1 System Installation Steps OS Version:1804 Image...

CentOS7 firewall and port related commands introduction

Table of contents 1. Check the current status of ...

Vue implements dynamic query rule generation component

1. Dynamic query rules The dynamic query rules ar...

Introduction to MySQL isolation level, lock and MVCC

This article aims to clarify the relationship bet...

Solution to Nginx 500 Internal Server Error

Today, when I was using Nginx, a 500 error occurr...

JavaScript countdown to close ads

Using Javascript to implement countdown to close ...