pipeline { agent any // 工具配置:指定Node.js版本(需在Jenkins全局工具中预先配置) tools { nodejs "NodeJS16" } environment { // 项目核心路径配置 UNI_APP_DIR = "./java-mall-app-shop-admin" // 项目根目录 OUTPUT_DIR = "${UNI_APP_DIR}/unpackage/dist/build/web" // uniapp H5编译输出目录 DEPLOY_TARGET = "/data/nginx/www/fafamall/demo" // 本地部署目录 } stages { stage('拉取代码') { steps { checkout scm // 从代码仓库拉取最新代码 } } stage('安装依赖(含uniapp核心)') { steps { script { dir("${UNI_APP_DIR}") { echo "清理旧依赖..." sh 'rm -rf node_modules package-lock.json' sh 'npm cache clean --force' echo "设置国内镜像源加速..." sh 'npm config set registry https://registry.npmmirror.com' sh 'npm config set @dcloudio:registry https://registry.npmmirror.com' echo "安装uniapp必需依赖..." sh 'npm install @vue/cli-service@5.0.8 --save-dev' sh 'npm install @dcloudio/vue-cli-plugin-uni@latest --save-dev' sh 'npm install' // 验证核心依赖 def uniPluginExists = fileExists("${UNI_APP_DIR}/node_modules/@dcloudio/vue-cli-plugin-uni") if (!uniPluginExists) { error "uniapp核心依赖安装失败!缺少@dcloudio/vue-cli-plugin-uni" } } } } } stage('编译Uniapp项目(H5平台)') { steps { script { dir("${UNI_APP_DIR}") { echo "开始编译Uniapp项目..." sh 'npx vue-cli-service uni-build --mode production --platform h5' // 验证编译产物 if (!fileExists("${OUTPUT_DIR}")) { error "编译失败!未生成${OUTPUT_DIR}目录" } echo "Uniapp编译完成,输出路径:${OUTPUT_DIR}" } } } } stage('部署到本地目录') { steps { script { echo "开始部署到本地目录:${DEPLOY_TARGET}" // 1. 确保目标目录存在并清空 sh """ mkdir -p ${DEPLOY_TARGET} rm -rf ${DEPLOY_TARGET}/* """ // 2. 复制构建产物到目标目录 sh "cp -r ${OUTPUT_DIR}/* ${DEPLOY_TARGET}/" // 3. 验证部署结果 def fileCount = sh( script: "ls -la ${DEPLOY_TARGET} | wc -l", returnStdout: true ).trim() if (fileCount.toInteger() <= 2) { // 只包含.和..说明部署失败 error "部署失败:目标目录${DEPLOY_TARGET}为空" } echo "部署完成!文件已输出到${DEPLOY_TARGET}" } } } } post { success { echo "✅ Uniapp本地部署成功" } failure { echo "❌ Uniapp本地部署失败" } always { // 可选:清理工作空间 cleanWs() } } }