Jenkins+Docker realizes one-click automatic deployment project

  • 1. Install docker

  • 2. Install Jenkins

  • 3. System configuration

  • 4. Create a task

  • 5. Test

  • Six, run the project

hevc?url=https://mmbiz.qpic.cn/mmbiz_jpg/6mychickmupU5q99GKljESgwnq2MVMMM4GGmecOes116sMWlmWK9qSyhY5cAfqnUf2TVVHdSzKxBc6l9b53QLNQ/640?wx_fmt=j peg&tp=wxpic&wxfrom=5&wx_lazy=1&wx_co=1 &type=jpg

This article implements the simplest and most comprehensive Jenkins + docker + springboot one-click automatic deployment project, with complete steps and less pitfalls.

Environment: centos7 + git(gitee)

Briefly describe the implementation steps: install jenkins on docker, configure jenkins basic information, and use Dockerfile and shell scripts to automatically pull, package and run the project.

1. Install docker

docker install community version CE

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

yum update

2. Uninstall the old version (if you have installed the old version)

yum remove docker docker-common docker-selinux docker-engine

3. Install the required packages

yum install -y yum-utils device-mapper-persistent-data lvm2

4. Set yum source

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

5. Install docker

yum install docker-ce #Because only the stable warehouse is enabled by default in the repo, the latest stable version 17.12.0 is installed here
yum install <your own version> # For example: sudo yum install docker-ce-17.12.0.ce

6. Start and boot

systemctl start docker
systemctl enable docker

7. Verify that the installation was successful

docker version

Background management system + user applet based on Spring Boot + MyBatis Plus + Vue & amp; Element, supports RBAC dynamic permissions, multi-tenancy, data permissions, workflow, three-party login, payment, SMS, mall and other functions

  • Project address: https://github.com/YunaiV/ruoyi-vue-pro

  • Video tutorial: https://doc.iocoder.cn/video/

2. Install Jenkins

Jenkins Chinese official website:

https://www.jenkins.io/zh/

1. Install Jenkins

Everything is so simple to install docker, pay attention to check whether the 8080 is already occupied! Modify port if occupied

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

After the installation is complete, access the address -> http://{IP of the service where Jenkins is deployed}:8080

There will be a few minutes of waiting here.

2. Initialize Jenkins

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

hevc?url=https://mmbiz.qpic.cn/mmbiz_png/6mychickmupU5q99GKljESgwnq2MVMMM4CZ5HrIXL8LnLWQbjfcuA82pw6jMw7yJLBkr07BYNhd5Uf87UrJtzhg/640?wx_fmt= png&tp=wxpic&wxfrom=5&wx_lazy=1&wx_co=1 &type=png

2.2 Install the plugin

Choose the first one: Install Recommended Plugins

hevc?url=https://mmbiz.qpic.cn/mmbiz_png/6mychickmupU5q99GKljESgwnq2MVMMM4WAibaUwcoMaCaOU4G3LD91Zfdyzvc9ianlIciaqCFMbVxelydYoYPM5yQ/640?wx_fmt=png&tp=w xpic&wxfrom=5&wx_lazy=1&wx_co=1 & amp;type=png

2.3 Create an administrator user

Remember this account

Background management system + user applet based on Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & amp; Element, supporting RBAC dynamic permissions, multi-tenancy, data permissions, workflow, three-party login, payment, SMS, mall and other functions

  • Project address: https://github.com/YunaiV/yudao-cloud

  • Video tutorial: https://doc.iocoder.cn/video/

3. System Configuration

1. Plug-ins are required for installation

Enter [Home] – [System Management] – [Plugin Management] – [Optional Plugins]

Search for the following plug-ins that need to be installed, and click Install.

hevc?url=https://mmbiz.qpic.cn/mmbiz_png/6mychickmupU5q99GKljESgwnq2MVMMM4BmQMMdoF4PmqIHFOicznxftWRLS6Pzia0qcNZ4pH62YqJ4Wib95zibYEzQ/640?wx_fmt=png&t p=wxpic&wxfrom=5&wx_lazy=1&wx_co=1 & amp;type=png

  • Install Maven Integration

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

  • If you use Gitee code cloud, install the plugin Gitee (Git comes with no installation)

2. Configure Maven

Enter [Home] – [System Management] – [Global Configuration], pull to the bottom maven – maven installation

4. Create task

1. Create a new task

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

