Jenkins implements automated construction of Gradle+Java and Vue+nodejs projects (available for personal testing!)

Install Jenkins on Linux

Preconditions

Jenkins download

Download address: https://www.jenkins.io/download/Here
is running directly through the war package, just download the latest version
image.png

run

Here, xftp is used to connect remotely through Sftp and the downloaded jenkins.war package is placed under a folder in Linux.
I create a new jenkins folder under /usr/local to store jenkins.war.
Then use the nohup command to start the war package. Running in the background, log redirection is placed in the run.log file, & means running in the background

Running Jenkins in the background

# nohup英文全称 no hang up(不挂起),用于在系统后台不挂断地运行命令,退出终端不会影响程序的运行。
# 2>&1 解释:将标准错误 2 重定向到标准输出 &1 ,标准输出 &1 再被重定向输入到 run.log 文件中。
# & 让命令在后台执行,终端退出后命令仍旧执行。
nohup java -jar jenkins.war  > run.log 2>&1 &

View Jenkins initial password and fill in the box below

cat /var/lib/jenkins/secrets/initialAdminPassword

Enter the server IP address to access port 8080 to enter the following interface

(Pay attention to check whether the server firewall has opened port 8080. If you cannot access it, try opening port 8080 of the firewall)
image.png

Choose to install recommended plug-ins

image.png

Other commonly used plug-ins will be used during project deployment

image.png
image.png
image.png
image.png

Global tool configuration (path: Dashboart->System Management->Global tool configuration)

1.Maven

image.png

2.Java JDK

In Linux, you can view JAVA_HOME in the '/etc/profile' file
image.png
image.png

3.Git

image.png

Gradle

image.png

Node

image.png

First Java project

NewItem

Select Free Style->OK
image.png

General

  • Description: Fill in the purpose of your project
  • Check the parameterized build process->Git parameters (you need to install the Git Parameter Plug-In to have this option)

image.png
image.png
The effect obtained is as follows:
image.png
General can add a parameterized build process according to his own habits. This article uses git branch. Just set these up.

Source code management

image.png

Build trigger

What I use here is a script, and the authentication token is filled in casually.
image.png

Build environment

Nothing to choose, just keep it as is

Construct

  • Here I am using Gradle7.4.2: You need to install gradle on your target server and configure it in the global tool. You can directly select the version you configured here.

image.png
image.png

Post-build operations

There is something to note here. When the target deployment server is configured with a remote folder, the remote folder currently set by SSH Publishers is the server plus the currently configured address. For example: SSH Server configures the remote folder to be P0
. The remote folder under the current post-build operation is P1, then the folder deployed to the remote is P0+P1
image.png

# 进入远程需要执行的脚本的目录
cd /home/user/server/scripts
# 停止正在运行的服务
./stop.sh
# 启动服务
./start.sh

.stop.sh

#!/bin/bash
echo "Stop Procedure : Tms-0.0.1-SNAPSHOT.jar"
pid=`ps -ef |grep java|grep Tms-0.0.1-SNAPSHOT.jar|awk '{print $2}'`
echo 'old Procedure pid:'$pid
if [ -n "$pid" ]
then
kill -9 $pid
fi

.start.sh

#!/bin/bash
#export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64/bin/java
#echo ${JAVA_HOME}
echo 'Start the program : Tms-0.0.1-SNAPSHOT.jar'
chmod 777 /home/user/server
echo '-------Starting-------'
cd /home/user/server
chmod +x Tms-0.0.1-SNAPSHOT.jar
chmod +x /usr/lib/jvm/java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64/bin/java
nohup java -jar Tms-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod > run.log 2>&1 &
echo 'start success'

Build with Parameters: Start the project

Successfully completed, sprinkle flowers!
image.png

The first Vue project

Project initialization

  • Start with the steps to build the environment after creating the new Item in the Java project above. The previous steps are the same.

Build environment

image.png
image.png

Construct

image.png

#!/bin/bash
#下载工作区npm包
node -v
npm -version
#给npm命令赋予root命令执行权(不然会报没有权限的错误,npm会用一个默认的没权限nobody用户去执行)
npm install --unsafe-perm=true --allow-root
#删除dist目录下的所有文件,dist目录即为当前jenkins工作区打包后的文件。
rm -rf ./dist/*
#执行打包命令
npm run build
#删除服务器上/root/ghyExploit/vue/saltpan/目录下的所有文件/root/ghyExploit/vue/saltpan/ 为服务器项目放置位置
rm -rf /home/user/server/dist/*
#把当前构建工作区dist目录里的文件 copy 到服务器/root/ghyExploit/vue/saltpan 文件夹下
cp -rf ./dist/* /home/user/server/dist/

Startup project

Finished spreading flowers!
image.png

Guess you like

Origin blog.csdn.net/qq_42071369/article/details/126905740