ruoyi-vue gitlab-runner ci/cd automated deployment

Article directory

  • illustrate
  • 1. Environment
  • 2. Install Gitlab-Runner
  • 3. Register runner
      • Precautions
  • 4. Install docker
  • 5. Error handling
  • 6. Backend.gitlab-ci.yml
  • 7. Front-end.gitlab-ci.yml
  • 8. Description

Description

Manual deployment is mostly inconvenient. It requires a series of operations such as restarting the service every time. After the automated deployment and configuration of gitlab-runner is completed, it can be said to be done once and for all. Unless the server is migrated (there is no way), other operations are still very difficult. convenient. The content of this article is to realize automated deployment on the originally deployed environment. If you have not deployed before, please refer to other information on the Internet for corresponding deployment.

1. Environment

Operating system: CentOS Linux release 7.9.2009 (Core)
Database: mysql 5.7.32
Cache database: redis 6.0.8
Proxy service: nginx 1.22

2. Install Gitlab-Runner

#1. Download files
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

#2. Authorization
sudo chmod +x /usr/local/bin/gitlab-runner

#3. Create GitLab Runner user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

#4. Installation
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner

#5. Run the service
sudo gitlab-runner start

3. Register runner

group runner
image.png
shared runner
image.png
You can choose group or sharing. It is recommended to choose sharing.
Three need to be registered here, shell, back-end docker, and front-end docker. If the entered text is incorrect during the input process, don’t be busy deleting it. You can modify it in the config.toml configuration file or modify it in the runner in gitlab.
shell

[root@localhost app]# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=26285 revision=8ec04662 version=16.3.0
Running in system-mode.
                                                   
#Enter gitlab address
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://ip:port number/
#Input token, there are two types of tokens, one is group and the other is sharing
Enter the registration token:
VsrN69z1meZxmEWvS1hE
#Enter runner description
Enter a description for the runner:
[localhost.localdomain]: cooper
#Enter the runner tag, which corresponds to the tag in .gitlab.yml and can be modified in the interface
Enter tags for the runner (comma-separated):
cooper_shell_dev
#Enter instructions for maintenance
Enter optional maintenance note for the runner:
cooper
WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org /gitlab/-/issues/380872
Registering runner... succeeded runner=VsrN69z1
#Select the type of executor
Enter an executor: shell, docker + machine, instance, kubernetes, custom, docker, docker-windows, parallels, ssh, virtualbox, docker-autoscaler:
shell
#Enter the specific image, which is not used in my project
Enter the default Docker image (for example, ruby:2.7):
centos7:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
 
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

Backend docker

[root@localhost app]# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=26285 revision=8ec04662 version=16.3.0
Running in system-mode.
                                                   
#Enter gitlab address
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://ip:port number/
#Input token, there are two types of tokens, one is group and the other is sharing
Enter the registration token:
VsrN69z1meZxmEWvS1hE
#Enter runner description
Enter a description for the runner:
[localhost.localdomain]: cooper
#Enter the runner tag, which corresponds to the tag in .gitlab.yml and can be modified in the interface
Enter tags for the runner (comma-separated):
cooper_docker_dev
#Enter instructions for maintenance
Enter optional maintenance note for the runner:
cooper
WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org /gitlab/-/issues/380872
Registering runner... succeeded runner=VsrN69z1
#Select the type of executor
Enter an executor: shell, docker + machine, instance, kubernetes, custom, docker, docker-windows, parallels, ssh, virtualbox, docker-autoscaler:
docker
#Enter the specific image, which is not used in my project
Enter the default Docker image (for example, ruby:2.7):
centos7:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
 
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

Front-end docker

[root@localhost app]# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=26285 revision=8ec04662 version=16.3.0
Running in system-mode.
                                                   
#Enter gitlab address
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://ip:port number/
#Input token, there are two types of tokens, one is group and the other is sharing
Enter the registration token:
VsrN69z1meZxmEWvS1hE
#Enter runner description
Enter a description for the runner:
[localhost.localdomain]: vue-project
#Enter the runner tag, which corresponds to the tag in .gitlab.yml and can be modified in the interface
Enter tags for the runner (comma-separated):
vue_docker_dev
#Enter instructions for maintenance
Enter optional maintenance note for the runner:
vue_dev
WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org /gitlab/-/issues/380872
Registering runner... succeeded runner=VsrN69z1
#Select the type of executor
Enter an executor: shell, docker + machine, instance, kubernetes, custom, docker, docker-windows, parallels, ssh, virtualbox, docker-autoscaler:
docker
#Enter the specific image, which is not used in my project
Enter the default Docker image (for example, ruby:2.7):
centos7:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
 
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

After the registration is completed, the configuration file will be generated. The file address is /etc/gitlab-runner/config.toml. The following is the modified content. The specific content shall be subject to your own.

