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
-
Make sure the yum package is updated to the latest
yum update
-
Uninstall the old version (if you installed it)
yum remove docker docker-common docker-selinux docker-engine
-
Install required packages
yum install -y yum-utils device-mapper-persistent-data lvm2
-
Set up yum source
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
-
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
-
Start and set up startup
systemctl start docker systemctl enable docker
-
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
2.2 Install plug-in
Choose the first option: Install recommended plug-ins.
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.
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.
4Create tasks
1. Create a new task
Click [New Task], enter the task name, and click to build a free-style software project.
2. Source code management
Click [Source Code Management]-[Git], enter the warehouse address, add the certificate, and select the certificate.
3. Build trigger
Click [Build Trigger] – [Build] – [Add Build Step] – [Call Top-Level Maven Goal] – [Fill in Configuration] – [Save].
The command here is just install to see if the jar package can be generated.
clean install -Dmaven.test.skip=true
4. Save
Click the [Save] button.
5 test
This function tests whether packaging can be performed normally.
1. Build
Click the Build button.
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.
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
The configuration is as follows:
“-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:
-
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;
-
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;
-
Save: Click to save;
-
Build: Check the Jenkins console output. The output is as follows, which proves success;
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
, andindustry discussion
Advertisers are not allowed to enter, and do not trust private messages to prevent being deceived.