17

When we use the traditional development method to develop the background system, we need to re-run the project every time we write a function point, and then test it. If the project is relatively small, it is ok, but if the project is relatively large, there are many people involved. , this development method is more troublesome. Based on this, we need to use Jenkins and Gitee to build an automated deployment platform and host the code on the server, which reduces the pressure on the local computer and liberates the deployment process.

1. Build the Jenkins platform

First, we need to build the Jenkins automated build platform. First, we need to install Docker, and then install Jenkins in Docker. The installation command is as follows:

# 安装yum-utils工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# 配置阿里云的Docker Yum源
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安装Docker
sudo yum install docker-ce docker-ce-cli containerd.io
# 启动Docker
sudo systemctl start docker
# 配置开机自启动
sudo systemctl enable docker

Through the above instructions, Docker can be successfully installed and started, and then we will run Jenkins through Docker.

docker run \
  -d \
  --rm \
  -u root \
  -p 8080:8080 \
  -v /home/jenkins-data:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /opt/develop_resource/apache-maven-3.6.3:/usr/local/maven \
  -v "$HOME":/home \
  jenkinsci/blueocean

Executing the above command Docker will automatically pull the Jenkins image and start it. Because we want to deploy SpringBoot, we need to prepare the JDK and Maven environments. However, the Jenkins image comes with a JDK environment. Just prepare Maven. First, Download the Maven tarball with the following command:

wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz

Then, use the command to extract the files.

tar -zxvf apache-maven-3.6.3-bin.tar.gz

After decompression, be sure to pay attention to the directory where Maven is located, for example:

/opt/develop_resource/apache-maven-3.6.3

Mount it in the container's directory. Remember to replace the Maven directory in the command -v /opt/develop_resource/apache-maven-3.6.3:/usr/local/maven with your own. Now, we can run the command just now to start Jenkins, and we can check whether the container is started through the docker ps command.

[root@10 /]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS         PORTS                                                  NAMES
dfa1b8b2c7a3   jenkinsci/blueocean   "/sbin/tini -- /usr/…"   15 seconds ago   Up 9 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 50000/tcp   condescending_meitner

Next, we use the server's ip plus port 8080 to access Jenkins.
在这里插入图片描述
The administrator password can be viewed in the Jenkins startup log, use docker logs dfa1b8b2c7a3 to view the log:
在这里插入图片描述
The password is the string in the red box, pay attention to a prompt under the red box:

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

This means that you can view the administrator password in the file /var/jenkins_home/secrets/initialAdminPassword, but this is the directory in the Jenkins container. When we start Jenkins, we mount the key directory of Jenkins /var/jenkins_home. The host directory is /home/jenkins-data, so you can use the following command to view the administrator password.

cat /home/jenkins-data/secrets/initialAdminPassword

After getting the password, enter it into the Jenkins page to unlock Jenkins, and then click Install the recommended plugin.
在这里插入图片描述
在这里插入图片描述
Next, just click [Next].
在这里插入图片描述
在这里插入图片描述
At this point, the Jenkins platform can be officially used.

2, Jenkins platform configuration

Next, is to configure the Jenkins platform, first configure Maven.
在这里插入图片描述
Click according to the steps to enter the system configuration. First, configure it in the global properties.
在这里插入图片描述
在这里插入图片描述
Remember the Maven directory we mount when we run the Jenkins container? The directory mounted to the Jenkins container is /usr/local/maven. If you really don't understand, you can keep the same configuration as mine.

In the same way, add another configuration:
在这里插入图片描述
The function of PATH+EXTRA is to keep the environment in the original PATH variable from being lost, and finally click Save. After Maven configuration is complete, Gitee needs to be configured.
在这里插入图片描述
在这里插入图片描述
Click the optional plug-in, select Gitee, and then click Install without restart to install, wait for the installation to complete, and we will talk about the related configuration of Gitee later.

3. Create a SpringBoot application

First, we create a simple SpringBoot application for testing, the controller code is as follows.

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(){
        return "Hello World!";
    }
}

Then in the configuration file application.yml add:

server:
  port: 8000

And create a new docker folder under main, and create a new Dockerfile file under the docker folder, the contents are as follows.

# 指定是基于哪个基础镜像
FROM java:8

# 作者信息
MAINTAINER wwj

# 挂载点声明
VOLUME /tmp

# 将本地的一个文件或目录,拷贝到容器的文件或目录里
ADD /target/demo-0.0.1-SNAPSHOT.jar springboot.jar

#shell脚本
RUN bash -c 'touch /springboot.jar'

# 将容器的8000端口暴露,给外部访问。
EXPOSE 8000

# 当容器运行起来时执行使用运行jar的指令
ENTRYPOINT ["java", "-jar", "springboot.jar"]

It should be noted that when the ADD instruction is written, when the SpringBoot application is packaged, its jar package will be placed in the target directory.
在这里插入图片描述
So you need to specify the location of the file, use the ADD command to put it into the container to be built, then create a new warehouse in Gitee, and push the code to the warehouse.
在这里插入图片描述
The warehouse name is whatever you want, and then just push the application just now.
在这里插入图片描述

4. Gitee configuration

After the push is completed, go back to the Jenkins management interface, let's complete the configuration of Gitee, and open the system configuration.
在这里插入图片描述
Find the Gitee configuration and fill in the corresponding information:
在这里插入图片描述
Click the Add button to add a Jenkins credential.
在这里插入图片描述
Select Gitee API Token:
在这里插入图片描述
The address for obtaining private tokens is: https://gitee.com/profile/personal_access_tokens .

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

5. Create a new automated deployment task

After the configuration is complete, create a new task and click New Item.
在这里插入图片描述
Just enter a task name and select [Freestyle project].
在这里插入图片描述
Check Git in the source code management, fill in the project address, and then check the timing of triggering packaging in the build trigger position.
在这里插入图片描述
Click Generate Gitee WebHook Password at the bottom of the build trigger.
在这里插入图片描述

Then open the WebHooks in the Gitee project and add the webHook.
在这里插入图片描述
在这里插入图片描述

The URL here needs to be filled with a public IP, so if you are using a virtual machine, you need to map it with an intranet penetration tool.

在这里插入图片描述
在这里插入图片描述
As for what the URL should be filled in, we need to modify it.
在这里插入图片描述
After filling in, click Add, and Gitee will send a Post request to Jenkins. If the request result is as shown below, the configuration is successful.
在这里插入图片描述
Go back to the Jenkins management interface, continue to check the polling SCM under the build trigger, and enter the polling frequency.
在这里插入图片描述
Finally, add a build step under the build location and choose to execute the shell.
在这里插入图片描述
The shell script code is as follows.

#!/bin/bash -il
docker rm -f app_docker
sleep 1
docker rmi -f app_docker:1.0
sleep 1
mvn clean install -Dmaven.test.skip=true
sleep 1
docker build -t app_docker:1.0 -f ./src/main/docker/Dockerfile .
sleep 1
docker run -d -p 8000:8000 --name app_docker app_docker:1.0

This script means to delete the running app_docker container, and delete the app_docker:1.0 image, then use the mvn command to package the project code pulled from Gitee, and then use the Dockerfile file in the project to build an image named app_docker:1.0, and finally Run the image.

6. Packaging test

Finally, click Save, and the deployment task is created. Let's test if there is any problem.
在这里插入图片描述
Click Build Now, Jenkins will immediately perform a build, and view the console output.
在这里插入图片描述
Finally, we can open the default address.


xiangzhihong
5.9k 声望15.3k 粉丝

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