Operations 10 min read

How to Automate Java SpringBoot Deployment with Jenkins and Docker Swarm

This guide walks you through setting up Jenkins to pull code from GitLab, build a SpringBoot 3 Java project with Maven, back up and copy JAR files to a remote server, and perform rolling updates of Docker Swarm services, all with step‑by‑step instructions and code snippets.

macrozheng
macrozheng
macrozheng
How to Automate Java SpringBoot Deployment with Jenkins and Docker Swarm

Background

A new project lacks a complete deployment pipeline, so a custom Jenkins‑based solution is built.

Server resources and architecture diagrams are shown.

Jenkins Deployment Overview

Jenkins runs on a dedicated server with plugins for GitLab, remote command execution, and file copying.

When a job starts, Jenkins pulls code from GitLab, builds the Java project with JDK and Maven, backs up existing JARs, copies the new JAR to a fixed directory on the remote server, and restarts the service.

"This may be a useful open‑source project: a SpringBoot3 + JDK17 + Vue e‑commerce system (60K GitHub stars) containerized with Docker, supporting full order flow, products, carts, coupons, members, payments, etc."

Jenkins Simple Installation

Refer to the official Jenkins website for download and installation instructions.

<code>https://www.jenkins.io/download/</code>

Installation steps are documented on the official Jenkins site.

<code>https://www.jenkins.io/doc/book/installing/</code>

Configure Jenkins

Because the target project is a Java application, configure the JDK and Maven installations in Jenkins global tools.

Set environment variables for Java JDK and Maven.

One‑Click Deployment with Jenkins

Create a job (FreeStyle, Maven, or Pipeline). The example uses a FreeStyle project.

Pull code → compile Java project → backup server JAR → delete old JAR → copy new JAR → update service.

Create a Project

After creation, a record appears in the dashboard.

Configure Repository

Repository URL

Username and password

Configure Build

<code>echo "开始打包"
mvn clean package
echo "打包完成"
</code>

Run the Project

Click Build Now in the dashboard to start pulling code and building the project.

The console shows the build log, JAR location, and execution time (e.g., 2 min 33 s) with status Finished: SUCCESS .

Copy Installation Package

Install Plugin

Install the Publish Over SSH plugin via Jenkins Manage Plugins.

Configure Plugin

SSH Server Name

Hostname

Username (or key)

Remote Directory

Backup Server JAR

Create a

bak

directory on the remote server, then a timestamped subdirectory, and rename the existing JAR with a timestamp.

Copy JAR to Remote Server

Configure the SSH server, source JAR path (e.g.,

C:\ProgramData\Jenkins.jenkins\workspace\&lt;job&gt;\target

), remove prefix, and remote directory.

<code>C:\ProgramData\Jenkins.jenkins\workspace&lt;Jenkins 任务名&gt;&lt;编译目录&gt;
</code>

Rolling Update Services

The project uses Docker Swarm for microservice management. Jenkins triggers a remote script via SSH to force‑update services.

<code>cd /nfs-data/wukong/
nohup sudo sh restart.sh
exit
</code>

The script uses

nohup

and

sudo

to run Docker service updates for

accountservice

,

gatewayservice

, and

qmsservice

, redirecting output to log files.

Create Remote Script

<code>echo "部署 passjava 服务"
nohup sudo docker service update accountservice --force > /nfs-data/wukong/jenkins/account.txt
nohup sudo docker service update gatewayservice --force > /nfs-data/wukong/jenkins/gateway.txt
nohup sudo docker service update qmsservice --force > /nfs-data/wukong/jenkins/qms.txt
</code>

Deploy

Run the job to compile, upload JAR, and update microservices. The final console output shows Finished: SUCCESS .

Javaci/cddeploymentmavenSpringBootJenkinsDocker Swarm
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.