concurrent = 1
check_interval = 0
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "cooper"
  url = "http://ip:port/"
  id=12
  token = "vuVFQ7DPyfGUsmvKrnfk"
  executor = "shell"
  [runners.docker]
    tls_verify = false
    image = "centos7:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    pull_policy = ["if-not-present"]
    shm_size = 0

[[runners]]
  name = "cooper"
  url = "http://ip:port/"
  id=13
  token = "9UJ987Me85Tf8cUvwL73"
  executor = "docker"
  [runners.cache]
    MaxUploadedArchiveSize = 0
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "centos7:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache", "/home/gitlab-runner/.m2:/root/.m2"]
    pull_policy = ["if-not-present"]
    shm_size = 0

[[runners]]
  name = "vue-project"
  url = "http://ip:port/"
  id=14
  token = "jP7mzAfNhHtJjw8BArTv"
  executor = "docker"
  [runners.cache]
    MaxUploadedArchiveSize = 0
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "centos7:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    pull_policy = ["if-not-present"]
    shm_size = 0

Gitlab will also generate the corresponding content as shown below, two groups and one shared
image.png
image.png

Notes

The tags of different servers are set to different values, otherwise there will be problems and the execution results will not be what you want.

4. Install docker

# Install docker dependency packages before installing Docker
sudo yum install -y yum-utils
# Create Docker warehouse (map warehouse address)
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# Install
yum install -y docker-ce
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * centos-sclo-rh: mirrors.163.com
 * centos-sclo-sclo: mirrors.163.com
 * epel: mirror.01link.hk
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Resolving dependencies
--> Checking transaction
---> Package docker-ce.x86_64.3.24.0.6-1.el7 will be installed
--> Processing dependency container-selinux >= 2:2.74, which is required by package 3:docker-ce-24.0.6-1.el7.x86_64
--> Processing dependency containerd.io >= 1.6.4, which is required by package 3:docker-ce-24.0.6-1.el7.x86_64
--> Processing dependency docker-ce-rootless-extras, which is required by package 3:docker-ce-24.0.6-1.el7.x86_64
--> Checking transaction
---> Package container-selinux.noarch.2.2.119.2-1.911c772.el7_8 will be installed
---> Package containerd.io.x86_64.0.1.6.22-3.1.el7 will be installed
--->The package docker-ce-rootless-extras.x86_64.0.24.0.6-1.el7 will be installed
--> Dependency resolution completed

dependency resolution

================================================== ================================================== =============
 Package Architecture Version Source Size
================================================== ================================================== =============
Installing:
 docker-ce x86_64 3:24.0.6-1.el7 docker-ce-stable 24 M
Install for dependencies:
 container-selinux noarch 2:2.119.2-1.911c772.el7_8 extras 40k
 containerd.io x86_64 1.6.22-3.1.el7 docker-ce-stable 34 M
 docker-ce-rootless-extras x86_64 24.0.6-1.el7 docker-ce-stable 9.1 M

transaction summary
================================================== ================================================== =============
Install 1 package (+ 3 dependent packages)

Total downloads: 67 M
Installation size: 225M
Downloading packages:
No Presto metadata available for docker-ce-stable
(1/4): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm | 40 kB 00:00:00
(2/4): docker-ce-24.0.6-1.el7.x86_64.rpm | 24 MB 00:00:51
(3/4): containerd.io-1.6.22-3.1.el7.x86_64.rpm | 34 MB 00:00:51
(4/4): docker-ce-rootless-extras-24.0.6-1.el7.x86_64.rpm | 9.1 MB 00:00:08
-------------------------------------------------- -------------------------------------------------- -------------
Total 1.1 MB/s | 67 MB 00:00:59
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing: 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 1/4
setsebool: SELinux is disabled.
  Installing: containerd.io-1.6.22-3.1.el7.x86_64 2/4
  Installing: 3:docker-ce-24.0.6-1.el7.x86_64 3/4
  Installing: docker-ce-rootless-extras-24.0.6-1.el7.x86_64 4/4
  Verifying: docker-ce-rootless-extras-24.0.6-1.el7.x86_64 1/4
  Verifying: containerd.io-1.6.22-3.1.el7.x86_64 2/4
  Verifying: 3:docker-ce-24.0.6-1.el7.x86_64 3/4
  Verifying: 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 4/4

Installed:
  docker-ce.x86_64 3:24.0.6-1.el7

Installed as a dependency:
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8 containerd.io.x86_64 0:1.6.22-3.1.el7
  docker-ce-rootless-extras.x86_64 0:24.0.6-1.el7

complete!

