首先windows安装ssh服务,这里使用的是PowerShellServer 下载地址:PowerShellServer
安装配置如下:
启用scp功能,点击启动即可
jenkins新建项目,选择流水线
pipeline脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #!groovy pipeline { agent any environment { REPOSTTORY="[email protected]:xinpanshi/flowPlatform.git" MODULE="flowplatform" SCRIPT_PATH="/root/scripts" } stages { stage('获取代码') { steps { echo "start fetch code from git:${REPOSTTORY}" deleteDir() git "${REPOSTTORY}" } } stage('编译+单元测试') { steps { echo "start compile" sh "source /etc/profile" sh "/opt/src/apache-maven-3.5.4/bin/mvn -Dmaven.test.skip=true -U -am clean package" } } /**stage('构建镜像') { steps { echo "start build image" sh "${SCRIPT_PATH}/${MODULE}/Dockerfile.sh ${MODULE}" sh "${SCRIPT_PATH}/${MODULE}/build_image.sh ${MODULE}" } }**/ stage('发布系统') { steps { echo "restart deploy" sh "/root/scripts/flowplatform/restart.sh" } } } } |
jenkins机器上的shell脚本需要提前安装sshpass:
cat /root/scripts/flowplatform/restart.sh
1 2 3 | #!/bin/bash sshpass -p '123' scp /root/.jenkins/workspace/flowPlatform/target/flow-0.0.1-SNAPSHOT.jar xxx@192.168.0.192:/ sshpass -p '123' ssh xxx@192.168.0.192 "start d:\flowadmin\restart_flow.bat" |
windows上的bat重启复制项目脚本:
cat d:\flowadmin\restart_flow.bat
1 2 3 4 5 6 7 8 | @echo off taskkill /f /im java.exe ping 127.0.0.1 -n 5 cd D:\flowAdmin d: xcopy /Y D:"Program Files (x86)""PowerShell Server 2016"\sftproot\flow-0.0.1-SNAPSHOT.jar D:\flowAdmin\ java -jar flow-0.0.1-SNAPSHOT.jar exit |
完成后直接构建即可: