One‑Click Deployment of Branch Code to a Remote Server Using Jenkins
This article provides a step‑by‑step guide on installing Jenkins, configuring global tools, creating a freestyle project, pulling code from GitLab, building a Java JAR, backing up existing JARs, copying the new package to a remote server, and performing a rolling Docker Swarm update, while also addressing common SSL and SSH issues.
Background : A new project lacked an automated deployment pipeline, so a Jenkins‑based solution was built to pull a GitLab branch, build a Java JAR, back up the existing JAR on the target server, copy the new JAR, and restart Docker services.
Jenkins Installation : Download Jenkins from https://www.jenkins.io/download/ and follow the official installation guide at https://www.jenkins.io/doc/book/installing/. After installation, open the Jenkins dashboard.
Global Tool Configuration : In Manage Jenkins → Global Tool Configuration , add the Java JDK and Maven installations so Jenkins can compile the project.
Project Creation : Create a Freestyle Project named (e.g., passjava‑dev). Configure the source code management section with the GitLab repository URL, username, and password.
Build Steps :
Execute shell:
echo "开始打包"
mvn clean package
echo "打包完成"Running the build (click Build Now ) pulls the code, runs mvn clean package, and produces a JAR located at C:\ProgramData\Jenkins.jenkins\workspace\passjava‑dev. The console output shows the build duration (e.g., 2 min 33 s) and a Finished: SUCCESS status.
Publish Over SSH Plugin : Install the “Publish Over SSH” plugin via Manage Jenkins → Plugin Manager → Available . Configure a global SSH server with hostname, username, password/key, and remote directory.
Backup Existing JAR on the remote server:
Create a bak directory.
Create a timestamped sub‑directory inside bak.
Rename the current JAR to original‑timestamp.jar.
Copy New JAR to the remote server using the SSH plugin, specifying the workspace JAR path (e.g.,
C:\ProgramData\Jenkins.jenkins\workspace\<project>\target\app.jar) and the remote target directory.
Rolling Update with Docker Swarm :
On the remote server, a restart.sh script is executed via SSH:
echo "部署 jar 包"
cd /nfs-data/wukong/
nohup sudo sh restart.sh
exitThe restart.sh script forces updates of Docker services:
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.txtProblems Encountered and Solutions :
Git SSL certificate error : Disable SSL verification with
git config --system http.sslVerify false
git config --global http.sslVerify falseSSH timeout : Increase the timeout setting in the SSH plugin configuration.
Permission issues : Configure sudoers on the remote host so the Jenkins user can run Docker commands without a password.
After addressing these issues, the pipeline completes with Finished: SUCCESS, confirming that the JAR is built, transferred, and the Docker services are updated automatically.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
