Skip to content

Commit 53d67a1

Browse files
committed
refactor: centralize generator docker execution and fallback logic
1 parent c1395d2 commit 53d67a1

4 files changed

Lines changed: 69 additions & 27 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,12 @@ jobs:
321321
- name: validate generation configuration
322322
shell: bash
323323
run: |
324-
docker run \
325-
--rm \
324+
bash generation/run_generator_docker.sh "${library_generation_image_tag}" "${{ github.base_ref || 'main' }}" \
326325
--quiet \
327326
-u "$(id -u):$(id -g)" \
328327
-v "$(pwd):${workspace_name}" \
329328
--entrypoint python \
330-
gcr.io/cloud-devrel-public-resources/java-library-generation:"${library_generation_image_tag}" \
329+
-- \
331330
/src/library_generation/cli/entry_point.py validate-generation-config
332331
env:
333332
library_generation_image_tag: 2.68.0

.github/workflows/generated_files_sync.yaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ jobs:
2727
- name: Generate root pom.xml file
2828
shell: bash
2929
run: |
30-
docker run \
31-
--rm \
30+
bash generation/run_generator_docker.sh "${library_generation_image_tag}" "${{ github.base_ref }}" \
3231
--quiet \
3332
-u "$(id -u):$(id -g)" \
3433
-v "$(pwd):/workspace" \
3534
--entrypoint python \
36-
gcr.io/cloud-devrel-public-resources/java-library-generation:"${library_generation_image_tag}" \
35+
-- \
3736
/src/library_generation/cli/generate_monorepo_root_pom.py \
3837
generate \
3938
--repository-path=/workspace
@@ -48,13 +47,12 @@ jobs:
4847
- name: Generate gapic-libraries-bom/pom.xml
4948
shell: bash
5049
run: |
51-
docker run \
52-
--rm \
50+
bash generation/run_generator_docker.sh "${library_generation_image_tag}" "${{ github.base_ref }}" \
5351
--quiet \
5452
-u "$(id -u):$(id -g)" \
5553
-v "$(pwd):/workspace" \
5654
--entrypoint python \
57-
gcr.io/cloud-devrel-public-resources/java-library-generation:"${library_generation_image_tag}" \
55+
-- \
5856
/src/library_generation/cli/generate_monorepo_gapic_bom.py \
5957
generate \
6058
--repository-path=/workspace \

generation/run_generator_docker.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 workflow file
50+
PREVIOUS_TAG=$(git show "${TARGET_BRANCH}":.github/workflows/hermetic_library_generation.yaml | grep -m 1 "^[[:space:]]*image_tag:" | cut -d ':' -f 2- | cut -d '#' -f 1 | xargs || true)
51+
if [ -n "$PREVIOUS_TAG" ]; then
52+
echo "Using previous image version: $PREVIOUS_TAG"
53+
IMAGE_TAG="$PREVIOUS_TAG"
54+
else
55+
echo "Failed to extract fallback tag. Proceeding with requested tag."
56+
fi
57+
fi
58+
fi
59+
60+
# Execute Docker run with proper ordering
61+
docker run --rm "${DOCKER_OPTS[@]}" "${IMAGE_NAME}:${IMAGE_TAG}" "${CONTAINER_CMD[@]}"

sdk-platform-java/.github/scripts/hermetic_library_generation.sh

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,28 +124,12 @@ changed_libraries="$(cat "${changed_libraries_file}")"
124124
echo "Changed libraries are: ${changed_libraries:-"No changed library"}."
125125

126126
# run hermetic code generation docker image.
127-
# Attempt to pull the image to see if it exists on release PRs.
128-
if [[ "$current_branch" =~ ^release-please-- ]]; then
129-
echo "Detected release PR branch: $current_branch"
130-
if ! docker pull "${IMAGE_NAME}:${image_tag}"; then
131-
echo "Image not found for version ${image_tag}. Falling back to previous version from ${target_branch}."
132-
previous_tag=$(git show "${target_branch}":.github/workflows/hermetic_library_generation.yaml | grep -m 1 "^[[:space:]]*image_tag:" | cut -d ':' -f 2- | cut -d '#' -f 1 | xargs || true)
133-
if [ -n "$previous_tag" ]; then
134-
echo "Using previous image version: $previous_tag"
135-
image_tag="$previous_tag"
136-
else
137-
echo "Failed to extract previous version from ${target_branch}. Proceeding with original tag."
138-
fi
139-
fi
140-
fi
141-
142-
docker run \
143-
--rm \
127+
bash generation/run_generator_docker.sh "${image_tag}" "${target_branch}" \
144128
-u "$(id -u):$(id -g)" \
145129
-v "$(pwd):${workspace_name}" \
146130
-v "${api_def_dir}:${workspace_name}/googleapis" \
147131
-e GENERATOR_VERSION="${image_tag}" \
148-
"${IMAGE_NAME}:${image_tag}" \
132+
-- \
149133
--generation-config-path="${workspace_name}/${generation_config}" \
150134
--library-names="${changed_libraries}" \
151135
--api-definitions-path="${workspace_name}/googleapis"

0 commit comments

Comments
 (0)