From 5e2f92f177f53cc7fa16a7a944b9e37789555354 Mon Sep 17 00:00:00 2001 From: lzh Date: Tue, 13 Jan 2026 09:58:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Jenkinsfile=20?= =?UTF-8?q?=E9=A6=96=E6=AC=A1=E6=9E=84=E5=BB=BA=E6=A3=80=E6=B5=8B=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 改用 git rev-parse HEAD~1 检测上一次提交 - 首次构建时正确触发全量构建 - 添加更多调试日志 --- Jenkinsfile | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 76645e3..99400e6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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") } } }