Jenkins + Docker + Maven + Windows one-click deployment of Spring Boot programs to remote Linux servers
Article directory
1. Preparation
1. Environment
- Native: Windows 10;
- Local: Jenkins + Publish Over SSH plugin;
- local: Maven;
- Remote: Linux;
- remote: docker;
2. Basic process
preparation steps
- Build the image using Dockerfile;
The basic idea
-
The first step: use
mvn clean package -DskipTests
to package the Spring Boot program as a jar package; -
Step 2: Use the Windows command to copy the jar package to the jenkins project working directory;
-
Step 3: Use the Publish Over SSH plug-in to send the jar package to the remote Linux
/home/zibo/docker/video/app
directory; -
Step 4: Use the Publish Over SSH plug-in to execute
docker restart video-app
and restart the video-app container;The image needs to be built. If it is the first time to start the container, you need to start the container first. Refer to the relevant commands below.
3. Related commands
### 镜像方式启动
# 构建镜像
docker build -t video-app-image .
# 删除镜像
docker rmi video-app-image
# 启动容器
docker run -itd -p 8081:8080 -v /home/zibo/docker/video/app:/app --name video-app video-app-image
# 重启容器
docker restart video-app
# 删除容器
docker rm video-app
# 查看实时日志
docker logs -f video-app
# 进入容器内部
docker exec -it video-app /bin/bash
# 离开容器
exit
4. Dockerfile
# 该镜像需要依赖的基础镜像
FROM openjdk:17-jdk
# 创建挂载点
VOLUME /app
# 定义工作目录为/app,命令在此目录下执行
WORKDIR /app
# 指定docker容器启动时运行jar包,并将端口设置为8080
ENTRYPOINT ["java", "-jar", "-Dserver.port=8080", "app.jar"]
# 指定端口 8080
EXPOSE 8080
# 指定维护者的名字为zibo
LABEL maintainer="zibo"
5. Configure the remote server, install and use the Publish Over SSH plug-in
see previous article
Jenkins sends files to remote servers: Publish Over SSH plugin
https://blog.csdn.net/qq_29689343/article/details/131349578
2. Jenkins project
1. Create Freestyle project
a project
2. Construction steps
Step 1: Packing
D:
cd D:\MyFile\GitHub\perfect-video
mvn clean package -DskipTests
icon
Step Two: Copy the Files
xcopy /y D:\MyFile\GitHub\perfect-video\target\app.jar C:\ProgramData\Jenkins\.jenkins\workspace\perfect-video
icon