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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
| pipeline { agent any // 1. 定义构建参数 parameters { string( name: 'DEPLOY_BRANCH', defaultValue: 'master', description: '请输入要部署的分支名称' ) } // 2. 环境变量配置 environment { // ====== 需要修改的配置 ====== PROJECT_DIR = "C:\\Users\\Administrator\\.jenkins\\workspace\\metactivity" // 项目代码目录 VERSION_DIR = "C:\\Users\\Administrator\\.jenkins\\workspace\\metactivity\\src\\main\\resources\\static" // 项目代码目录 SERVICE_NAME = "metactivity-server" // Windows服务名称 MAVEN_HOME = "D:\\tools\\apache-maven-3.9.8" // Maven安装路径 DEPLOY_DIR = "D:\\app\\metactivity_9094" // 部署目录 EMAIL_TO = "ren_zhen_yu@163.com" // 通知邮箱 // =========================== // 固定配置 MAVEN_OPTS = "-Dmaven.repo.local=C:\\Users\\Administrator\\.m2\\repository -Dfile.encoding=UTF-8" BUILD_OPTS = "-DskipTests=true" // 跳过测试,使用多线程构建 BUILD_LOG = "build_${BUILD_NUMBER}.log" JAR_NAME = "metactivity.jar" // 重命名后的jar文件名 JAVA_TOOL_OPTIONS = "-Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8" CONSOLE_ENCODING = "UTF-8" } // 3. 构建阶段 stages { // 阶段1: 准备环境 stage('环境准备') { steps { script { // 设置控制台编码为 UTF-8 bat "chcp 65001" // 清理工作空间 cleanWs() // 创建日志文件 bat "echo 构建开始时间: %DATE% %TIME% > ${env.BUILD_LOG}" // 打印环境信息 bat "echo 构建分支: ${params.DEPLOY_BRANCH} >> ${env.BUILD_LOG}" bat "echo 项目目录: ${env.PROJECT_DIR} >> ${env.BUILD_LOG}" bat "echo Maven路径: ${env.MAVEN_HOME} >> ${env.BUILD_LOG}" } } } // 阶段2: 拉取代码 stage('拉取代码') { steps { script { // 设置控制台编码为 UTF-8 bat "chcp 65001" bat """ cd /d ${env.PROJECT_DIR} echo [INFO] 清理工作区... >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} git reset --hard HEAD >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} 2>&1 git clean -f -d >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} 2>&1 git fetch --all >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} 2>&1 echo [INFO] 切换到分支 ${params.DEPLOY_BRANCH}... >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} git checkout -B ${params.DEPLOY_BRANCH} origin/${params.DEPLOY_BRANCH} --force >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} 2>&1 git pull origin ${params.DEPLOY_BRANCH} >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} 2>&1 echo [SUCCESS] 代码拉取完成! >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} echo 当前分支: && git branch >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} """ } } } // 阶段3: Maven构建 stage('Maven构建') { steps { script { // 设置控制台编码为 UTF-8 bat "chcp 65001" bat """ cd /d ${env.VERSION_DIR} :: 使用 Python 获取格式化的时间 (YYYY-MM-DD_HH:mm:ss) echo import time > get_time.py echo with open("version.html", "w", encoding="utf-8") as f: >> get_time.py echo f.write("Version:" + time.strftime("%%Y-%%m-%%d_%%H:%%M:%%S")) >> get_time.py :: 执行 Python 脚本 python get_time.py :: 记录生成的版本号 type version.html >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} :: 清理临时文件 del get_time.pyrsion.html type version.html >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} echo [INFO] 生成版本号... >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} cd /d ${env.PROJECT_DIR} set PATH=${env.MAVEN_HOME}\\bin;%PATH% set MAVEN_OPTS=${env.MAVEN_OPTS} echo [INFO] 开始Maven构建... >> ${env.BUILD_LOG} mvn clean package -Dmaven.repo.local=C:\\Users\\Administrator\\.m2\\repository ${env.BUILD_OPTS} echo [SUCCESS] Maven构建完成! """ } } } // 阶段4: 部署服务 stage('部署服务') { steps { script { // 设置控制台编码为 UTF-8 bat "chcp 65001" // 停止服务 powershell """ try { echo '[INFO] 停止服务 ${env.SERVICE_NAME}...' >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} Stop-Service -Name '${env.SERVICE_NAME}' -Force -ErrorAction Stop echo '[SUCCESS] 服务 ${env.SERVICE_NAME} 已停止' >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} } catch { echo '[WARNING] 停止服务失败或服务未运行: ${env.SERVICE_NAME}' >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} } """ // 部署新版本(包含重命名步骤) bat """ cd /d ${env.PROJECT_DIR} echo [INFO] 部署新版本... >> ${env.BUILD_LOG} :: 查找并重命名jar文件 echo [INFO] 查找并重命名jar文件... >> ${env.BUILD_LOG} set JAR_FOUND=false for /f "delims=" %%i in ('dir /b target\\*.jar') do ( set ORIGINAL_JAR=%%i set JAR_FOUND=true echo [INFO] 找到jar文件: %%i >> ${env.BUILD_LOG} echo [INFO] 重命名为 ${env.JAR_NAME} >> ${env.BUILD_LOG} copy "target\\%%i" "target\\${env.JAR_NAME}" >> ${env.BUILD_LOG} 2>&1 goto :jar_renamed ) :jar_renamed if "%JAR_FOUND%"=="false" ( echo [ERROR] 未找到jar文件! >> ${env.BUILD_LOG} exit 1 ) :: 复制重命名后的jar文件到部署目录 echo [INFO] 复制jar文件到部署目录... >> ${env.BUILD_LOG} xcopy /Y "target\\${env.JAR_NAME}" "${env.DEPLOY_DIR}\\" >> ${env.BUILD_LOG} 2>&1 echo [SUCCESS] 部署完成! >> ${env.BUILD_LOG} """ // 启动服务 powershell """ try { echo '[INFO] 启动服务 ${env.SERVICE_NAME}...' >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} Start-Service -Name '${env.SERVICE_NAME}' -ErrorAction Stop echo '[SUCCESS] 服务 ${env.SERVICE_NAME} 已启动' >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} } catch { echo '[ERROR] 启动服务失败: ${env.SERVICE_NAME}' >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} error "服务启动失败" } """ // 检查服务状态 powershell """ echo '[INFO] 服务状态检查:' >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} Get-Service -Name '${env.SERVICE_NAME}' | Select-Object Name, Status >> ${env.PROJECT_DIR}\\${env.BUILD_LOG} """ } } } } // 4. 构建后处理 post { always { // 归档日志文件 archiveArtifacts artifacts: "${env.BUILD_LOG}", allowEmptyArchive: true // 发送邮件通知(添加异常捕获) script { // 记录当前环境变量 echo "=== 环境变量 ===" echo "JOB_NAME: ${env.JOB_NAME}" echo "BUILD_NUMBER: ${env.BUILD_NUMBER}" echo "BUILD_URL: ${env.BUILD_URL}" echo "EMAIL_TO: ${env.EMAIL_TO}" echo "currentBuild.currentResult: ${currentBuild.currentResult}" // 获取构建触发者信息 def buildUserName = currentBuild.getBuildCauses()[0].userName if (buildUserName == null) { buildUserName = "" } try { mail ( to: "${env.EMAIL_TO}", subject: "构建通知: ${currentBuild.currentResult} - ${env.JOB_NAME}", body: """ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> body { font-family: Arial, sans-serif; font-size: 16px; } h1 { font-size: 24px; color: #2c3e50; } .info { margin: 10px 0; } .label { font-weight: bold; color: #34495e; } .status-success { color: #27ae60; font-weight: bold; } a { color: #3498db; } </style> </head> <body> <h1>构建通知</h1> <div class="info"><span class="label">项目:</span> ${env.JOB_NAME}</div> <div class="info"><span class="label">构建号:</span> ${env.BUILD_NUMBER}</div> <div class="info"><span class="label">操作员:</span> ${buildUserName}</div> <div class="info"><span class="label">状态:</span> <span class="status-success">${currentBuild.currentResult}</span></div> <div class="info"><span class="label">分支:</span> <span class="status-success">${params.DEPLOY_BRANCH}</span></div> <div class="info"><span class="label">持续时间:</span> ${currentBuild.durationString}</div> <div class="info"><span class="label">控制台日志:</span> <a href="${env.BUILD_URL}console">查看详情</a></div> <div class="info"><span class="label">构建日志:</span> <a href="${env.BUILD_URL}artifact/${env.BUILD_LOG}">下载日志</a></div> <div class="info"><span class="label">服务名称:</span> ${env.SERVICE_NAME}</div> <div class="info"><span class="label">部署目录:</span> ${env.DEPLOY_DIR}</div> </body> </html> """, mimeType: "text/html" ) echo "内置 mail 步骤执行成功" } catch (Exception e) { echo "内置 mail 步骤失败: ${e.toString()}" } } // 清理工作空间 cleanWs() } success { echo "✅ 流水线执行成功!服务 ${env.SERVICE_NAME} 已部署并启动" } failure { echo "❌ 流水线执行失败!请检查日志" } } }
// 获取日志摘要函数 def getLogSummary() { def summary = "" try { // 使用 currentBuild.getLog() 替代 rawBuild def logLines = currentBuild.getLog(1000) def log = logLines.join('\n') // 提取关键信息 def stages = ["环境准备", "拉取代码", "Maven构建", "部署服务"] stages.each { stage -> def stageLog = log.find(/(?s)\[${stage}\].*?(?=\[下一阶段|$)/) if (stageLog) { summary += "===== ${stage} =====\n" // 提取关键步骤 def keywords = ["INFO]", "SUCCESS]", "WARNING]", "ERROR]"] def lines = stageLog.split('\n') lines.each { line -> if (keywords.any { line.contains(it) }) { summary += line + "\n" } } summary += "\n" } } } catch (Exception e) { summary = "获取日志摘要失败: ${e.toString()}" } return summary ?: "未获取到日志摘要" }
|