# start up
systemctl start docker
# Set startup at boot
systemctl enable docker
# Check docker version
docker -v
# Configure image accelerator
#Create if there is no such file. By default, there is a docker file.
sudo mkdir -p /etc/docker
# Set the image content into the daemon.json file
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}
EOF
# After the modification is completed, reload the configuration file
systemctl daemon-reload
# Restart docker
systemctl restart docker
sudo chown root:docker /var/run/docker.sock # Modify docker.sock permissions to root:docker
sudo groupadd docker # Add docker user group
sudo gpasswd -a $USER docker # Add the current user to the docker user group
sudo gpasswd -a gitlab-runner docker # Add the gitlab-runner user to the docker user group
newgrp docker # Update docker user group
# Create time zone file
echo 'Asia/Shanghai'>/etc/timezone
# The image that needs to be pulled
docker pull openjdk:8
docker pull maven:3.6.3-jdk-8
docker pull node:18
docker pull nginx:1.23

5. Error handling

1. The git version is too low
image.png

yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
yum install git
yum update git

2. The user is not added to the docker user group
image.png

# Add gitlab-runner to the docker group
gpasswd -a gitlab-runner docker

3. How to modify the mounting directory of a container that has been started by Docker

# View container ID
docker ps
# Stop docker
systemctl stop docker
# Modify the mount item in Binds
vi /var/lib/docker/containers/containerid/hostconfig.json
# Modify mounting items in MountPoints
vi /var/lib/docker/containers/containerid/config.v2.json

image.png

# Start docker:
systemctl start docker

6. backend.gitlab-ci.yml

#Introduce different branch configuration files
include:
  - local: gitlab-ci-dev.yml
    rules:
      - if: $CI_COMMIT_BRANCH == 'dev'
  - local: gitlab-ci-master.yml
    rules:
      - if: $CI_COMMIT_BRANCH == 'master'
# This pipeline is build to clone newest repo to sanic project

default:
  tags:
    - cooper_shell_dev
#Judge branch
#workflow:
# rules:
# - if: $CI_COMMIT_BRANCH == 'dev' || $CI_COMMIT_BRANCH == 'master'

variables:
  BASIC_DIR: /home/gitlab-runner/repo
  PROJECT_DIR: ${<!-- -->BASIC_DIR}/${<!-- -->CI_PROJECT_NAME}
  CUSTOM_IMAGE: 'hv-cooperation'
  CONTAINER_NAME: 'hvCooperation'
  IMAGE_NAME: 'openjdk:8'
  SERVER: '-p 8080:8080 -v ${PROJECT_DIR}:/app'
  TIMEZONE: '-v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro'
  FILE_DIR: '-v /home/ruoyi/uploadPath:/home/ruoyi/uploadPath'
  AUTO_RESTART: '--restart unless-stopped'
  CREATE_CONTAINER: 'docker run ${TIMEZONE} ${FILE_DIR} ${SERVER} ${AUTO_RESTART} --privileged=true --name $CONTAINER_NAME -d ${CUSTOM_IMAGE}'
  MAVEN_OPTS: "-Djava.awt.headless=true -Dmaven.repo.local=/root/.m2/repository"
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version"
  DEV_BUILD: "mvn clean package -B -e -Dmaven.test.skip=true -P test $MAVEN_OPTS --settings=/root/.m2/settings-docker.xml"
  PROD_BUILD: "mvn clean package -B -e -Dmaven.test.skip=true -P prod $MAVEN_OPTS --settings=/root/.m2/settings-docker.xml"

stages: stages
  -build
  - package
  -deploy

image-build:
  stage: build
  rules:
    - if: $CI_COMMIT_TITLE =~ /image/
  tags:
    -cooper_shell_dev
  script:
    - docker build -t ${<!-- -->CUSTOM_IMAGE} .

