springBoot integrates Jenkins to realize automatic deployment (centos7&windows, covering scripts that prevent jenkins from killing)
One, install Jenkins
//下载安装
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install jenkins
Two, modify the configuration
//修改配置1 找到 JENKINS_USER 和JENKINS_PORT ,修改为root和你需要的端口
vi /etc/sysconfig/jenkins
JENKINS_USER="root"
JENKINS_PORT="8001"
// 修改配置2 找到candidates后面改为你的jdk路径
vi /etc/rc.d/init.d/jenkins
Three, start
service jenkins start
service jenkins stop
service jenkins restart
4. Enter "ip+port" in the browser to configure jenkins
- Home
- Copy the prompt path to find the initial password
//复制密码输入
cat /var/lib/jenkins/secrets/initialAdminPassword
3. Directly recommend installation, wait...
4. Create user
- Successfully installed
Five, Jenkins maven, jdk environment configuration
- Configure jdk and maven in Manage Jenkins
- JDK (cancel automatic installation and fill in your own path)
- MAVEN (Cancel automatic installation and fill in your own path)
- Install the maven plugin and restart after installation
6. New spring boot project
- Create a new spring boot demo.
- Any controller
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/")
@ResponseBody
public String hello() {
return "Hello, SpringBoot 2";
}
}
3. Upload to the git repository, what I upload here is gitee.
Seven, create a task and run
- New task
- Enter the project name and select the maven project
- General
- Source code management, configure git and fill in the git account password.
- Build trigger
- You can modify your maven configuration file in Build advanced
clean install -Dmaven.test.skip -U
//如需要选择分支打包详情参照:https://blog.csdn.net/qq_39231769/article/details/109311606
clean package -P dev
7. post steps
- Add an execution shell, pay attention to modify your jar package name and ensure that the path exists
#!/bin/bash
#export BUILD_ID=dontKillMe这一句很重要,这样指定了,项目启动之后才不会被Jenkins杀掉。
export BUILD_ID=dontKillMe
#指定最后编译好的jar存放的位置
www_path=/out
#Jenkins中编译好的jar位置
jar_path=/var/lib/jenkins/workspace/demo/target
#Jenkins中编译好的jar名称
jar_name=demo-0.0.1.jar
#获取运行编译好的进程ID,便于我们在重新部署项目的时候先杀掉以前的进程
pid=$(cat /out/demo.pid)
#进入指定的编译好的jar的位置
cd ${jar_path}
#将编译好的jar复制到最后指定的位置
cp ${jar_path}/${jar_name} ${www_path}
#进入最后指定存放jar的位置
cd ${www_path}
#杀掉以前可能启动的项目进程
kill -9 ${pid}
#启动jar,指定SpringBoot后台启动
nohup java -jar ${jar_name} &
#将进程ID存入到shaw-web.pid文件中
echo $! > /out/demo.pid
8. Save and build the project
- Build the project
- Click on the build history to see the console output
- Visit the project and deploy successfully
- Additional content:
Windows version installation
- The steps are basically similar to centos, but there are a few precautions;
- download link
- The process of installation, startup, and password modification is omitted. Similar to centos.
- Note 1: Configure the shell path to the sh.exe file path in the git directory on the server.
- Note 2: When adding a new project, post steps need to be set to batch of windows
- Note 3: The difference in execution scripts
@echo off
setlocal enabledelayedexpansion
::设置端口
set PORT=8082
:: 设置生成路径
set OLD_PATH=C:\Windows\system32\config\systemprofile\AppData\Local\Jenkins\.jenkins\workspace\gtoa\target
:: 设置新的存放路径
set NEW_PATH=C:\out
:: 设置jar包名
set JAR_NAME=gtoa-0.0.1-SNAPSHOT.jar
::set /p port=Please enter the port :
for /f "tokens=1-5" %%a in ('netstat -ano ^| find ":%PORT%"') do (
if "%%e%" == "" (
set pid=%%d
) else (
set pid=%%e
)
echo !pid!
taskkill /f /pid !pid!
)
::复制文件
XCOPY %OLD_PATH%\%JAR_NAME% "%NEW_PATH%" /y
::防止启动后被杀死进程
set BUILD_ID=dontKillMe
::启动jar包
start javaw -jar %NEW_PATH%\%JAR_NAME%