更新 Jenkinsfile

增加直接上传代码的部署方式
This commit is contained in:
panjunjie 2025-08-11 18:49:44 +08:00
parent 53f5cd19ee
commit c988f73da0

73
Jenkinsfile vendored
View File

@ -1,56 +1,63 @@
pipeline {
agent any
tools {
nodejs 'NodeJS22'
}
// 环境变量配置
environment {
// 抽取部署目标路径为公共变量
// 部署目标服务器路径
DEPLOY_DIR = '/data/nginx/www/fafamall/website'
// 代码仓库中预编译的dist文件夹路径
DIST_PATH = 'dist'
}
stages {
stage('拉取代码') {
stage('拉取代码含预编译dist') {
steps {
checkout scm
script {
// 从代码仓库拉取最新代码包含预编译的dist
checkout scm
// 验证dist文件夹是否存在
echo "验证 ${DIST_PATH} 文件夹是否存在..."
sh """
if [ ! -d "${DIST_PATH}" ]; then
echo "错误:未找到预编译的 ${DIST_PATH} 文件夹,请确认代码仓库中已包含编译产物!"
exit 1
fi
echo "${DIST_PATH} 文件夹验证通过"
"""
}
}
}
stage('安装依赖') {
stage('部署项目复制dist内容') {
steps {
sh '''
rm -rf node_modules package-lock.json
npm config set registry https://registry.npmmirror.com
npm install
'''
}
}
stage('构建项目') {
steps {
sh 'npm run build'
}
}
stage('部署项目') {
steps {
sh '''
# 公共部署逻辑(创建目录、清理、复制)
mkdir -p ${DEPLOY_DIR}
rm -rf ${DEPLOY_DIR}/*
cp -r dist/* ${DEPLOY_DIR}/
'''
script {
echo "开始部署到目标路径:${DEPLOY_DIR}"
sh """
# 确保部署目录存在
mkdir -p ${DEPLOY_DIR}
# 清理目标目录旧文件(保留目录本身)
rm -rf ${DEPLOY_DIR}/*
# 复制dist文件夹下的所有内容到部署目录
cp -r ${DIST_PATH}/* ${DEPLOY_DIR}/
echo "部署完成:${DIST_PATH} 内容已复制到 ${DEPLOY_DIR}"
"""
}
}
}
}
// 流水线执行结果处理
post {
success {
echo 'Vue项目部署成功.'
echo "✅ 预编译dist文件夹部署成功"
}
failure {
echo 'Vue项目部署失败.'
echo "❌ 预编译dist文件夹部署失败请查看日志排查问题"
}
}
}
}