7
头图
I wrote an article before "Goodbye Jenkins! A few lines of scripts are used to automate deployment, this artifact is a bit powerful! 》 , talking about the use of Gogs+Drone to achieve automated deployment. I recently discovered that Gitlab's CI/CD function can also realize automated deployment, and it's quite simple to use! If you are using Gitlab as a Git repository, you might as well try its CI/CD function. This article still takes SpringBoot's automated deployment as an example to practice Gitlab's CI/DI function, I hope it will be helpful to everyone!

SpringBoot actual combat e-commerce project mall (50k+star) address: https://github.com/macrozheng/mall

Install

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

Install Gitlab

. Friends who are not familiar with the installation and use of Own Git Repository in 10 Minutes" 16100ba2ee4099.
docker run --detach \
  --hostname git.macrozheng.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
192.168.7.134 git.macrozheng.com
server {
    listen       80; # 同时支持HTTP
    server_name  git.macrozheng.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 that, we can access git.macrozheng.com through the domain name 06100ba2ee4351, 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 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!

Install Gitlab Runner

Gitlab is just a code repository. If you want to implement CI/CD, you need to install gitlab-runner . gitlab-runner equivalent to the executor of tasks in Gitlab. Gitlab will call it when it needs to perform tasks.
  • First, download the gitlab-runner Docker image, choose alpine-bleeding , this version is very small!
docker pull gitlab/gitlab-runner:alpine-bleeding
  • 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 time, if we check gitlab-runner , we will find the following error. The config.toml cannot be found. Don't worry about this problem. When we 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 gitlab-runner to Gitlab, turn on 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
docker exec -it gitlab-runner /bin/bash
  • 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 remotely execute Linux commands, which is very easy to use. It is recommended to use this!

  • 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"
  • In the CI/CD settings of Gitlab, we can find that a runner is successfully registered!

Install Maven

SpringBoot project packaging needs to rely on Maven, we need to install it on the server first.

  • 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
  • Modify the /etc/profile file and add environment variable configuration:
export MAVEN_HOME=/mydata/apache-maven-3.8.1
export PATH=$PATH:$MAVEN_HOME/bin
  • Test whether the installation is successful by checking the Maven version.
mvn -v
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"

Install JDK

JRE is installed by default on CentOS, and JDK needs to be installed to use Maven.

  • After the download is complete, 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
  • 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

use

Everything is ready, then the automated deployment of SpringBoot applications can be achieved through the CI/CD function of Gitlab!
  • .gitlab-ci.yml file in the root directory of the project to 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 run the script run.sh package the Docker image of the application and run it;
# 打包任务
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 is worth mentioning here that by default the runner will only execute the job with the same label. Since we set the label for both the job and the runner to docker , 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 ssh actuator, it will log on to our designated server, do we .gitlab-ci.yml defined script command, will get the code to start Git repository before, so we need to modify the next The host file on the server;
vim /etc/hosts
192.168.7.134 git.macrozheng.com
  • The next step is to submit the script to the Git repository. After submission, the task being executed Project->CI/CD->Pipelines

  • Open the details page of the Pipeline, you can find that the two tasks we defined have been executed successfully;

  • Open the job details interface, we can see the log information output during the task execution;

  • If you want to manually execute the Pipeline instead of submitting the trigger, you can click the Run Pipeline button on the Pipelines page;

Summarize

If you use Gitlab as a Git repository, 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. In fact, we have introduced many automated deployment solutions before and, such as Jenkins, Gogs+Drone, Gitlab CI/CD, we can find one thing in common, these solutions are inseparable from Linux commands. So if you want to play with automated deployment, you have to play with Linux commands first!

Reference

Official document: https://docs.gitlab.com/ee/ci/

Project source code address

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-gitlab

This article GitHub https://github.com/macrozheng/mall-learning has been included, welcome to Star!

macrozheng
1.1k 声望1.3k 粉丝