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

Nginx one domain name to access multiple projects method example

Background Recently, I encountered such a problem...

Tutorial on installing MySQL8 compressed package version on Win10

1 Download MySQL8 from the official website and i...

VMware virtual machine three connection methods example analysis

NAT In this way, the virtual machine's networ...

Native js to implement drop-down box selection component

This article example shares the specific code of ...

Centos7 installation of Nginx integrated Lua sample code

Preface The computer I use is a Mac, and the oper...

Using MySQL database with Python 3.4 under Windows 7

The detailed process of using MySQL database with...

js to achieve sliding carousel effect

This article shares the specific code of js to ac...

Use of Linux file command

1. Command Introduction The file command is used ...

React's transition from Class to Hooks

Table of contents ReactHooks Preface WhyHooks? Fo...

Vue implements various ideas for detecting sensitive word filtering components

Table of contents Written in front Requirements A...