pipeline { agent any environment { DEPLOY_HOST = '49.51.46.148' // 目标机 IP 或域名 DEPLOY_DIR = '/data/tengine/html/lessie_official' // 目标机部署目录 } stages { stage('Checkout 代码') { steps { git branch: 'dev', credentialsId: 'fly_gitlab_auth', url: 'http://172.24.16.20/web/scalelink-frontend.git' } } stage('Install & Build') { steps { sh """ cd ${WORKSPACE}/projects/lessie' && npm install --frozen-lockfile && npm run build """ } } stage('同步文件') { steps { // 打包必要文件:.output、package.json、pnpm-lock.yaml # sh ''' # rm -rf deploy.tar.gz # tar czf deploy.tar.gz \ # .output package.json pnpm-lock.yaml # ''' # archiveArtifacts artifacts: 'deploy.tar.gz' } } stage('Deploy to Target') { steps { sshagent([SSH_CRED]) { // 1) 传输压缩包 sh """ scp deploy.tar.gz ${DEPLOY_USER}@${DEPLOY_HOST}:/tmp/ """ // 2) 解压、安装依赖、重启 sh """ ssh ${DEPLOY_USER}@${DEPLOY_HOST} ' mkdir -p ${DEPLOY_DIR} && tar xzf /tmp/deploy.tar.gz -C ${DEPLOY_DIR} && cd ${DEPLOY_DIR} && # 安装生产依赖 npm install --production && # 重启 pm2 服务 pm2 reload nuxt-app || pm2 start .output/server/index.mjs --name nuxt-app ' """ } } } } post { success { echo '部署成功 🎉' } failure { echo '部署失败,请检查日志 ❌' } } }