Jenkins file edit
All checks were successful
test/timeline-frontend/pipeline/head This commit looks good
All checks were successful
test/timeline-frontend/pipeline/head This commit looks good
This commit is contained in:
61
Jenkinsfile
vendored
61
Jenkinsfile
vendored
@@ -65,8 +65,54 @@ pipeline {
|
|||||||
// 安装 pnpm
|
// 安装 pnpm
|
||||||
sh 'npm install -g pnpm'
|
sh 'npm install -g pnpm'
|
||||||
|
|
||||||
// 使用 pnpm 安装依赖,由于锁文件兼容性问题,不使用 --frozen-lockfile
|
// 检查 package.json 或 pnpm-lock.yaml 是否自上次构建以来发生了更改
|
||||||
sh 'pnpm install --no-frozen-lockfile'
|
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'
|
sh 'pnpm run build'
|
||||||
@@ -214,13 +260,12 @@ def deployToEnvironment(String env) {
|
|||||||
docker rm ${containerName} || true
|
docker rm ${containerName} || true
|
||||||
"""
|
"""
|
||||||
|
|
||||||
// 运行新容器
|
// 运行新容器 - 使用标准端口映射
|
||||||
sh """
|
sh """
|
||||||
docker run -d \
|
docker run -d \\
|
||||||
--name ${containerName} \
|
--name ${containerName} \\
|
||||||
--restart unless-stopped \
|
--restart unless-stopped \\
|
||||||
-p ${getPortForEnvironment(env)}:80 \
|
-p ${getPortForEnvironment(env)}:80 \\
|
||||||
--network host.docker.internal:host-gateway \
|
|
||||||
${imageToDeploy}
|
${imageToDeploy}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user