Jenkins remote SSH deployment SpringBoot project

1. Pre-environment

Pre-environment configuration: jdk, maven, git

2. Configure git credentials in Jenkins

Please view previous articles:
https://blog.csdn.net/RookiexiaoMu_a/article/details/122655272?spm=1001.2014.3001.5501

3. Install the Publish over SSH plug-in

Insert image description here

4. Configure SSH Servers

After installing the Publish over SSH plug-in, enter Manager Jenkins—>Configure System
Insert image description here
Scroll down to the end and find SSH Servers:
Insert image description here
Complete Then click: Advanced
Insert image description here
Insert image description here
Use the default configuration for other settings, and click Apply to save.

5. Create a new free style project

For those who don’t know how to create a new one, please check the previous articles:
https://blog.csdn.net/RookiexiaoMu_a/article/details/122655272?spm=1001.2014.3001.5501
Insert image description here
5.1 Fill in the configuration:
Insert image description here
Insert image description here

5.2 Add build steps:

Insert image description here
Insert image description here
Enter shell command

pwd
if [ -e "*.jar" ]; then 
    rm -rf *.jar
fi
cp /var/lib/jenkins/workspace/test-parent-package/test-wechatpush/target/*.jar test-wechatpush.jar

This step is still executed on our jenkins machine. The meaning of the command is:
1. Output the current path (each new project on jenkins will be created in the jenkins working directory A folder)
For example, the output of pwd at this time is:/var/lib/jenkins/workspace/test-wechatpush
2. Change the jar in the current path Delete all packages
3. Copy the target jar package in the jenkins working path to the current path

5.3 Add post-build operations:

Insert image description here
Insert image description here
Insert image description here
This step will copy the jar file of Source files to the Remote directory

Remote directory: remote directory
Exec command: command to be executed on the remote machine

5.4 Command to be executed on the remote machine: Exec command

source /etc/profile
cd /db/app/test-parent
if [ -e "/db/app/test-parent/test-wechatpush/*.jar"]; then 
    rm /db/app/test-parent/test-wechatpush/test-wechatpush.jar
fi
cp test-wechatpush.jar /db/app/test-parent/test-wechatpush/test-wechatpush.jar
cd /db/app/test-parent/test-wechatpush/
api_id=`ps -ef | grep 8080 | grep -v "grep" | grep "test-wechatpush" | awk '{print $2}'`
echo $api_id
for id in $api_id
do
 kill -9 $id
 echo "killed $id"
done
BUILD_ID=dontKillMe
nohup java -Xms64m -Xmx128m -jar /db/app/test-parent/test-wechatpush/test-wechatpush.jar --name=test-wechatpush --spring.profiles.active=dev --server.port=8080 > /dev/null 2>&1 &

The first line: Refresh the configuration
The second line: Switch directory
The third, fourth and fifth lines: Remove test- on the remote directory wechatpush.jar file
Line 6: Copy the test-wechatpush.jar file in the Remote Directory (step 5.3) directory to /db/app/test-parent/test-wechatpush on the remote machine Under the directory
Line 7: Switch directory
Lines 8 to 15: Kill the process with port 8080 (8080 is us) < a i=6> Line 16: If you use nohup in the shell and find that it still cannot run in the background, just add this command Line 17: Start the java project command

Guess you like

Origin blog.csdn.net/RookiexiaoMu_a/article/details/128589691