diff --git a/.github/workflows/push-image.yml b/.github/workflows/push-image.yml index c463e93..f491fed 100644 --- a/.github/workflows/push-image.yml +++ b/.github/workflows/push-image.yml @@ -81,21 +81,49 @@ jobs: env: IMAGE_NAME: ${{ matrix.image }} REGISTRY_ALIAS: ${{ vars.AWS_REGISTRY_ALIAS }} + shell: bash run: | set -euo pipefail - # Query ECR Public for existing image tags matching semver pattern - TAGS=$(aws ecr-public describe-image-tags \ - --region us-east-1 \ - --repository-name "${IMAGE_NAME}" \ - --no-paginate \ - --query 'imageTagDetails[].imageTag' \ - --output text 2>/dev/null || echo "") + if [[ -z "${IMAGE_NAME:-}" ]]; then + echo "ERROR: IMAGE_NAME environment variable is not set" >&2 + exit 1 + fi - # Find the highest semver tag - LATEST_VERSION=$(echo "$TAGS" | tr '\t' '\n' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1 || true) + # Verify the repository exists — fail immediately if it doesn't. + aws ecr-public describe-repositories \ + --region us-east-1 \ + --repository-names "${IMAGE_NAME}" > /dev/null 2>&1 || { + echo "ERROR: Repository ${IMAGE_NAME} does not exist in ECR Public." >&2 + exit 1 + } - if [[ -z "$LATEST_VERSION" ]]; then + # Query existing image tags. + RESPONSE=$(aws ecr-public describe-image-tags \ + --region us-east-1 \ + --repository-name "${IMAGE_NAME}" \ + --no-paginate) || { + echo "ERROR: Failed to query image tags for ${IMAGE_NAME}." >&2 + exit 1 + } + + TAGS=$(echo "$RESPONSE" | jq -r '.imageTagDetails') || { + echo "ERROR: Failed to parse ECR response as JSON:" >&2 + echo "$RESPONSE" >&2 + exit 1 + } + + # Find the highest semver tag using jq sort + LATEST_VERSION=$(echo "$TAGS" | \ + jq -r '[.[] | select(.imageTag | test("^[0-9]+\\.[0-9]+\\.[0-9]+$"))] | sort_by(.imageTag | split(".") | map(tonumber))' | \ + jq -r '.[-1].imageTag') || { + echo "ERROR: Failed to extract latest version from tags:" >&2 + echo "$TAGS" >&2 + exit 1 + } + + # If no semver tags exist yet, start at 1.0.0; otherwise bump patch. + if [[ -z "$LATEST_VERSION" || "$LATEST_VERSION" == "null" ]]; then NEXT_VERSION="1.0.0" else IFS='.' read -r MAJOR MINOR PATCH <<< "$LATEST_VERSION"