SpringBoot integrates Jenkins to realize automated deployment (centos7&windows, covering scripts that prevent jenkins from killing

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

enter description here
enter description here
enter description here

Three, start

service jenkins start
service jenkins stop
service jenkins restart

4. Enter "ip+port" in the browser to configure jenkins

  1. Home
    enter description here
  2. Copy the prompt path to find the initial password
//复制密码输入
cat /var/lib/jenkins/secrets/initialAdminPassword

enter description here
3. Directly recommend installation, wait...
enter description here
4. Create user
enter description here

  • Successfully installed
    enter description here

Five, Jenkins maven, jdk environment configuration

  1. Configure jdk and maven in Manage Jenkins
    enter description here
  • JDK (cancel automatic installation and fill in your own path)
    enter description here
  • MAVEN (Cancel automatic installation and fill in your own path)
    enter description here
  1. Install the maven plugin and restart after installation
    enter description here
    enter description here

6. New spring boot project

  1. Create a new spring boot demo.
    enter description here
  2. 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";
   }
}

enter description here
3. Upload to the git repository, what I upload here is gitee.
enter description here

Seven, create a task and run

  1. New task
    enter description here
  2. Enter the project name and select the maven project
    enter description here
  3. General
    enter description here
  4. Source code management, configure git and fill in the git account password.
    enter description here
    enter description here
  5. Build trigger
    enter description here
  6. 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

enter description here
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

enter description here
8. Save and build the project

  • Build the project
    enter description here
  • Click on the build history to see the console output
    enter description here
  1. Visit the project and deploy successfully
    enter description here

  • Additional content:

Windows version installation

  • The steps are basically similar to centos, but there are a few precautions;
  1. download link
    enter description here
  2. The process of installation, startup, and password modification is omitted. Similar to centos.
  3. Note 1: Configure the shell path to the sh.exe file path in the git directory on the server.
    enter description here
    enter description here
  4. Note 2: When adding a new project, post steps need to be set to batch of windows
    enter description here
  5. 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%

Guess you like

Origin blog.csdn.net/qq_39231769/article/details/109358406