|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2026 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +set -exo pipefail |
| 17 | + |
| 18 | +REQUESTED_TAG="$1" |
| 19 | +TARGET_BRANCH="$2" |
| 20 | +shift 2 |
| 21 | + |
| 22 | +# Parse arguments using '--' as delimiter |
| 23 | +DOCKER_OPTS=() |
| 24 | +CONTAINER_CMD=() |
| 25 | +found_delimiter=false |
| 26 | + |
| 27 | +for arg in "$@"; do |
| 28 | + if [ "$arg" == "--" ]; then |
| 29 | + found_delimiter=true |
| 30 | + continue |
| 31 | + fi |
| 32 | + if $found_delimiter; then |
| 33 | + CONTAINER_CMD+=("$arg") |
| 34 | + else |
| 35 | + DOCKER_OPTS+=("$arg") |
| 36 | + fi |
| 37 | +done |
| 38 | + |
| 39 | +IMAGE_NAME="gcr.io/cloud-devrel-public-resources/java-library-generation" |
| 40 | +# Support both local git and GitHub Actions environment variables |
| 41 | +CURRENT_BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME:-$(git branch --show-current)}}" |
| 42 | +IMAGE_TAG="$REQUESTED_TAG" |
| 43 | + |
| 44 | +# Fallback logic on Release PR branches |
| 45 | +if [[ "$CURRENT_BRANCH" =~ ^release-please-- ]]; then |
| 46 | + echo "Detected release PR branch: $CURRENT_BRANCH" |
| 47 | + if ! docker pull "${IMAGE_NAME}:${IMAGE_TAG}"; then |
| 48 | + echo "Image not found for version ${IMAGE_TAG}. Falling back to previous version from ${TARGET_BRANCH}." |
| 49 | + # Extract tag from target branch's versions.txt using explicit fetch |
| 50 | + git fetch origin "${TARGET_BRANCH}" --depth=1 || true |
| 51 | + PREVIOUS_TAG=$(git show FETCH_HEAD:versions.txt | grep "^gapic-generator-java:" | cut -d ':' -f 2 || true) |
| 52 | + if [ -n "$PREVIOUS_TAG" ]; then |
| 53 | + echo "Using previous image version: $PREVIOUS_TAG" |
| 54 | + IMAGE_TAG="$PREVIOUS_TAG" |
| 55 | + else |
| 56 | + echo "Failed to extract fallback tag. Proceeding with requested tag." |
| 57 | + fi |
| 58 | + fi |
| 59 | +fi |
| 60 | + |
| 61 | +# Execute Docker run with proper ordering |
| 62 | +docker run --rm "${DOCKER_OPTS[@]}" "${IMAGE_NAME}:${IMAGE_TAG}" "${CONTAINER_CMD[@]}" |
0 commit comments