// ==================== ↓↓↓↓↓↓ git ↓↓↓↓↓↓ ====================
def GIT_AUTH = "xxxx"
// ==================== ↓↓↓↓↓↓ project ↓↓↓↓↓↓ ====================
def PROJECT_GIT_URL = "xxxxxxxx.git"
def PROJECT_BASE_HOME = "/xxx/xx/" // 这是远程 Linux 服务器路径,保留为 Linux 格式
node {
def JENKINS_WORKSPACE = "${WORKSPACE}"
def CURRENT_TIME = ""
def project_service_name_select = "${PROJECT_SERVICE_NAME}".split(",")
def publish_ssh_server_select = "${PUBLISH_SSH_SERVER}".split(",")
stage('初始化准备') {
echo '****************************** 初始化准备 ******************************'
// 使用 bat 获取时间(Windows)
CURRENT_TIME = bat(script: '@echo off && echo %date:~0,4%-%date:~5,2%-%date:~8,2% %time%', returnStdout: true).trim()
// 或者使用 PowerShell 更精确:
CURRENT_TIME = powershell(script: 'Get-Date -Format "yyyy-MM-dd HH:mm:ss"', returnStdout: true).trim()
echo "当前工作空间:${JENKINS_WORKSPACE}"
echo "使用分支:${GIT_BRANCH}"
echo "当前时间:${CURRENT_TIME}"
}
stage('拉取代码') {
echo '****************************** 拉取代码 ******************************'
checkout([$class: 'GitSCM', branches: [[name: "${GIT_BRANCH}"]],
userRemoteConfigs: [[credentialsId: "${GIT_AUTH}", url: "${PROJECT_GIT_URL}"]]])
bat 'dir' // Windows 下查看当前目录内容
}
stage('公共工程打包') {
echo '****************************** 公共工程打包 ******************************'
// 使用 bat 执行 mvn 命令(需确保 mvn 已加入系统 PATH,或指定完整路径如 C:\\apache-maven\\bin\\mvn.cmd)
bat "mvn clean install -Dmaven.test.skip=true"
}
stage('微服务打包&部署远程服务器') {
echo '****************************** 微服务打包 & 部署远程服务器 ******************************'
for (int i = 0; i < project_service_name_select.length; i++) {
def current_app_name = project_service_name_select[i]
def current_app_jar = ""
def current_app_parent = ""
def current_app_ops_location = ""
def current_app_ops_sh = ""
switch (current_app_name) {
case "elderly-gateway":
current_app_parent = "elderly-gateway"
current_app_ops_location = "gateway"
current_app_ops_sh = "meal-gateway"
current_app_jar = "meal-gateway.jar"
break
case "elderly-module-system-server":
current_app_parent = "elderly-module-system/elderly-module-system-server"
current_app_ops_location = "system"
current_app_ops_sh = "meal-system-server"
current_app_jar = "meal-system-server.jar"
break
case "elderly-module-infra-server":
current_app_parent = "elderly-module-infra/elderly-module-infra-server"
current_app_ops_location = "infra"
current_app_ops_sh = "meal-infra-server"
current_app_jar = "meal-infra-server.jar"
break
case "elderly-module-member-server":
current_app_parent = "elderly-module-member/elderly-module-member-server"
current_app_ops_location = "member"
current_app_ops_sh = "meal-member-server"
current_app_jar = "meal-member-server.jar"
break
case "elderly-module-care-server":
current_app_parent = "elderly-module-care/elderly-module-care-server"
current_app_ops_location = "care"
current_app_ops_sh = "meal-care-server"
current_app_jar = "meal-care-server.jar"
break
default:
error "未知的服务名称: ${current_app_name}"
}
echo "正在构建项目: jar ${current_app_jar}"
echo "正在构建项目: ${current_app_name} (路径: ${current_app_parent})"
// 构建项目
// bat "mvn -f \"${current_app_parent}/${current_app_name}\" clean install -Dmaven.test.skip=true"
def jarFile = "${current_app_parent}\\target\\${current_app_jar}"
echo "project: ${jarFile}"
// 检查 JAR 是否存在(使用 bat 判断文件是否存在)
def fileExists = fileExists(jarFile)
if (!fileExists) {
error "${current_app_name} 的 JAR 文件未找到!路径: ${jarFile}"
}
echo "找到 JAR 文件: ${jarFile}"
// 部署到每台远程服务器(通过 SSH)
for (int j = 0; j < publish_ssh_server_select.length; j++) {
def currentServerName = publish_ssh_server_select[j]
echo "开始发布到远程服务器: ${currentServerName} 项目路径:/${PROJECT_BASE_HOME}${current_app_ops_location}/"
sshPublisher(publishers: [
sshPublisherDesc(
configName: "${currentServerName}",
transfers: [
sshTransfer(
sourceFiles: jarFile.replace('\\', '/'), // 统一为 / 分隔符(Jenkins 插件更友好)
removePrefix: "${current_app_parent}/target".replace('\\', '/'),
remoteDirectory: "/${PROJECT_BASE_HOME}${current_app_ops_location}/",
//remoteDirectory: "//mnt/service/cloud/elderly-gateway/",
execCommand: """
cd ${PROJECT_BASE_HOME}/${current_app_ops_location}/ && \
source /etc/profile && \
/bin/bash ${current_app_ops_sh}.sh restart
""",
execTimeout: 120000,
flatten: false,
makeEmptyDirs: true
)
],
usePromotionTimestamp: false,
useWorkspaceInPromotion: false,
verbose: false
)
])
}
}
}
}
当前共有 条评论