Skip to content

Commit

Permalink
feat: Support a Bazel Path (#29)
Browse files Browse the repository at this point in the history
- Allow users to pass in a path to the Bazel path executable. Useful for
custom installations of Bazel.
- Only install Bazel to the PATH if the expected bazel-path is `bazel`,
but no bazel exists. If a user provides a custom bazel-path, we assume
the executable exists. Similarly, if bazel already exists, no need to
install.
  • Loading branch information
nikhilbirmiwal authored Sep 6, 2023
1 parent 6489285 commit 6c5a6fc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/compute_impacted_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
VERBOSE: 1
WORKSPACE_PATH: ./tests/simple_bazel_workspace
BAZEL_STARTUP_OPTIONS: --host_jvm_args=-Xmx12G,--block_for_lock,--client_debug
BAZEL_PATH: bazel

- name: Validate Impacted Targets Computation
shell: bash
Expand Down
23 changes: 15 additions & 8 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,14 @@ inputs:
https://bazel.build/reference/command-line-reference#startup-options for a complete list. If
unspecified, no startup options are specified.
required: false
bazel-path:
description: A path to the Bazel executable. Defaults to PATH.
required: false
default: bazel

runs:
using: composite
steps:
- name: Setup Bazel
# trunk-ignore(semgrep): Trust third-party `bazelbuild` GH Action
uses: bazelbuild/setup-bazelisk@v2

- name: Setup jq
# trunk-ignore(semgrep): Trust third-party action to install JQ. Source code: https://github.com/dcarbone/install-jq-action/
uses: dcarbone/install-jq-action@v1.0.1

- name: Prerequisites
id: prerequisites
run: ${GITHUB_ACTION_PATH}/src/scripts/prerequisites.sh
Expand All @@ -45,6 +41,16 @@ runs:
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
TARGET_BRANCH: ${{ inputs.target-branch }}
WORKSPACE_PATH: ${{ inputs.bazel-workspace-path }}
BAZEL_PATH: ${{ inputs.bazel-path }}

- name: Install Bazel in PATH
if: ${{ steps.prerequisites.outputs.requires_default_bazel_installation == 'true' }}
# trunk-ignore(semgrep): Trust third-party `bazelbuild` GH Action
uses: bazelbuild/setup-bazelisk@v2

- name: Setup jq
# trunk-ignore(semgrep): Trust third-party action to install JQ. Source code: https://github.com/dcarbone/install-jq-action/
uses: dcarbone/install-jq-action@v1.0.1

- name: Compute Impacted Targets
id: compute-impacted-targets
Expand All @@ -55,6 +61,7 @@ runs:
PR_BRANCH: ${{ github.head_ref }}
VERBOSE: ${{ inputs.verbose }}
WORKSPACE_PATH: ${{ steps.prerequisites.outputs.workspace_path }}
BAZEL_PATH: ${{ inputs.bazel-path }}
BAZEL_STARTUP_OPTIONS: ${{ inputs.bazel-startup-options }}

- name: Upload Impacted Targets
Expand Down
8 changes: 4 additions & 4 deletions src/scripts/compute_impacted_targets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ logIfVerbose "Bazel startup options" "${bazel_startup_options}"

_bazel() {
# trunk-ignore(shellcheck)
bazel ${bazel_startup_options} "$@"
${BAZEL_PATH} ${bazel_startup_options} "$@"
}

# trunk-ignore(shellcheck)
Expand Down Expand Up @@ -91,7 +91,7 @@ fi
# Install the bazel-diff JAR. Avoid cloning the repo, as there will be conflicting WORKSPACES.
curl --retry 5 -Lo bazel-diff.jar https://github.com/Tinder/bazel-diff/releases/latest/download/bazel-diff_deploy.jar
_java -jar bazel-diff.jar -V
bazel version # Does not require running with startup options.
_bazel version # Does not require running with startup options.

# Output Files
merge_instance_branch_out=./${merge_instance_branch_head_sha}
Expand All @@ -100,11 +100,11 @@ impacted_targets_out=./impacted_targets_${pr_branch_head_sha}

# Generate Hashes for the Merge Instance Branch
git switch "${MERGE_INSTANCE_BRANCH}"
bazelDiff generate-hashes --workspacePath="${WORKSPACE_PATH}" "-so=${bazel_startup_options}" "${merge_instance_branch_out}"
bazelDiff generate-hashes --bazelPath="${BAZEL_PATH}" --workspacePath="${WORKSPACE_PATH}" "-so=${bazel_startup_options}" "${merge_instance_branch_out}"

# Generate Hashes for the Merge Instance Branch + PR Branch
git -c "user.name=Trunk Actions" -c "user.email=actions@trunk.io" merge --squash "${PR_BRANCH}"
bazelDiff generate-hashes --workspacePath="${WORKSPACE_PATH}" "-so=${bazel_startup_options}" "${merge_instance_with_pr_branch_out}"
bazelDiff generate-hashes --bazelPath="${BAZEL_PATH}" --workspacePath="${WORKSPACE_PATH}" "-so=${bazel_startup_options}" "${merge_instance_with_pr_branch_out}"

# Compute impacted targets
bazelDiff get-impacted-targets --startingHashes="${merge_instance_branch_out}" --finalHashes="${merge_instance_with_pr_branch_out}" --output="${impacted_targets_out}"
Expand Down
9 changes: 9 additions & 0 deletions src/scripts/prerequisites.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ if [[ -z ${workspace_path} ]]; then
workspace_path=$(pwd)
fi

requires_default_bazel_installation="false"
if [[ ${BAZEL_PATH} == "bazel" ]]; then
if ! command -v bazel; then
requires_default_bazel_installation="true"
fi
fi

# Outputs
# trunk-ignore(shellcheck/SC2129)
echo "merge_instance_branch=${merge_instance_branch}" >>"${GITHUB_OUTPUT}"
echo "workspace_path=${workspace_path}" >>"${GITHUB_OUTPUT}"
echo "requires_default_bazel_installation=${requires_default_bazel_installation}" >>"${GITHUB_OUTPUT}"

0 comments on commit 6c5a6fc

Please sign in to comment.