Operations 8 min read

Automated Build, Deployment, and Service Startup with Jenkins and SaltStack for a Spring Boot Application

This guide explains how to package a Spring Boot service, publish the artifact and startup script to remote hosts, and remotely start the service using Jenkins pipelines and SaltStack commands, covering preparation, Jenkins project setup, Jenkinsfile logic, and deployment logs.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Automated Build, Deployment, and Service Startup with Jenkins and SaltStack for a Spring Boot Application

Overall approach: package the application, copy the artifact and startup script to target hosts, and start the service remotely via SaltStack.

Preparation: the demo repository is https://github.com/zeyangli/springboot-helloworld.git.

Jenkins project configuration includes parameters such as serviceName, buildShell, targetHosts, targetDir, user, and port.

Jenkinsfile defines variables, checks out code, builds with Maven, archives the JAR and service script, and uses SaltStack commands to deploy and manage the service on remote hosts.

String buildShell = "${env.buildShell}"
String targetHosts = "${env.targetHosts}"
String targetDir = "${env.targetDir}"
String serviceName = "${env.serviceName}"
String user = "${env.user}"
String port = "${env.port}"
def jarName
node("master"){
//检出代码
stage("checkout"){
checkout scm
}
//执行构建
stage("build"){
def mvnHome = tool 'M3'
sh " ${mvnHome}/bin/mvn ${buildShell} "
jarName = sh returnStdout: true, script: "cd target && ls *.jar"
jarName = jarName - "\n"
//归档制品
sh "mkdir -p /srv/salt/${serviceName} && mv  service.sh target/${jarName} /srv/salt/${serviceName} "
}
//发布应用
stage("deploy"){
sh " salt ${targetHosts} cmd.run ' rm -fr  ${targetDir}/*.jar '"
sh " salt ${targetHosts} cp.get_file salt://${serviceName}/${jarName}  ${targetDir}/${jarName} mkdirs=True"
sh " salt ${targetHosts} cp.get_file salt://${serviceName}/service.sh  ${targetDir}/service.sh mkdirs=True"
sh " salt ${targetHosts} cmd.run 'chown ${user}:${user} ${targetDir} -R '"
sh " salt ${targetHosts} cmd.run 'su - ${user} -c \"cd ${targetDir} &&  sh service.sh stop\" ' "
sh " salt ${targetHosts} cmd.run 'su - ${user} -c \"cd ${targetDir} &&  sh service.sh start ${jarName} ${port} ${targetDir}\" ' "
}
}

The service startup script (service.sh) provides start, stop, and restart functions for the Java application.

#!/bin/bash
app=$2
port=$3
targetDir=$4
start(){
cd ${targetDir}
nohup java -jar ${app} --server.port=${port} >>/dev/null 2>&1& echo $! > service.pid
cd -
}
stop(){
pid=`cat service.pid`
if [ -z $pid ]
then
echo "pid"
else
kill -9 ${pid}
kill -9 ${pid}
kill -9 ${pid}
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
;;
*)
echo "[start|stop|restart]"
;;
esac

Build log shows Maven scanning, compilation, packaging, and successful creation of the JAR file, followed by the SaltStack deployment steps.

Pipeline execution logs illustrate the checkout, build, and deploy stages, including SaltStack commands that copy files, set ownership, and invoke the service script on the target host, culminating in a successful run.

ci/cdautomationdeploymentSpring BootJenkinsSaltStack
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

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.