From cf9af4efa406805cb8850e478b993cb032cecab0 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Tue, 14 Jul 2026 18:43:39 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E4=BB=85=E6=B5=8B=E8=AF=95PR=E5=8F=98?= =?UTF-8?q?=E6=9B=B4=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pull_request_5x.yml | 9 +++++---- changes.sh | 24 +++++++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/.github/workflows/pull_request_5x.yml b/.github/workflows/pull_request_5x.yml index c2233f249..5e204668a 100644 --- a/.github/workflows/pull_request_5x.yml +++ b/.github/workflows/pull_request_5x.yml @@ -8,9 +8,10 @@ jobs: build: runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v4 + with: + fetch-depth: 0 - name: Set up JDK 17 uses: actions/setup-java@v1 with: @@ -20,5 +21,5 @@ jobs: with: path: ~/.m2 key: ${{ runner.os }}-${{ hashFiles('**/pom.xml') }} - - name: Build with Maven - run: ./mvnw test -q + - name: Build changed modules with Maven + run: ./mvnw test -q -pl "$(./changes.sh)" diff --git a/changes.sh b/changes.sh index 86569f777..44b22bddd 100755 --- a/changes.sh +++ b/changes.sh @@ -1,18 +1,32 @@ #!/usr/bin/env bash -# 收集变更模块 -modules=$(git diff --name-only HEAD~1 HEAD | \ -while read file; do +set -euo pipefail + +# 收集变更模块。 +# PR 场景按目标分支对比,避免 CI 修复类提交只修改 .github 时退化为全仓库测试; +# push 场景保持原有 HEAD~1..HEAD 行为,兼容发布工作流。 +if [ -n "${GITHUB_BASE_REF:-}" ]; then + git fetch origin "${GITHUB_BASE_REF}" --depth=1 >/dev/null 2>&1 || true + if git rev-parse --verify "origin/${GITHUB_BASE_REF}" >/dev/null 2>&1; then + diff_args=("origin/${GITHUB_BASE_REF}...HEAD") + else + diff_args=("HEAD~1" "HEAD") + fi +else + diff_args=("HEAD~1" "HEAD") +fi + +modules=$(git diff --name-only "${diff_args[@]}" | while read -r file; do dir=$(dirname "$file") while [ "$dir" != "." ] && [ "$dir" != "/" ]; do if [ -f "$dir/pom.xml" ]; then echo "$dir"; break; fi dir=$(dirname "$dir") done -done | sort -u | tr '\n' ',' | sed 's/,$//') +done | sort -u | paste -sd, -) # 如果为空,则使用默认值 '.' if [ -z "$modules" ]; then echo "." else echo "$modules" -fi \ No newline at end of file +fi