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.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。