5

Previously, to achieve automated packaging and deployment, Jenkins was generally needed. However, now Gitlab's CI/CD function can also be automated deployment, and the operation is simpler. If you are also using Gitlab as a Git repository, you might as well try its CI/CD function.

1. Installation

To achieve automated deployment through the CI/CD function of Gitlab, we need to install services such as Gitlab, Gitlab Runner, and Maven.

1.1 Install Gitlab

First, we need to install Gitlab on our own server. If you don’t know how to install it yet, you can refer to " Build Your Own Git Repository in 10 Minutes" .

In order to avoid some unnecessary troubles, we can use the following command to run the Gitlab service. Here we need to pay attention to the addition of the hostname attribute, so that we can access Gitlab through the domain name. The environment variable GITLAB_ROOT_PASSWORD can be directly set to the root account in Gitlab password:

docker run --detach \
  --hostname git.bilibili.com \
  --publish 10443:443 --publish 1080:80 --publish 1022:22 \
  --name gitlab \
  --restart always \
  --volume /mydata/gitlab/config:/etc/gitlab \
  --volume /mydata/gitlab/logs:/var/log/gitlab \
  --volume /mydata/gitlab/data:/var/opt/gitlab \
  -e GITLAB_ROOT_PASSWORD=12345678 \
  gitlab/gitlab-ce:latest

Then, we can access Gitlab through the domain name git.bilibili.com. If you don't have a domain name, you can do so by modifying the host file of the machine:

192.168.7.134 git.bilibili.com

Since our Gitlab runs on port 1080, if we want to access without a port, we can use Nginx's reverse proxy for processing. Friends who are not familiar with Nginx can "These magical uses of Nginx, you must not knew! " . The following is the git.conf configuration file added in the Nginx configuration folder, the content is as follows:

server {
    listen       80; # 同时支持HTTP
    server_name  git.bilibili.com; #修改域名

    location / {
        proxy_pass   http://192.168.7.134:1080; # 设置代理服务访问地址
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

After completion, we can access Gitlab through the domain name git.bilibili.com, and enter the account password root:12345678 to log in.
在这里插入图片描述
Upload our SpringBoot application code to Gitlab, so Gitlab is ready! It should be noted here that if you did not specify a hostname when you started Gitlab, your project's HTTP access address will be the ID of the container, and you will not be able to access the Git repository if you use this address!
在这里插入图片描述

1.2 Install Gitlab Runner

Gitlab is just a code repository. If you want to implement CI/CD, you need to install gitlab-runner. Gitlab-runner is equivalent to the executor of tasks in Gitlab. Gitlab will call it when it needs to perform tasks.

First, download the Docker image of gitlab-runner and choose alpine-bleeding. This version is very small.

docker pull gitlab/gitlab-runner:alpine-bleeding

Then, use the following command to run gitlab-runner.

docker run --name gitlab-runner --restart always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /mydata/gitlab-runner:/etc/gitlab-runner \
-d gitlab/gitlab-runner:alpine-bleeding

At this point, if we check the container log of gitlab-runner, we will find the following error, and the config.toml file cannot be found. Don't worry about this problem, when we register gitlab-runner to Gitlab, the file will be automatically generated.

ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory  builds=0

Next, we need to register gitlab-runner to Gitlab, open the Project->Settings->CI/CD function, and get the address and token needed for runner registration.
在这里插入图片描述
Next, use the following command to enter the inside of the gitlab-runner container.

docker exec -it gitlab-runner /bin/bash

Then, use the following command to register the runner in the container.

gitlab-runner register

When registering, an interactive interface will appear, prompting you to enter the registration address, token, actuator type and other information. The ssh actuator can execute Linux commands remotely, as shown below.
在这里插入图片描述
After the registration is completed, we can find that the config.toml file has been generated, and the content is as follows. When we want to modify the runner configuration in the future, we can directly modify this file.

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "docker-runner"
  url = "http://192.168.7.134:1080/"
  token = "c2kpV6tX6woL8TMxzBUN"
  executor = "ssh"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.ssh]
    user = "root"
    password = "123456"
    host = "192.168.7.134"
    port = "22"

Next, open the project on Gitlab, and then in the CI/CD settings of Gitlab, you can find that the runner is successfully registered!

在这里插入图片描述

1.3 Install Maven

SpringBoot project packaging needs to rely on Maven, we need to install it on the server first. First, download the Linux installation package of Maven, download address .
在这里插入图片描述
After the download is complete, use the following command to extract to the specified directory.

cd /mydata
tar -zxvf apache-maven-3.8.1-bin.tar.gz

Next, modify the /etc/profile file to add environment variable configuration.

export MAVEN_HOME=/mydata/apache-maven-3.8.1
export PATH=$PATH:$MAVEN_HOME/bin

We can use the mvn -v command to test whether Maven is installed successfully.

Maven home: /mydata/apache-maven-3.8.1
Java version: 1.8.0_292, vendor: AdoptOpenJDK, runtime: /mydata/java/jdk1.8/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"

1.4 Install JDK

JRE is installed by default on CentOS, and JDK needs to be installed to use Maven. First, download JDK 8.
在这里插入图片描述 downloading 161789d9585810, unzip the JDK to the specified directory.

cd /mydata/java
tar -zxvf OpenJDK8U-jdk_x64_linux_xxx.tar.gz
mv OpenJDK8U-jdk_x64_linux_xxx.tar.gz jdk1.8

Then, add the environment variable JAVA_HOME in the /etc/profile file.

vi /etc/profile
# 在profile文件中添加
export JAVA_HOME=/mydata/java/jdk1.8
export PATH=$PATH:$JAVA_HOME/bin
# 使修改后的profile文件生效
. /etc/profile

2. Use CI/CD for deployment

Everything is ready, then the automated deployment of SpringBoot applications can be achieved through the CI/CD function of Gitlab! First, add the .gitlab-ci.yml file to the root directory of the project, and define two tasks. One task will package the application code into a Jar package and copy it to the specified directory, and the other task will be packaged by running the script run.sh The Docker image of the application is running.

# 打包任务
build-job:
  stage: build
  # 指定标签,只有具有该标签的runner才会执行
  tags:
    - docker
  script:
    # 使用Maven打包
    - mvn clean package
    # 将jar包、Dockerfile、运行脚本复制到指定目录
    - cp target/mall-tiny-gitlab-1.0-SNAPSHOT.jar /mydata/build/mall-tiny-gitlab-1.0-SNAPSHOT.jar
    - cp Dockerfile /mydata/build/Dockerfile
    - cp run.sh /mydata/build/run.sh

# 部署任务
deploy-job:
  stage: deploy
  tags:
    - docker
  script:
    # 进入指定目录并执行运行脚本
    - cd /mydata/build
    - chmod +x run.sh
    - ./run.sh

It should be noted that, by default, the runner will only execute jobs with the same label. Since we have set the label to docker for both the job and the runner, we can execute it here. If you have not set a label, you need to enable the runner to execute the job without a label under the settings of the editor interface of the runner.
在这里插入图片描述
Since our gitlab-runner uses the ssh executor, it will log in to the server we specify, execute the script command we defined in .gitlab-ci.yml, and get the code from the Git repository before that , So we also need to modify the host file on the server.

vim /etc/hosts
192.168.7.134 git.bilibili.com

The next step is to submit the script to the Git repository. After submission, the task being executed will be found in Project->CI/CD->Pipelines.
在这里插入图片描述
Open the details page of the Pipeline, you can find the execution results of the two tasks we defined.

在这里插入图片描述
Open the job details interface, we can see the log information output during the execution of the task.
在这里插入图片描述

If you want to manually execute the Pipeline instead of submitting the trigger, you can click the Run Pipeline button on the Pipelines page.
在这里插入图片描述
As you can see, it's really good to use its CI/CD function to automate deployment! Install a lightweight gitlab-runner and write a simple .gitlab-ci.yml script file.


xiangzhihong
5.9k 声望15.3k 粉丝

著有《React Native移动开发实战》1,2,3、《Kotlin入门与实战》《Weex跨平台开发实战》、《Flutter跨平台开发与实战》1,2、《Android应用开发实战》和《鸿蒙HarmonyOS应用开发实践》