Jenkins+Docker+Gitee+SpringBoot automated deployment

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. -v /opt/develop_resource/apache-maven-3.6.3:/usr/local/mavenRemember to replace the Maven directory in this command with your own. Now, we can run the command just now to start Jenkins, and we docker pscan check whether the container is started through the 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.
insert image description here
The administrator password can be viewed in the startup log of Jenkins. Use docker logs dfa1b8b2c7a3 to view the log:
insert image description here
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.
insert image description here
insert image description here
Next, just click [Next].
insert image description here
insert image description here
At this point, the Jenkins platform can be officially used.

2, Jenkins platform configuration

Next, is to configure the Jenkins platform, first configure Maven.
insert image description here
Click according to the steps to enter the system configuration. First, configure it in the global properties.
insert image description here
insert image description here
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:
insert image description here
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.
insert image description here
insert image description here
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.
insert image description here
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.
insert image description here
The warehouse name is whatever you want, and then just push the application just now.
insert image description here

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.
insert image description here
Find the Gitee configuration and fill in the corresponding information:
insert image description here
Click the Add button to add a Jenkins credential.
insert image description here
Select Gitee API Token:
insert image description here
The address to obtain the private token is: https://gitee.com/profile/personal_access_tokens .

insert image description here
insert image description here

insert image description here

5. Create a new automated deployment task

After the configuration is complete, create a new task and click New Item.
insert image description here
Just enter a task name and select [Freestyle project].
insert image description here
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.
insert image description here
Click Generate Gitee WebHook Password at the bottom of the build trigger.
insert image description here

Then open the WebHooks in the Gitee project and add the webHook.
insert image description here
insert image description here

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.

insert image description here
insert image description here
As for what the URL should be filled in, we need to modify it.
insert image description here
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.
insert image description here
Go back to the Jenkins management interface, continue to check the polling SCM under the build trigger, and enter the polling frequency.
insert image description here
Finally, add a build step under the build location and choose to execute the shell.
insert image description here
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.
insert image description here
Click Build Now, Jenkins will immediately perform a build, and view the console output.
insert image description here
Finally, we can open the default address.

Guess you like

Origin blog.csdn.net/xiangzhihong8/article/details/123184099