本測試用ruoyi-plus的代碼。?
1 [GitLab 自動觸發 Jenkins 構建_jenkins構建觸發器沒有build when a change is pushed to git-CSDN博客](https://blog.csdn.net/wangyiyungw/article/details/81776972)
2 [jenkins使用Send build artifacts over SSH遇到的坑-CSDN博客](https://blog.csdn.net/goxingman/article/details/129749284)
### 多層級配置ssh遠程傳送構建物:
1 在ssh配置帳號密碼時要配置一個默認的遠程目錄
2 配置Source files:jenkins工作空間后的打包項目目錄:即$SERVER_NAME后面的部分:
```
如:JAR_PATH=/var/lib/jenkins/workspace/$SERVER_NAME/ruoyi-admin/target
ruoyi-admin/target/slcsp-2supply.jar
```
3 配置目錄:目標機器的目錄:Remote directory[ ?
](http://192.168.0.243:8081/job/slcsp-2supply/configure# "Help for feature: Remote directory")
如:slcsp/slcsp-2supply
則會把jar包發送給默認遠程目錄+Remote directory
?
配置后可以看日志
```
#!/bin/sh
#進入jar所在路徑
cd /usr/local/slcsp/slcsp-2supply
#查看運行記錄,并停止進程
BUILD_ID=DONTKILLME #后臺執行
pid=$(ps -ef|grep slcsp-2supply.jar|grep -v grep | awk '{print $2}')
function stop(){
if [ -n "$pid" ]
? then
? ? ? ? echo "pid進程 :$pid"
? ? ? ? kill -9 $pid
?else
? ? echo "進程沒有啟動"
fi
}
stop
sleep 5s
#啟動運行程序
nohup /usr/local/java/jdk-17.0.11/bin/java -Xms512m -Xmx1024m -jar /usr/local/slcsp/slcsp-2supply/slcsp-2supply.jar ? --spring.profiles.active=dev /dev/null 2>&1 &
```
日志:
```
Standard Commons Logging discovery in action with spring-jcl: please remove commons-logging.jar from classpath in order to avoid potential conflicts
SSH: Connecting from host [TestServer-1]
SSH: Connecting with configuration [slcsp-dev-239] ...
SSH: Creating session: username [root], hostname [192.168.0.239], port [22]
SSH: Connecting session ...
SSH: Connected
SSH: Opening SFTP channel ...
SSH: SFTP channel open
SSH: Connecting SFTP channel ...
SSH: Connected
SSH: cd [/usr/local]
SSH: OK
SSH: cd [/usr/local]
SSH: OK
SSH: cd [slcsp/slcsp-2supply]
SSH: OK
SSH: put [slcsp-2supply.jar]
SSH: Opening exec channel ...
SSH: EXEC: channel open
SSH: EXEC: STDOUT/STDERR from command
```
問題:
jenkins遠程ssh啟動程序后日志報錯:
SSH: Disconnecting configuration [slcsp-dev-239] ... ERROR: Exception when publishing, exception message [Exec timed out or was interrupted after 120,006 ms] Build step 'Send build artifacts over SSH' changed build result to UNSTABLE Finished: UNSTABLE
解決:
方案一:延長 SSH 執行超時時間
在 Jenkins 配置中調整 SSH 步驟的超時設置:
登錄 Jenkins;
進入你的項目配置頁面;
找到 Send build artifacts over SSH 步驟;
修改 Exec command timeout (ms) 設置,例如設為 300000(5 分鐘);
默認是 120000(2 分鐘)