diff --git a/Jenkinsfile b/Jenkinsfile index 937959a..8cd5bd9 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -65,8 +65,54 @@ pipeline { // 安装 pnpm sh 'npm install -g pnpm' - // 使用 pnpm 安装依赖,由于锁文件兼容性问题,不使用 --frozen-lockfile - sh 'pnpm install --no-frozen-lockfile' + // 检查 package.json 或 pnpm-lock.yaml 是否自上次构建以来发生了更改 + def shouldInstall = false + + // 检查是否有上次构建的哈希记录 + def packageJsonHash = '' + def pnpmLockHash = '' + + if (fileExists('.last_build_hashes')) { + def lastHashes = readFile('.last_build_hashes').split('\n') + def lastPackageJsonHash = '' + def lastPnpmLockHash = '' + + lastHashes.each { line -> + if (line.startsWith('packageJson=')) { + lastPackageJsonHash = line.split('=')[1].trim() + } else if (line.startsWith('pnpmLock=')) { + lastPnpmLockHash = line.split('=')[1].trim() + } + } + + // 计算当前文件哈希 + packageJsonHash = sh(script: 'cat package.json | md5sum | cut -d" " -f1', returnStdout: true).trim() + pnpmLockHash = sh(script: 'cat pnpm-lock.yaml | md5sum | cut -d" " -f1', returnStdout: true).trim() + + // 如果任一文件哈希与上次不同,则需要安装 + if (packageJsonHash != lastPackageJsonHash || pnpmLockHash != lastPnpmLockHash) { + shouldInstall = true + echo "Detected changes in package management files, running pnpm install" + } else { + echo "No changes detected in package management files, skipping pnpm install" + } + } else { + // 第一次构建,需要安装 + shouldInstall = true + packageJsonHash = sh(script: 'cat package.json | md5sum | cut -d" " -f1', returnStdout: true).trim() + pnpmLockHash = sh(script: 'cat pnpm-lock.yaml | md5sum | cut -d" " -f1', returnStdout: true).trim() + echo "First build, running pnpm install" + } + + if (shouldInstall) { + // 使用 pnpm 安装依赖,由于锁文件兼容性问题,不使用 --frozen-lockfile + sh 'pnpm install --no-frozen-lockfile' + + // 保存当前哈希值供下次比较 + writeFile file: '.last_build_hashes', text: "packageJson=${packageJsonHash}\npnpmLock=${pnpmLockHash}" + } else { + echo "Skipping pnpm install due to no changes in package management files" + } // 构建项目 sh 'pnpm run build' @@ -214,13 +260,12 @@ def deployToEnvironment(String env) { docker rm ${containerName} || true """ - // 运行新容器 + // 运行新容器 - 使用标准端口映射 sh """ - docker run -d \ - --name ${containerName} \ - --restart unless-stopped \ - -p ${getPortForEnvironment(env)}:80 \ - --network host.docker.internal:host-gateway \ + docker run -d \\ + --name ${containerName} \\ + --restart unless-stopped \\ + -p ${getPortForEnvironment(env)}:80 \\ ${imageToDeploy} """