preface

Automatic deployment refers to the code merged into a branch and automatically deployed to the server, which can greatly improve development efficiency 🚀 and is consistent with agile development

Through this article, we can learn how to set up the automatic deployment, and at the end of the article attached to the pit record, hoping to let everyone less detour, if there is an error can first check whether there is a relevant solution in the pit record below.

concept

First of all, I would like to understand the following two concepts 📜 :

Gitlab-runner: is the carrier of Gitlab-CI (continuous integration), which is used to execute.gitlab-ci. yML and return the result to GitLab. Gitlab-runner needs to associate the project with token, which can be associated with multiple or one project

.gitlab-ci.yml: file placed at the root of the project. After push, Gitlab parses this file and executes it with gitlab-runner

process

Setting up the process for automated deployment is simple

  1. Install Gitlab-Runner on the server and associate the repository.
  2. Implement crypt-free login to the server where the project is deployed and update the built file

It is actually very simple, but do it may encounter a lot of pits, make good use of search engines can be excluded one by one! The article will be accompanied by a record of trampling

implementation

Start by initializing any project, hosting it on gitLab (or hosting it on your own deployed private GitLab), and manually deploying it to the server without further discussion about deployment

What we’re going to do is push the code to the master, and automatically deploy the new code to the server, and refresh the page to see the latest results

As shown below, I randomly wrote a page and deployed it on my own server. Since there is no domain name, I access it directly through IP

Install gitlab – runner

Log in to another server and install Gitlab-Runner

  • Download the corresponding installation package according to your server system installation, mine is 64-bit centos7 system, so install the first one
# Linux x86-64
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# Linux x86
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386

# Linux arm
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm
Copy the code
  • Assign permissions to Gitlab-runner
sudo chmod +x /usr/local/bin/gitlab-runner
Copy the code
  • Create a user named gitlab-runner on which the subsequent configuration of secret-free login will be performed
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
Copy the code
  • The installation
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
Copy the code
  • Enter the GitLab URL, or the address of your private GitLab if your code is hosted in private GitLab

  • Enter the token

There are two kinds of tokens

One is the Shared Runner, which can be used by all projects

On the other hand, Specific runners can only be used for Specific projects. Enter a project, find Settings, CI/CD, and run

  • Enter runner description

  • Enter runner tag

  • Enter the executor shell

If the installation is successful, you should see the following description

  • Install git

Gitlab-runner requires git to be installed

#Install Git (-y = yes)
yum -y install git

#Check whether the Git version is successfully installed
git --version
Copy the code
  • Install YARN. For details, see linuxize.com/post/how-to…

To implement secret-free login

When we install Gitlab-Runner, we create a gitlab-Runner user

  • Now make sure we are under user gitlab-runner
# check the current user
whoami

# If not under Gitlab-Runner, switch
su gitlab-runner
Copy the code
  • Generate the key
ssh-keygen
Copy the code
  • View the generated content
# ~ points to the user's home directory
cd ~/.ssh
ls -a
Copy the code
  • Send the public key to the server installed on the deployment project
ssh-copy-id [email protected] # Public IP address of the server where the project is deployed
Copy the code
  • Enter the server password

  • After completion, you will be prompted to try to log in and check whether it is successful

ssh [email protected]
Copy the code

If the login succeeds, then our server deployment part is a success.

Write. Gitlab – ci. Yml

cache:
  paths:
    - node_modules # cache node_modules

stages:
  - install
  - build
  - deploy

install:
  stage: install
  only:
    - master Only master changes are processed
  script:
    - echo 'Install Dependency Phase'
    # - yarn config set registry https://registry.npm.taobao.org
    # - yarn config set sass-binary-site https://npm.taobao.org/mirrors/node-sass
    - yarn
  tags:
    - test # install gitlab-runner tag

build:
  stage: build
  script:
    - echo 'Construction phase'
    - yarn build
  artifacts:
    paths:
      - dist
  tags:
    - test

deploy:
  stage: deploy
  script:
    - echo 'Replace file stage'
    - cd dist
    - pwd
    - whoami
    - ssh [email protected]
    # list all files
    - ssh [email protected] "ls -a"
    Delete everything in the project folder
    - ssh [email protected] "rm -rf ./home/myapp/test/*"
    Use SCP to copy files remotelySCP - r - P 22. / * [email protected]: / home/myapp /test
  tags:
    - test
Copy the code

Now that the automatic deployment has been set up successfully, modify the code and push to view the result

Record on pit

  1. Using curl to download the Gitlab-Runner installation package is slow
For example, download Linux 64-bit
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
Copy the code

Solution: Open the download address in the browser, rename the file to gitlab-runner, and upload the file to /usr/local/bin/

  1. Gitlab-runner failed to install sudo: Modify/etc/sudoers file and find similar to this one line: Defaults secure_path = / sbin: / bin: / usr/sbin, / usr/bin will execute the command directory is added to the back, can, such as: Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
Modify/etc/sudoers file and find similar to this one line: Defaults secure_path = / sbin: / bin: / usr/sbin, / usr/bin will execute the command directory is added to the back, can, such as:  Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
Copy the code
  1. Install phase error git fetch – pack: expected shallow list upgrade git, reference: sysadmin.cyklodev.com/fatal-git-f…

reference

Install gitlab- Runner Gitlab automatic deployment blog install YARN upgrade Git