fix: 修复 Jenkinsfile 首次构建检测逻辑
- 改用 git rev-parse HEAD~1 检测上一次提交 - 首次构建时正确触发全量构建 - 添加更多调试日志
This commit is contained in:
19
Jenkinsfile
vendored
19
Jenkinsfile
vendored
@@ -59,12 +59,15 @@ pipeline {
|
||||
// 获取变更的文件列表
|
||||
def changedFiles = sh(
|
||||
script: '''
|
||||
if [ "${GIT_PREVIOUS_COMMIT}" = "" ]; then
|
||||
# 首次构建,构建所有核心服务
|
||||
# 获取上一次成功构建的提交
|
||||
PREV_COMMIT=$(git rev-parse HEAD~1 2>/dev/null || echo "")
|
||||
|
||||
if [ -z "$PREV_COMMIT" ]; then
|
||||
# 首次构建或只有一个提交,构建所有核心服务
|
||||
echo "all"
|
||||
else
|
||||
# 检测变更的文件
|
||||
git diff --name-only ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT}
|
||||
git diff --name-only $PREV_COMMIT HEAD
|
||||
fi
|
||||
''',
|
||||
returnStdout: true
|
||||
@@ -75,8 +78,9 @@ pipeline {
|
||||
// 分析需要构建的服务
|
||||
def servicesToBuild = []
|
||||
|
||||
if (changedFiles == 'all') {
|
||||
if (changedFiles == 'all' || changedFiles.isEmpty()) {
|
||||
// 首次构建或强制全量构建
|
||||
echo "首次构建或无法检测变更,构建所有核心服务"
|
||||
servicesToBuild = CORE_SERVICES.split(',')
|
||||
} else {
|
||||
// 检测每个服务是否有变更
|
||||
@@ -86,13 +90,16 @@ pipeline {
|
||||
if (changedFiles.contains(modulePath) ||
|
||||
changedFiles.contains('pom.xml') ||
|
||||
changedFiles.contains('viewsh-framework') ||
|
||||
changedFiles.contains('viewsh-dependencies')) {
|
||||
changedFiles.contains('viewsh-dependencies') ||
|
||||
changedFiles.contains('Jenkinsfile') ||
|
||||
changedFiles.contains('docker/')) {
|
||||
servicesToBuild.add(service)
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有检测到变更,但有代码提交,构建所有服务
|
||||
if (servicesToBuild.isEmpty() && !changedFiles.isEmpty()) {
|
||||
echo "检测到代码变更但未匹配到具体服务,构建所有服务"
|
||||
servicesToBuild = CORE_SERVICES.split(',')
|
||||
}
|
||||
}
|
||||
@@ -103,7 +110,7 @@ pipeline {
|
||||
if (servicesToBuild.isEmpty()) {
|
||||
echo "No services need to be built. Skipping build."
|
||||
currentBuild.result = 'SUCCESS'
|
||||
return
|
||||
error("No changes detected, skipping build")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user