Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions scripts/publish/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,27 @@ steps:
echo "Skipping firepit build for preview version."
fi

# Tag the previous version of the docker image
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk:slim"
entrypoint: "bash"
args:
- "-c"
- |
if [ "${_VERSION}" != "preview" ]; then
IMAGE_NAME="us-docker.pkg.dev/${_ARTIFACT_REGISTRY_PROJECT}/us/firebase"
TAGS=$(gcloud container images list-tags $${IMAGE_NAME} --filter="tags:latest" --format="value(tags)")
echo "Tags for latest: $${TAGS}"
PREVIOUS_VERSION=$(echo $${TAGS} | tr ',' '\n' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$$' | head -n 1)
echo "Detected previous version: $${PREVIOUS_VERSION}"
if [ -n "$${PREVIOUS_VERSION}" ]; then
gcloud container images add-tag $${IMAGE_NAME}:latest $${IMAGE_NAME}:no-new-use-public-image-$${PREVIOUS_VERSION} --quiet
Comment on lines +170 to +175
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation uses legacy gcloud container commands and could be more robust when parsing tags.

  1. Use Artifact Registry Commands: Since the registry is us-docker.pkg.dev, it is recommended to use gcloud artifacts docker commands instead of the legacy gcloud container commands.
  2. Handle Missing Images: If the image does not exist yet (e.g., in a new environment or first run), the gcloud command will exit with a non-zero status, which might fail the build depending on the shell environment. Adding || true and redirecting stderr ensures the script continues gracefully.
  3. Robust Parsing: Quoting the TAGS variable in echo prevents word splitting issues. Using tr ',;' handles different potential delimiters (semicolon is the default for many gcloud list values) to ensure tags are correctly split into lines for grep.
          TAGS=$(gcloud artifacts docker images list $${IMAGE_NAME} --filter="tags:latest" --format="value(tags)" 2>/dev/null || true)
          echo "Tags for latest: $${TAGS}"
          PREVIOUS_VERSION=$(echo "$${TAGS}" | tr ',;' '\n' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$$' | head -n 1)
          echo "Detected previous version: $${PREVIOUS_VERSION}"
          if [ -n "$${PREVIOUS_VERSION}" ]; then
            gcloud artifacts docker tags add $${IMAGE_NAME}:latest $${IMAGE_NAME}:no-new-use-public-image-$${PREVIOUS_VERSION} --quiet

else
echo "Could not detect previous version tag. Skipping tagging."
fi
else
echo "Skipping for preview version."
fi

# Publish the Firebase docker image
- name: "gcr.io/cloud-builders/docker"
entrypoint: "bash"
Expand Down
Loading