Jenkins deploys Spring Boot project detailed process

Jenkins deploys Spring Boot project


To deploy Spring Boot projects through Jenkins, the deployed machine must have JDK, Git, and Maven installed.

Linux installation Git: Linux installation Git detailed steps
Linux configuration Java environment: Linux configuration Java environment

1. Introduction to Jenkins

  1. Jenkins is an open source continuous integration tool developed by JAVA.
  2. The main function of Jenkins: to provide continuous integration services for software development.
  3. The main features of Jenkins: Jenkins can be understood as a scheduling platform, with a variety of plug-ins, to complete the automation process through scheduling plug-ins.
  4. The basic working process of Jenkins: automatically pull code from Github or Gitlab, and then automatically package and deploy the project by writing shell scripts.

2. Jenkins installation and deployment

1. Download

Download the required version of the war package from the Jenkins official website: https://jenkins.io/zh/download/ (It is recommended to download LTS, that is, the long-term support version, with fewer problems)

2. Installation

Upload Jenkins to the corresponding directory of Linux and run the war package:

nohup java -jar jenkins.war --httpPort=1234 >> jenkins.log 2>&1 &

  • nohup: The project can still run in the background after closing the command window.
  • httpPort: Specifies the port on which the project runs.
  • The log output is a jenkins.logfile .

Browser input http://ip地址:1234can successfully access Jenkins:

image-20220320122706512

Follow the prompts to get the password to create an administrator account.

3. Configure the plugin to accelerate the download

Enter the plugin management interface of Jenkins:

image-20220320122552461

Scroll to the bottom of the page:

image-20220320122832122

Fill in the following address, use the Tsinghua software mirror to accelerate the download of the plug-in, and then click Submit:

https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

4. Two ways to download plugins

  1. Plug-in acceleration has been configured, just search and download directly in Jenkins.

  2. Through the Tsinghua University open source software mirror site: https://mirrors.tuna.tsinghua.edu.cn/jenkins/plugins/, download the plugin and import it into Jenkins:

    image-20220320123331885

3. Jenkins Chinese (optional)

  1. Download the Locale pluginplugin .

  2. Enter the system configuration page:

    image-20220320124731127
  3. Enter zh_cn in the following location and click Save:

    image-20220320124903922
  4. Restart Jenkins:http://ip地址:1234/restart

Fourth, Jenkins installation plugin

Project deployment requires the installation of two required plugins: Git pluginand Maven Integration plugin:

  • Git plugin: A plugin that supports Git.
  • Maven Integration plugin: A plugin required to build a Maven project. After installation, you can choose to build a Maven project when creating a new project.

5. Jenkins global configuration

Enter the global configuration through the following steps:

image-20220320124249049

Enter the local JDK, Git, Maven installation directory, and configure it as shown in the following figure:

image-20220320125447937

6. Jenkins new project

Select New Item and configure as follows:

image-20220320125756456

Seven, Jenkins configuration project

1. Project Description

image-20220320130023125

2. Configure Git:

image-20220320130248011

Get the token of the Github account:

  1. Go to Github settings:

    image-20220320130425220
  2. Generate Token:

    image-20220320130554573
  3. Jenkins adds credentials (click Add to configure the source code management steps of the project):

    image-20220320130725345

3. Configure the timeout period for accessing Github:

To prevent network instability and temporarily unable to successfully obtain Github code:

image-20220320131224322

4. Build phase configuration

image-20220320131420422

5. Configure the shell script to be executed after building the project

image-20220320131555150
#!/bin/bash
#输入Maven打包后的项目名称
app=xxx-0.0.1-SNAPSHOT
#项目移动的目的地址
path=/usr/xxx
echo this is app : $app

#若项目已启动,杀死旧进程
api_pid=`ps -ef | grep "$app.jar" | grep -v grep | awk '{print $2}'`
echo api_pid = $api_pid

if [ "$api_pid" != "" ]; then
        echo kill api
        kill -9 $api_pid

        echo sleep 3s
        sleep 1
        echo sleep 2s
        sleep 1
        echo sleep 1s
        sleep 1
fi

#将jar包从jenkins工作空间中移动到指定路径下
mv /root/.jenkins/workspace/项目名/target/$app.jar $path
cd $path

#防止进程被杀死
BUILD_ID=dontKillMe

#后台进程形式启动项目,日志文件为out.log
nohup java -jar $app.jar >> out.log 2>&1 &
echo $app start success
exit 0

Note: You can build by parameterization and $参数名get , thereby reducing the situation of writing the shell script to death.

6. Click Save

8. Build Now

After the configuration is complete, click Build Nowto start the automated build process:

image-20220320132333924

9. View console output

image-20220320132611702

After the Build operation is completed, the Spring Boot project is successfully deployed automatically. After that, you only need to submit the code, and then click Build Now to automatically deploy.

Guess you like

Origin blog.csdn.net/weixin_49343190/article/details/123611576