maven-build:
  stage: package
  tags:
    - cooper_docker_dev
  image: maven:3.6.3-jdk-8
  # cache:
  # key: ${CI_COMMIT_REF_NAME}
  # paths:
  # - /root/.m2/
  script:
    - echo $DEV_BUILD
    - echo $PROD_BUILD
    - mkdir -p /home/gitlab-runner/.m2
    - $DEV_BUILD
  artifacts:
    paths:
      - ruoyi-admin/target/*.jar
deploy:
  stage:deploy
  # rules:
  # - if: $CI_COMMIT_BRANCH == 'main'
  script:
    - echo PROJECT_DIR - $PROJECT_DIR
    - echo CI_PROJECT_DIR - ${<!-- -->CI_PROJECT_DIR}
    - mkdir -p $PROJECT_DIR
    - cp -a ruoyi-admin/target/* $PROJECT_DIR
    - |
      echo 'Auto restart or create container';
      if [ "$(docker container ls --no-trunc -q -f name=^${CONTAINER_NAME}$)" != "" ];
      then (echo Restart: & amp; & amp; docker restart $(docker container ls -q -f name=^${CONTAINER_NAME}$));
      else (echo Create: ${CREATE_CONTAINER} & amp; & amp; ${CREATE_CONTAINER});
      fi

For example: Project scenario: Example: Communicate with the mobile phone APP through the Bluetooth chip (HC-05), transmitting a batch of sensor data every 5 seconds (not very large)

7. Front-end.gitlab-ci.yml

#Introduce different branch configuration files
include:
  - local: gitlab-ci-dev.yml
    rules:
      - if: $CI_COMMIT_BRANCH == 'dev'
  - local: gitlab-ci-main.yml
    rules:
      - if: $CI_COMMIT_BRANCH == 'main'
# This file is written to deploy vue project

default:
  image: node:18

#workflow:
# rules:
# - if: $CI_COMMIT_REF_NAME == 'dev' || $CI_COMMIT_REF_NAME == 'main'

stages: stages
  -build
  -deploy
  -launch

variables:
  BASIC_DIR: /home/gitlab-runner/repo/${<!-- -->CI_PROJECT_PATH} #Basic deployment directory
  NGINX_DIR: ${<!-- -->BASIC_DIR}/nginx #nginx configuration file directory
  MAIN_DIR: ${<!-- -->BASIC_DIR}/main #Production branch directory
  DEV_DIR: ${<!-- -->BASIC_DIR}/dev #Test branch directory

Build Vue Project:
  stage: build
  tags:
    - vue_docker_dev
  cache:
    key: ${<!-- -->CI_COMMIT_REF_NAME}
    paths:
      -node_modules/
  script:
    - echo "Front-end-management terminal starts packaging"
    - npm config set registry https://registry.npmmirror.com & amp; & amp; npm install
    - npm run build:stage
    - echo "Front-end-management end packaging"
  artifacts:
    name: dist
    paths:
      -dist/

Deploy Vue Project:
  stage:deploy
  tags:
    - cooper_shell_dev
  cache: {<!-- -->}
  variables:
    DEPLOY_DIR: ${<!-- -->BASIC_DIR}/${<!-- -->CI_COMMIT_REF_NAME} # Actual branch deployment directory
  before_script:
    #Create the corresponding configuration file and project directory
    - "echo Create dir: ${DEPLOY_DIR}"
    - mkdir -p ${<!-- -->NGINX_DIR} ${<!-- -->MAIN_DIR} ${<!-- -->DEV_DIR}
  script:
    - echo CI_PROJECT_NAME ${<!-- -->CI_PROJECT_NAME}
    - echo CI_PROJECT_PATH ${<!-- -->CI_PROJECT_PATH}
    - echo CI_PROJECT_DIR ${<!-- -->CI_PROJECT_DIR}
    - echo CI_COMMIT_REF_NAME ${<!-- -->CI_COMMIT_REF_NAME}
    - echo NGINX_DIR ${<!-- -->NGINX_DIR}
    - rm -rf ${<!-- -->DEPLOY_DIR}/*
    - mv -f dist/* ${<!-- -->DEPLOY_DIR}

Launch Nginx:
  stage: launch
  tags:
    -cooper_shell_dev
  rules:
    - if: $CI_COMMIT_TITLE =~ /nginx/
  variables:
    IMAGE_NAME: 'nginx:1.23'
    CONTAINER_NAME: cooper_nginx
    TIMEZONE: '-v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro'
    SERVER: '-p 80:80 -v ${BASIC_DIR}/${CI_COMMIT_REF_NAME}:/app/nginx/html/dist'
    NGINX_CONF: '-v ${NGINX_DIR}/nginx.conf:/etc/nginx/nginx.conf:ro'
    AUTO_RESTART: '--restart unless-stopped'
    CREATE_CONTAINER: docker run ${<!-- -->TIMEZONE} ${<!-- -->SERVER} ${<!-- -->NGINX_CONF} ${<!-- -->AUTO_RESTART} -- name ${<!-- -->CONTAINER_NAME} -d $IMAGE_NAME
  before_script:
    - rm -rf ${<!-- -->NGINX_DIR} #Delete the nginx directory and all files below it
    - cp -a nginx ${<!-- -->NGINX_DIR}
  script:
    - |
      if [ "$(docker container ls --no-trunc -q -f name=^${CONTAINER_NAME}$)" != "" ];
      then (echo Restart ${CONTAINER_NAME}: & amp; & amp; docker restart $(docker container ls -q -f name=^${CONTAINER_NAME}$));
      else (echo Create ${CONTAINER_NAME}: ${CREATE_CONTAINER} & amp; & amp; ${CREATE_CONTAINER});
      fi

8. Description

1. The front-end and back-end testing and production environments are deployed on different servers, with different configurations and corresponding to different branches.
2. There are subtle differences between the production environment configuration and the test environment.
3. .gitlab-ci.yml is in the root directory
4. Create tests and branches separately on the front end