1. Deploy the project to Docker
1.1 Project deployment process
Step 1: Upload the project to the server;
Step 2: Create a Docker image;
Step 3: Create a container;
Step 4: Start the container;
1.2 Configure the plug-in
The first step: configure the spring-boot-maven-plugin packaging plug-in.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Step 2: Configure the docker-maven-plugin plugin.
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<!--docker镜像相关的配置信息-->
<configuration>
<!--镜像名,这里用工程名-->
<imageName>${project.artifactId}-${project.version}</imageName>
<!--Dockerfile文件所在目录-->
<dockerDirectory>${project.basedir}/src/main/resources</dockerDirectory>
<!--TAG,这里用工程版本号-->
<imageTags>
<imageTag>${project.version}</imageTag>
</imageTags>
<imageName>${project.artifactId}:${project.version}</imageName>
<!--构建镜像的配置信息-->
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.artifactId}-${project.version}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
1.3 Writing Dockerfile
Create a new Dockerfile file in the project src/main/resources directory, the content of the file is as follows:
FROM java:8
ENV ARTIFACTID xc-govern-center
ENV ARTIFACTVERSION 1.0-SNAPSHOT
ENV HOME_PATH /home
WORKDIR $HOME_PATH
ADD /$ARTIFACTID-$ARTIFACTVERSION.jar $HOME_PATH/$ARTIFACTID.jar
ENTRYPOINT ["java", "-jar", "xc-govern-center.jar"]
1.4 Create a mirror
After copying the project to the server, you can enter the root directory of the project and execute the following commands.
mvn -f pom_docker.xml clean package -DskipTests docker:build
After the image is created successfully, you can deploy an application instance through the image for testing.
docker run --name 容器名称 -p 50101:50101 -idt 192.168.31.20:5000/镜像名称:镜像版本
Note: jdk, maven, and docker tools must also be installed on the server.
Two, create continuous integration tasks
2.1 Create a build task
Click "New Task" on the left side of the Jenkins homepage, and then enter the task name in the input box.
2.2 Configure Git
2.2.1 Configure Git Credentials
Git credentials are used to clone code from remote Gitlab.
Click "System" under "Credentials" on the left side of the Jenkins homepage.
Click "Add Credentials" on the left, then enter the username and password of the gitlab warehouse, and click the save button.
2.2.2 Configure Git Repository
Click My task to enter the task configuration page. Configure the Git repository in the source code management section.
2.3 Maven build configuration
- Use shell scripts to stop the container, delete the container, and delete the image.
The command is as follows:
#!/bin/bash
result=$(docker ps | grep "192.168.31.20:5000/ts-govern-center")
if [[ "$result" != "" ]]
then
echo "stop ts-govern-center"
docker stop ts-govern-center
fi
result1=$(docker ps -a | grep "192.168.31.20:5000/ts-govern-center")
if [[ "$result1" != "" ]]
then
echo "rm ts-govern-center"
docker rm ts-govern-center
fi
result2=$(docker images | grep "192.168.31.20:5000/ts-govern-center")
if [[ "$result2" != "" ]]
then
echo "192.168.31.20:5000/ts-govern-center:1.0-SNAPSHOT"
docker rmi 192.168.31.20:5000/ts-govern-center:1.0-SNAPSHOT
fi
- Perform a maven build.
The command is as follows:
clean package -f ts-govern-center/pom_docker_registry.xml -DskipTests docker:build
3. Pull the image, create the container, and start the container.
The command is as follows:
docker run --name xc-govern-center -p 50101:50101 -idt 192.168.31.20:5000/ts-govern-center:1.0-SNAPSHOT
docker logs -f ts-govern-center
2.4 Perform tasks
Go to the task page and click "Build Now".
View the build log information:
2.5 Automatic build
2.5.1 Add public key
Jenkins remotely accesses GitLab to obtain the source code. Here you need to configure the public key in GitLab.
Step 1: Enter the jenkins container and generate a public key.
ssh-keygen -t rsa -C "[email protected]"
cat ~/.ssh/id_rsa.pub
Step 2: Copy the content of the public key file.
Step 3: Enter GitLab to configure the public key.
2.5.2 Configure webhook
GitLab uses webhook to notify jenkins, and when there is a code push, it will notify jenkins to build.
The first step: install the gitlab plugin.
Step 2: Enter Jenkins settings to allow anonymous access to jenkins.
Select "System Configuration" in "System Management" to enter the system configuration page. Then find the gitlab part and uncheck the check box.
Step 3: Find the notification address in Jenkins and copy the address.
Step 4: Use the administrator to log in to jenkins and set permission to request local network services.
Select Configure GitLab on the homepage.
Select "Network" in "Settings", then expand Outbound requests and select the first check box.
Step 5: Set the hook address. Through the hook address, jenkins can access the project in gitlab.
- Log in to gitlab as a normal user again. Then enter the project home page.
- Select "Webhooks" in "Settings", and then enter the URL address, which is the hook address.
2.5.3 Test
Test steps:
- Modify the project code;
- Submit the code to gitlab;
- Check whether the task is started in Jenkins;