Skip to content

Commit

Permalink
Bazel build only build affected targets on pull_request
Browse files Browse the repository at this point in the history
  • Loading branch information
GleasonK committed Feb 28, 2025
1 parent d2708ad commit 1aaea43
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/buildAndTestBazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ jobs:
runs-on: ${{ github.repository == 'openxla/stablehlo' && 'ubuntu-22.04-64core' || 'ubuntu-22.04' }}

steps:
# Checkout StableHLO with all commits in the chain
- name: Checkout StableHLO
uses: actions/checkout@v4
with:
fetch-depth: $(( ${{ github.event_name == 'pull_request' && github.event.pull_request.commits || 0 }} + 1 ))

- name: Validate LLVM Commit
run: |
Expand All @@ -63,7 +66,18 @@ jobs:
restore-keys: |
${{ runner.os }}-stablehlo_bazelbuild-
- name: Build StableHLO
- name: Build and Test StableHLO (Diff Only)
if: ${{ github.event_name == 'pull_request' }}
shell: bash
run: |
START_HASH=${{ github.event.pull_request.base.sha }}
END_HASH=${{ github.event.pull_request.head.sha }}
BAZEL_DIFF=/tmp/bazel-diff.jar
curl -Lo "$BAZEL_DIFF" https://github.com/Tinder/bazel-diff/releases/latest/download/bazel-diff_deploy.jar
./build_tools/github_actions/ci_build_bazel.sh "$BAZEL_DIFF" "$START_HASH" "$END_HASH"
- name: Build and Test StableHLO (All)
if: ${{ github.event_name != 'pull_request' }}
shell: bash
run: |
./build_tools/github_actions/ci_build_bazel.sh
Expand Down
75 changes: 70 additions & 5 deletions build_tools/github_actions/ci_build_bazel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,76 @@ set -o errexit
set -o nounset
set -o pipefail

if [[ $# -ne 0 ]] ; then
echo "Usage: $0"
if [[ $# -ne 0 && $# -ne 3 ]] ; then
echo "Usage: $0 [<bazel-diff.jar> <start_commit> <end_commit>]"
echo " Builds and tests all bazel targets."
echo " If start_commit and end_commit are specified, uses bazel-diff to"
echo " determine the set of targets to build based on code changes."
echo
echo "Example: Build all affected targets between current branch and main"
echo " $ curl -Lo bazel-diff.jar https://github.com/Tinder/bazel-diff/releases/latest/download/bazel-diff_deploy.jar"
echo " $ ci_build_bazel.sh bazel-diff.jar $(git merge-base main HEAD) $(git rev-parse HEAD)"
exit 1
fi

# Build and Test StableHLO
bazel build --lockfile_mode=error //... --config=asan --config=ubsan
bazel test //... --config=asan --config=ubsan
bazel-test-all() {
# Build and Test StableHLO
bazel build --lockfile_mode=error //... --config=asan --config=ubsan
bazel test //... --config=asan --config=ubsan
}

bazel-test-diff() {
set -e
WORKSPACE_PATH=$(pwd) #$(git rev-parse --show-toplevel)
BAZEL_DIFF_JAR="$1"
PREVIOUS_REV="$2" # Starting Revision SHA
FINAL_REV="$3" # Final Revision SHA
BAZEL_PATH="$(which bazel)"

STARTING_HASHES_JSON="/tmp/starting_hashes.json"
FINAL_HASHES_JSON="/tmp/final_hashes.json"
IMPACTED_TARGETS_PATH="/tmp/impacted_targets.txt"

bazel-diff() {
java -jar "$BAZEL_DIFF_JAR" "$@"
}

git -C "$WORKSPACE_PATH" checkout "$PREVIOUS_REV" --quiet

echo "Generating Hashes for Revision '$PREVIOUS_REV'"
bazel-diff generate-hashes -w "$WORKSPACE_PATH" -b "$BAZEL_PATH" $STARTING_HASHES_JSON

git -C "$WORKSPACE_PATH" checkout "$FINAL_REV" --quiet

echo "Generating Hashes for Revision '$FINAL_REV'"
bazel-diff generate-hashes -w "$WORKSPACE_PATH" -b "$BAZEL_PATH" $FINAL_HASHES_JSON

echo "Determining Impacted Targets"
bazel-diff get-impacted-targets -sh $STARTING_HASHES_JSON -fh $FINAL_HASHES_JSON -o $IMPACTED_TARGETS_PATH
echo ""

impacted_targets=()
IFS=$'\n' read -d '' -r -a impacted_targets < $IMPACTED_TARGETS_PATH || true
formatted_impacted_targets=$(IFS=$'\n'; echo "${impacted_targets[*]}")
if [[ -z "$formatted_impacted_targets" ]]; then
echo "No impacted targets from change."
exit 0
fi

echo "Impacted Targets between $PREVIOUS_REV and $FINAL_REV:"
echo "$formatted_impacted_targets"
echo ""

# Build and Test impacted targets
bazel build --target_pattern_file=/tmp/impacted_targets.txt
bazel test --target_pattern_file=/tmp/impacted_targets.txt
}

# Run bazel build and test
if [[ $# -eq 0 ]] ; then
bazel-test-all
else
bazel-test-diff "$@"
fi


0 comments on commit 1aaea43

Please sign in to comment.