继续上一篇jenkins连接windows,我们需要在windows上可以运行window脚本,完成自动化任务。这里采用pipeline方式完成自动执行windows脚本。
pipeline
{
agent any
tools {
maven 'cloud-maven'
}
environment{
//git凭证ID
git_auth = "9d22b7d9-ca3b-47da-b3ea-ddafe84b0fb3"
//git的url地址
git_url = "https://gitee.com/flzhang/ruoyi.git"
branch = "${branch}"
//获取当前选择的项目名称
selectedProjectNames = "${app}"
//realProjectName = "${selectedProjectNames.tokenize("/")[1]}"
realProjectName = parseProjectName(selectedProjectNames)
//获取当前选择的服务器名称
selectedServers = "${servers}".split(",")
deploy_dir="C:\\temp\\ruoyi_cloud"
upload_dir="temp\\upload"
source_dir="C:\\Users\\admin\\temp\\upload"
dest_dir="C:\\temp\\upload"
}
parameters {
string(
description: """分支""",
name: 'branch',
trim: true
)
choice(name: 'app', choices:[
'ruoyi-gateway',
'ruoyi-auth',
'ruoyi-visual',
'ruoyi-modules/ruoyi-system',
'ruoyi-modules/ruoyi-gen',
'ruoyi-modules/ruoyi-job',
'ruoyi-modules/rouyi-file',
],description: '操作(请选择微服务)')
choice(name: 'select', choices:[
'部署',
'回滚',
],description: '操作(请选择操作:部署、回滚)')
extendedChoice(description: '服务器', value: '192.168.159.128,192.168.159.131,192.168.10.5,',
descriptionPropertyValue: '192.168.159.128(测试),192.168.159.131(正式),192.168.10.5(test),',
multiSelectDelimiter: ',', name: 'servers', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', visibleItemCount: 5)
string(
description: """回滚版本号""",
name: 'roll_number',
trim: true
)
}
stages {
// 传送jar到目标机器
stage('SSH上传jar包到服务器集群') {
steps{
script{
deploy()
}
}
}
}
}
def parseProjectName(name){
if(name.indexOf("/") > -1){
name=name.replace("/ruoyi",'')
}
return name
}
def deploy(){
//遍历所有服务器,分别部署
/*sh '''
ssh -p 22 admin@192.168.1.85 "cmd /c cd C:/temp&&md temp2"
'''*/
for(ip in params.servers.split(',')){
sshPublisher(
publishers: [
sshPublisherDesc(
configName: "${ip}",
transfers: [
sshTransfer(cleanRemote: false,
excludes: '',
execCommand: "cmd /c call C:\\temp\\test.bat ${source_dir} ${dest_dir}",
execTimeout: 120000,
flatten: false,
makeEmptyDirs: false,
noDefaultExcludes: false,
patternSeparator: '[, ]+',
remoteDirectory: "${upload_dir}",
remoteDirectorySDF: false,
removePrefix: "${selectedProjectNames}/target/",
sourceFiles: "${selectedProjectNames}/target/${realProjectName}.jar")
],
usePromotionTimestamp: false,
useWorkspaceInPromotion: false,
verbose: true)
])
}
}
重要的是远程登录windows后的执行命令的格式,需遵守jenkins的规则
execCommand: "cmd /c call C:\\temp\\test.bat ${source_dir} ${dest_dir}"