diff --git a/.github/workflows/bazel_test.yml b/.github/workflows/bazel_test.yml index d79c5066..fdc7891f 100644 --- a/.github/workflows/bazel_test.yml +++ b/.github/workflows/bazel_test.yml @@ -7,8 +7,41 @@ jobs: bazel_test: runs-on: ubuntu-latest steps: - - name: Cache bazel - uses: actions/cache@v2 + - name: Checkout code + uses: actions/checkout@v3 + + - name: Find affected files + id: affected_files + run: | + git fetch origin ${{ github.base_ref }} --depth=1 + git diff --name-only origin/${{ github.base_ref }} > affected_files.txt + echo "Affected files:" + cat affected_files.txt + + - name: Find affected Bazel targets + id: bazel_targets + run: | + > affected_targets.txt + + while read file; do + if [[ -f $file ]]; then + if [[ "$file" == *.cpp || "$file" == *.h || "$file" == *.hpp || "$file" == *.cc || "$file" == *.cxx || "$file" == *.hxx ]]; then + echo "Finding targets for: $file" + targets=$(bazel query --output=package "$file" || true)/... + echo "$targets" >> affected_targets.txt + fi + fi + done < affected_files.txt + + sort -u affected_targets.txt -o affected_targets.txt + echo "Affected targets:" + cat affected_targets.txt + + echo "targets=$(cat affected_targets.txt | tr '\n' ' ')" >> $GITHUB_ENV + + - name: Setup bazel cache + if: env.targets != '' + uses: actions/cache@v3 env: cache-name: bazel-cache with: @@ -18,8 +51,13 @@ jobs: key: ${{ runner.os }}-${{ env.cache-name }}-${{ github.ref }} restore-keys: | ${{ runner.os }}-${{ env.cache-name }}-development - - name: Checkout code - uses: actions/checkout@v3 - - name: Run Bazel test - run: |- - bazel test //... + + - name: Test affected targets + if: env.targets != '' + run: | + echo "Building targets: $targets" + bazel test ${targets} + + - name: No targets to test + if: env.targets == '' + run: echo "No affected Bazel targets found. Skipping test."