hevc?url=https://mmbiz.qpic.cn/mmbiz_png/6mychickmupU5q99GKljESgwnq2MVMMM45c6f2iavYiaUh83co1e8QTXztrwTicjhNcEj2wib9WuPgiahkwcOSHbLKxg/640?wx_fmt=png& tp=wxpic&wxfrom=5&wx_lazy=1&wx_co=1 & amp;type=png

2. Source code management

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

hevc?url=https://mmbiz.qpic.cn/mmbiz_png/6mychickmupU5q99GKljESgwnq2MVMMM4RITZ5Pwfq6w5RiaUSicsppCg16DGQZBm66GTrJ6jlxtBZgkD0xzbjjWQ/640?wx_fmt=png& tp=wxpic&wxfrom=5&wx_lazy=1&wx_co=1 & amp;type=png

hevc?url=https://mmbiz.qpic.cn/mmbiz_png/6mychickmupU5q99GKljESgwnq2MVMMM4JWtdQJgQ3fpcdAQzj99D5f8YHkBXkRgrJlE4JRLCyoEQYKJvuTH99A/640?wx_fmt=png &tp=wxpic&wxfrom=5&wx_lazy=1&wx_co=1 & amp;type=png

3. Build triggers

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

hevc?url=https://mmbiz.qpic.cn/mmbiz_png/6mychickmupU5q99GKljESgwnq2MVMMM42bqgQfcsZp62ricE1aGgB1CbtEGiaEE6jN6ylbjCJLhVDkVtte0icc5EA/640?wx_fmt=png&tp =wxpic&wxfrom=5&wx_lazy=1&wx_co=1 & amp;type=png

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

clean install -Dmaven.test.skip=true

hevc?url=https://mmbiz.qpic.cn/mmbiz_png/6mychickmupU5q99GKljESgwnq2MVMMM4AL6ULhZlicyn1uvWOHgEe0qiahTAVAduOWRbiaV4Vyibd6ZKDEZXNWlgKQ/640?wx_fmt=png&tp=wx pic&wxfrom=5&wx_lazy=1&wx_co=1 & amp;type=png

4. save

Click the [Save] button

5. Test

Whether the functional test can be packaged normally

1. build

Click the build button

hevc?url=https://mmbiz.qpic.cn/mmbiz_png/6mychickmupU5q99GKljESgwnq2MVMMM4khw2chfZquHPFtE7qc7GvpTnicXXuLto9QAnrMfcic25DpBGnvgao33g/640?wx_fmt=png&t p=wxpic&wxfrom=5&wx_lazy=1&wx_co=1 & amp;type=png

2. Check the log

Click the task being built, or click the task name to enter the details page, and check the console output to 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, and click build again to succeed.

3. View project location

  1. cd /var/jenkins_home/workspace

  2. ll to see if it exists

6. Run the project

Because our 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 the dockerfile, and then run it with docker.

1. Dockerfile

Create a new file named Dockerfile in the root directory of the springboot project. Note that there is no suffix. Its content is as follows: (roughly use jdk8, add the jar package to docker and run the prd configuration file. For details, please refer to other tutorials)

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

2. Modify the jenkins task configuration

The configuration is as follows:

hevc?url=https://mmbiz.qpic.cn/mmbiz_png/6mychickmupU5q99GKljESgwnq2MVMMM4dD32WM6kP51hKwv1S7q7L8RB066ltrFQ1Q3LO7wKUAL7f2r413Xgqg/640?wx_fmt= png&tp=wxpic&wxfrom=5&wx_lazy=1&wx_co=1 &type=png

  • -t: Specify 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. I used docker logs -f in the picture above for the convenience of viewing the logs. I really don’t want to use it, 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 execute, in order to prevent the first error report without the mirror

3. save

Click to save

4. build

Check the jenkins console output, the output is as follows, which proves success!

hevc?url=https://mmbiz.qpic.cn/mmbiz_png/6mychickmupU5q99GKljESgwnq2MVMMM4bL0hV8bTKJokE7PaDVqX6qiba2ap9f70XH8k4yhhtrvw8HyQrPscgRw/640?wx_fmt=png& tp=wxpic&wxfrom=5&wx_lazy=1&wx_co=1 & amp;type=png

5. Verification

  1. docker ps Check if you have your own container

  2. docker logs own container name to check if the log is correct

  3. Browser access project try