Jenkins + Docker one-click automated deployment of SpringBoot applications with the most streamlined process

This article implements the simplest and most comprehensive one-click automatic deployment project of Jenkins + Docker + Spring Boot. Complete steps, avoid pitfalls.

  • Environment: CentOS7 + Git (Gitee)

  • Implementation steps: Install Jenkins on Docker, configure the basic information of Jenkins, and use Dockerfile and Shell script to automatically pull, package and run the project.

1Install Docker

Install the community version Docker CE

  1. Make sure the yum package is updated to the latest

yum update
  1. Uninstall the old version (if you installed it)

yum remove docker docker-common docker-selinux docker-engine
  1. Install required packages

yum install -y yum-utils device-mapper-persistent-data lvm2
  1. Set up yum source

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  1. Install Docker

yum install docker-ce #Since only the stable warehouse is enabled by default in the repo, the latest stable version 17.12.0 is installed here.
yum install <own version> # For example: sudo yum install docker-ce-17.12.0.ce
  1. Start and set up startup

systemctl start docker
systemctl enable docker
  1. Verify installation was successful

docker version

2Install Jenkins

Jenkins Chinese official website: https://www.jenkins.io/zh/

1. Install Jenkins

Docker installation is so easy. Pay attention to check whether 8080 is already occupied. If it is occupied, please modify the port.

docker run --name jenkins -u root --rm -d -p 8080:8080 -p 50000:50000 -v /var/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/ var/run/docker.sock jenkinsci/blueocean

If the port number is not changed, the access address after the installation is completed:

http://{Service IP where Jenkins is deployed}:8080

There will be a few minutes of waiting here.

2. Initialize Jenkins

For details, see the official website tutorial: https://www.jenkins.io/zh/doc

2.1 Unlock Jenkins
# Enter the Jenkins container
docker exec -it {Jenkins container name} bash
# For example docker exec -it jenkins bash

# View password
cat /var/lib/jenkins/secrets/initialAdminPassword

# Copy the password into the input box

41c19744ff26ea8cf3476e80385ae688.png

2.2 Install plug-in

Choose the first option: Install recommended plug-ins.

14e549754608e5c85fea7540838b0a58.png

2.3 Create administrator user

Be sure to remember this account information.

3 System Configuration

1. Installation requires plug-ins

Enter [Homepage] – [System Management] – [Plug-in Management] – [Optional Plug-in]. Search for the following plug-ins that need to be installed and click to install.

f483af2022167163b2813103705a08e3.png

Install Maven Integration

Install Publish Over SSH (if you don’t need remote push, no need to install)

If you use Gitee code cloud, install the Gitee plug-in (it does not need to be installed separately if it comes with Git)

2. Configure Maven

Go to [Home] – [System Management] – [Global Configuration], scroll to the bottom of the page and install maven – maven.

c7d9494e16e67ab53501b976b91e0318.png

4Create tasks

1. Create a new task

Click [New Task], enter the task name, and click to build a free-style software project.

5640d6909d55da846ecd141a4d1b17fa.png

2. Source code management

Click [Source Code Management]-[Git], enter the warehouse address, add the certificate, and select the certificate.

e80011803494716c306b1d28aa8c2917.png
01366d5d633fcec24f7ff5873392d9bf.png

3. Build trigger

Click [Build Trigger] – [Build] – [Add Build Step] – [Call Top-Level Maven Goal] – [Fill in Configuration] – [Save].

d289d7d5d3ea2af5686f854fda04d252.png

The command here is just install to see if the jar package can be generated.

clean install -Dmaven.test.skip=true

4dc99a4c66ac79d2b979680f705211bf.png

4. Save

Click the [Save] button.

5 test

This function tests whether packaging can be performed normally.

1. Build

Click the Build button.

880eb4309efc868c29a5d21aec9c2f6a.png

2. View log

Click on the task being built, or click on the task name to enter the details page and view the console output. See if it can be successfully packaged into a jar package.

The log here may fail to download the dependent jar package for the first time. Click Build again to succeed.

e2851504ae8af968b3e1701dda69366f.png
4ea718669c36d9a21c51ca83bf8f8fcc.png
4d135330e7d2f19f65ed0805e7fe2c5c.png

3. View project location

cd /var/jenkins_home/workspace
ll # to check whether it exists

6Run the project

Because the project and Jenkins are on the same server, we use a shell script to run the project. The principle is to package the image through Dockerfile and then run it with docker.

1. Dockerfile

Create a new file named Dockerfile in the root directory of the Spring Boot project. Note that there is no suffix.

The content is as follows: (Roughly, use JDK 8, add the jar package to docker and then run the prd configuration file. You can check other tutorials for details)

FROM jdk:8
VOLUME /tmp
ADD target/zx-order-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8888ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar","--spring .profiles.active=prd"]

2. Modify Jenkins task configuration

673f0c044db6ac4c79ee9f1b305f9bda.png

The configuration is as follows:

d2ca3922d273706f2c17cf4bc135921c.png

“-t” specifies the new image name, “.” indicates that the Dockfile is in the current path.

cd /var/jenkins_home/workspace/zx-order-api
docker stop zx-order || true
docker rm zx-order || true
docker rmi zx-order || true
docker build -t zx-order .
docker run -d -p 8888:8888 --name zx-order zx-order:latest

Remark:

  1. The docker logs -f is used in the above picture to facilitate reading the logs. Do not use it in the real environment, because it will keep waiting for the logs and the build task will fail;

  2. Adding “|| true” means that if the command execution fails, it will continue to be executed, in order to prevent an error being reported for the first time without the mirror;

  3. Save: Click to save;

  4. Build: Check the Jenkins console output. The output is as follows, which proves success;

8082a6ecd7f317a4a5c9947851dcf1b8.png

5. Verification

docker ps # Check if you have your own container
docker logs # Own container name, check whether the log is correct
# Open the browser to access the project

Source: blog.csdn.net/zqqiang0307/

article/details/120458586

Back-end exclusive technology group

To build a high-quality technical exchange community, HR personnel engaged in programming development and technical recruitment are welcome to join the group. Everyone is also welcome to share their own company’s internal information, help each other, and make progress together!

Speech in a civilized manner, focusing on communication technology, recommendation of positions, and industry discussion

Advertisers are not allowed to enter, and do not trust private messages to prevent being deceived.

ca448ddeac8ffb2e853ef80a58467b3a.png

Add me as a friend and bring you into the group
The knowledge points of the article match the official knowledge archives, and you can further learn relevant knowledge. Cloud native entry-level skills treeContinuous integration and deployment (Jenkins)Use helm to install Jenkins16860 people are learning the system