fix: update apt package lists before installing cbt CLI - #122
Merged
Conversation
Update cbt installation in .kokoro/build.sh to update apt package lists before installing, and support installing via gcloud components or modern google-cloud-cli-cbt package. Previously, apt install -y google-cloud-sdk-cbt was called without running apt-get update, causing package downloads to 404 when the local package index in older base images pointed to older package versions that were no longer available in the remote pool. This resulted in cbt not being installed and subsequent tests failing with table not found errors.
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates .kokoro/build.sh to robustly locate or install the cbt CLI tool, falling back to gcloud components or apt-get with or without sudo if necessary. The reviewer suggested refactoring the script to eliminate code duplication by extracting the PATH resolution logic into a helper function and using a bash array to conditionally prepend sudo for apt-get commands.
Comment on lines
+49
to
+82
| # Ensure cbt is in PATH if it's installed alongside gcloud but not in PATH | ||
| if ! command -v cbt &> /dev/null && command -v gcloud &> /dev/null; then | ||
| GCLOUD_PATH=$(readlink -f "$(command -v gcloud)") | ||
| GCLOUD_BIN_DIR=$(dirname "${GCLOUD_PATH}") | ||
| if [[ -x "${GCLOUD_BIN_DIR}/cbt" ]]; then | ||
| export PATH="${PATH}:${GCLOUD_BIN_DIR}" | ||
| fi | ||
| fi | ||
|
|
||
| # Install cbt if it is not present | ||
| if ! command -v cbt &> /dev/null; then | ||
| echo "Installing the cbt CLI command." | ||
| if command -v gcloud &> /dev/null && gcloud components install cbt --quiet 2>/dev/null; then | ||
| echo "cbt installed via gcloud components." | ||
| else | ||
| echo "Installing cbt via apt..." | ||
| if command -v sudo &> /dev/null; then | ||
| sudo apt-get update -y || true | ||
| sudo apt-get install -y google-cloud-cli-cbt || sudo apt-get install -y google-cloud-sdk-cbt | ||
| else | ||
| apt-get update -y || true | ||
| apt-get install -y google-cloud-cli-cbt || apt-get install -y google-cloud-sdk-cbt | ||
| fi | ||
| fi | ||
|
|
||
| # Ensure the newly installed cbt is in PATH | ||
| if ! command -v cbt &> /dev/null && command -v gcloud &> /dev/null; then | ||
| GCLOUD_PATH=$(readlink -f "$(command -v gcloud)") | ||
| GCLOUD_BIN_DIR=$(dirname "${GCLOUD_PATH}") | ||
| if [[ -x "${GCLOUD_BIN_DIR}/cbt" ]]; then | ||
| export PATH="${PATH}:${GCLOUD_BIN_DIR}" | ||
| fi | ||
| fi | ||
| fi |
Contributor
There was a problem hiding this comment.
There is significant code duplication in this block:
- The logic to find
cbtnext togcloudand add it toPATHis duplicated exactly twice (lines 50-56 and lines 75-81). - The
apt-getupdate and install commands are duplicated with and withoutsudo(lines 65-71).
We can refactor this by:
- Extracting the
PATHresolution logic into a helper functionadd_cbt_to_path. - Using a bash array
sudo_cmdto conditionally prependsudoif available, avoiding the duplicatedif/elseblock forapt-get. - Removing the redundant
-yflag fromapt-get update(sinceupdatedoes not prompt for confirmation).
# Ensure cbt is in PATH if it's installed alongside gcloud but not in PATH
add_cbt_to_path() {
if ! command -v cbt &> /dev/null && command -v gcloud &> /dev/null; then
local gcloud_path
gcloud_path=$(readlink -f "$(command -v gcloud)")
local gcloud_bin_dir
gcloud_bin_dir=$(dirname "${gcloud_path}")
if [[ -x "${gcloud_bin_dir}/cbt" ]]; then
export PATH="${PATH}:${gcloud_bin_dir}"
fi
fi
}
add_cbt_to_path
# Install cbt if it is not present
if ! command -v cbt &> /dev/null; then
echo "Installing the cbt CLI command."
if command -v gcloud &> /dev/null && gcloud components install cbt --quiet 2>/dev/null; then
echo "cbt installed via gcloud components."
else
echo "Installing cbt via apt..."
sudo_cmd=()
if command -v sudo &> /dev/null; then
sudo_cmd=(sudo)
fi
"${sudo_cmd[@]}" apt-get update || true
"${sudo_cmd[@]}" apt-get install -y google-cloud-cli-cbt || "${sudo_cmd[@]}" apt-get install -y google-cloud-sdk-cbt
fi
# Ensure the newly installed cbt is in PATH
add_cbt_to_path
fi
brandtnewton
approved these changes
Aug 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Update cbt installation in
.kokoro/build.shto update apt package lists before installing, and support installing via gcloud components or moderngoogle-cloud-cli-cbtpackage.Root Cause
Previously,
apt install -y google-cloud-sdk-cbtwas called directly without runningapt-get update. In older base images (such asgcr.io/cloud-devrel-kokoro-resources/java11), the cached package index referenced older package versions (e.g.google-cloud-cli-cbt 514.0.0-0) that had since been replaced and removed frompackages.cloud.google.com/apt, causing404 Not Founderrors:Because
set +ewas active, the build continued withoutcbtinstalled, which caused subsequent tests requiringcbt(such asfuzz) to fail withcbt: command not foundwhen attempting to create/delete test tables.Solution
cbtis already in PATH or in the same directory asgcloud.gcloud components install cbt --quiet.apt-get update -y || truefollowed byapt-get install -y google-cloud-cli-cbt || apt-get install -y google-cloud-sdk-cbt(supportingsudoif present).cbtavailability.