Skip to content

Commit 1c277e3

Browse files
authored
fix: resolve BASE_REPO variable scope issue (#17)
## Problem The `BASE_REPO` variable was only defined inside the repository allowlist validation block (step 3), but it was being used later in the image existence check (step 5). This caused the variable to be empty when `verify_image_existence` was enabled, leading to validation failures. ## Root Cause In the image validation loop: - `BASE_REPO` extraction logic was inside the `if [ -n "$ALLOWED_REPOS" ]` block (lines 433-440) - The image existence check at line 524 used `CANONICAL_IMAGE="${BASE_REPO}:${TAG}"` - When `ALLOWED_REPOS` was set, `BASE_REPO` was defined and everything worked - However, the variable was being used outside its scope, which is a logic error ## Solution - **Move BASE_REPO extraction logic** outside the conditional block (before step 4) - Now `BASE_REPO` is always available for both repository validation and image existence check - Update step numbering in comments: steps 4-7 instead of 3-5 - Add explicit logging of `BASE_REPO` value for debugging ## Changes ```diff # 2. Extract repository and tag REPO="${image%:*}" TAG="${image##*:}" +# 3. Extract base repository name (always, needed for multiple validations) +BASE_REPO="$REPO" +if [[ "$REPO" =~ / ]]; then + if [[ "$REPO" =~ ^[a-z0-9.-]+\.[a-z]{2,}/ ]] || [[ "$REPO" =~ ^gcr\.io/ ]] || [[ "$REPO" =~ ^.*\.gcr\.io/ ]]; then + BASE_REPO="${REPO#*/}" + fi +fi +echo " Base repository: $BASE_REPO" + -# 3. Check repository is in allowlist (if configured) +# 4. Check repository is in allowlist (if configured) if [ -n "$ALLOWED_REPOS" ]; then - BASE_REPO="$REPO" # ← Was only defined here - if [[ "$REPO" =~ / ]]; then - ... - fi ... fi ``` ## Testing This fixes the validation failure in [PR #362](dotCMS/deutschebank-infrastructure#362) where the image existence check was failing due to empty `BASE_REPO` variable. After this fix is merged and v1.1.1 tag is recreated, PR #362 should pass all validations. ## Related - Fixes issue discovered in deutschebank-infrastructure PR #362 - Related to #15 (subshell fixes) - Related to #16 (second subshell fix)
1 parent 2338b52 commit 1c277e3

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

.github/workflows/deployment-guard.yml

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -417,28 +417,30 @@ jobs:
417417
echo " Repository: $REPO"
418418
echo " Tag: $TAG"
419419
420-
# 3. Check repository is in allowlist (if configured)
420+
# 3. Extract base repository name (handle both with and without registry prefix)
421+
# This is needed for both repository validation and image existence check
422+
# Examples:
423+
# mirror.gcr.io/dotcms/dotcms -> dotcms/dotcms
424+
# gcr.io/project/dotcms/dotcms -> dotcms/dotcms
425+
# dotcms/dotcms -> dotcms/dotcms
426+
BASE_REPO="$REPO"
427+
if [[ "$REPO" =~ / ]]; then
428+
# Check if REPO starts with a registry domain
429+
if [[ "$REPO" =~ ^[a-z0-9.-]+\.[a-z]{2,}/ ]] || [[ "$REPO" =~ ^gcr\.io/ ]] || [[ "$REPO" =~ ^.*\.gcr\.io/ ]]; then
430+
# Extract everything after the first slash (removes registry domain)
431+
BASE_REPO="${REPO#*/}"
432+
fi
433+
fi
434+
echo " Base repository: $BASE_REPO"
435+
436+
# 4. Check repository is in allowlist (if configured)
421437
if [ -n "$ALLOWED_REPOS" ]; then
422438
REPO_ALLOWED=false
423439
IFS=',' read -ra ALLOWED <<< "$ALLOWED_REPOS"
424440
for allowed_repo in "${ALLOWED[@]}"; do
425441
# Trim whitespace
426442
allowed_repo=$(echo "$allowed_repo" | xargs)
427443
428-
# Extract base repository name (handle both with and without registry prefix)
429-
# Examples:
430-
# mirror.gcr.io/dotcms/dotcms -> dotcms/dotcms
431-
# gcr.io/project/dotcms/dotcms -> dotcms/dotcms
432-
# dotcms/dotcms -> dotcms/dotcms
433-
BASE_REPO="$REPO"
434-
if [[ "$REPO" =~ / ]]; then
435-
# Check if REPO starts with a registry domain
436-
if [[ "$REPO" =~ ^[a-z0-9.-]+\.[a-z]{2,}/ ]] || [[ "$REPO" =~ ^gcr\.io/ ]] || [[ "$REPO" =~ ^.*\.gcr\.io/ ]]; then
437-
# Extract everything after the first slash (removes registry domain)
438-
BASE_REPO="${REPO#*/}"
439-
fi
440-
fi
441-
442444
echo " Comparing '$BASE_REPO' with allowed '$allowed_repo'"
443445
if [[ "$BASE_REPO" == "$allowed_repo" ]] || [[ "$REPO" == "$allowed_repo" ]]; then
444446
REPO_ALLOWED=true
@@ -460,7 +462,7 @@ jobs:
460462
echo "ℹ️ Repository validation skipped (no allowlist configured)"
461463
fi
462464
463-
# 4. Validate tag matches version pattern
465+
# 5. Validate tag matches version pattern
464466
if ! [[ "$TAG" =~ $VERSION_PATTERN ]]; then
465467
echo "❌ Version pattern validation failed"
466468
echo " Tag: $TAG"
@@ -472,7 +474,7 @@ jobs:
472474
fi
473475
echo "✅ Tag matches version pattern"
474476
475-
# 4.5. Anti-downgrade validation (compare versions)
477+
# 6. Anti-downgrade validation (compare versions)
476478
# Get the corresponding old image by index
477479
OLD_IMAGE="${OLD_IMAGES_ARRAY[$INDEX]}"
478480
if [ -n "$OLD_IMAGE" ]; then
@@ -517,7 +519,7 @@ jobs:
517519
# Increment index for next iteration
518520
((INDEX++))
519521
520-
# 5. Verify image exists in Docker Hub (canonical registry)
522+
# 7. Verify image exists in Docker Hub (canonical registry)
521523
if [ "$VERIFY_EXISTENCE" = "true" ]; then
522524
# Use canonical image (without registry prefix) to verify in Docker Hub
523525
# This assumes mirror registries have the same images as Docker Hub

0 commit comments

Comments
 (0)