pipeline {
agent any
environment {
// 定义环境变量
GIT_REPO = 'https://10.30.17.166:8929/linguoliang/police-kpi-evaluate.git'
//BRANCH = 'main'
BRANCH = '1.0.0'
//TARGET_SERVER = 'your_server_ip_or_hostname'
//DEPLOY_PATH = '/path/to/deploy/directory'
//JAR_NAME = 'your-app-*.jar' // 根据实际打包名称修改
}
stages {
// 阶段 1:从 Git 仓库拉取代码
stage('[1] - Git Checkout') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: "${BRANCH}"]],
userRemoteConfigs: [[
url: "${GIT_REPO}",
//credentialsId: 'your-git-credentials-id' // Jenkins 中配置的 Git 凭证
]]
])
}
}
// 阶段 2:使用 Maven 打包
stage('[2] - Maven Build') {
steps {
sh '/usr/local/mvn/bin/mvn clean package -Dmaven.test.skip=true' // 跳过测试
}
}
// 阶段 3:部署到服务器并启动应用
stage('[3] - Transfer File to DevelopmentApps Server') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: 'DevelopmentApps', transfers: [sshTransfer(cleanRemote: false, excludes: '',
execCommand: '''
cd /data/police_kpi_evaluate
bash stop.sh
mv PoliceKpiEvaluate_*.jar police_kpi_evaluate.jar
bash start.sh
''',
execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/PoliceKpiEvaluate_*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
post {
success{
dingtalk(
robot: 'Jenkins-DingDing',
type: 'MARKDOWN',
title: "success: ${JOB_NAME}",
text: ["- 成功构建: ${JOB_NAME}! \n- 版本: ${tag} \n- 持续时间: ${currentBuild.durationString}"]
)
}
failure{
dingtalk(
robot: 'Jenkins-DingDing',
type: 'MARKDOWN',
title: "success: ${JOB_NAME}",
text: ["- 构建失败: ${JOB_NAME}! \n-版本: ${tag} \n-持续时间: ${currentBuild.durationString}"]
)
}
}
}