Skip to content

Check Missing Container Images #21

Check Missing Container Images

Check Missing Container Images #21

name: Check Missing Container Images
on:
workflow_dispatch:
schedule:
- cron: '0 6 * * *'
jobs:
check-missing-images:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v5
with:
fetch-depth: 0
fetch-tags: true
- name: List version tags
id: list_tags
env:
VERSION_PREFIX: ${{ vars.VERSION_PREFIX || 'v' }}
run: |
if [[ -n "${VERSION_PREFIX}" ]]; then
tags=$(git tag | grep "^${VERSION_PREFIX}" | sort -r)
else
tags=$(git tag | sort -r)
fi
# Output tags for next step
{
echo "tags<<EOF"
printf "%s\n" "${tags}"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Check GHCR for existing images
id: check_images
env:
TOKEN: ${{ secrets.GHCR_READ_TOKEN || secrets.GITHUB_TOKEN }}
SKIP_TAGS: ${{ vars.SKIP_TAGS }}
GHCR_ORG: ${{ vars.GHCR_ORG || github.repository_owner }}
GHCR_CONTAINER: ${{ vars.GHCR_CONTAINER }}
run: |
missing_tags=()
# Use GitHub REST API to list container image versions (tags)
ghcr_tags=$(curl -sSL \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Bearer ${TOKEN}" \
"https://api.github.com/orgs/${GHCR_ORG}/packages/container/${GHCR_CONTAINER}/versions" \
| jq -r '.[].metadata.container.tags[]?' 2>/dev/null || true)
echo "📦 Existing GHCR tags:"
echo "$ghcr_tags"
# Combine multiline skip patterns into single regex
skip_regex=""
if [[ -n "${SKIP_TAGS:-}" ]]; then
skip_regex=$(echo "$SKIP_TAGS" | tr -s '\r\n' '|' | sed 's/|$//')
echo "🚫 Skip regex: $skip_regex"
else
echo "ℹ️ No skip patterns defined."
fi
declare -i skipped_count=0
declare -i total_tags=0
while IFS= read -r tag; do
total_tags+=1
if [[ -n "$skip_regex" && "$tag" =~ $skip_regex ]]; then
echo "➡️ Skipping $tag (matches skip pattern)"
skipped_count+=1
continue
fi
if ! grep -qx "${tag#v}" <<< "$ghcr_tags" >/dev/null 2>&1; then
echo "❌ Image missing for $tag"
missing_tags+=("$tag")
fi
done <<< "${{ steps.list_tags.outputs.tags }}"
echo "===== Summary ====="
echo " Total tags checked: $total_tags"
echo " Skipped tags: $skipped_count"
echo " Missing images found: ${#missing_tags[@]}"
# Output missing tags for next step
{
echo "missing_tags<<EOF"
printf "%s\n" "${missing_tags[@]}"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Generate GitHub App token
id: app_token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ secrets.WORKFLOW_APP_ID }}
private-key: ${{ secrets.WORKFLOW_APP_PRIVATE_KEY }}
- name: Trigger build-and-deploy for missing tags
if: steps.check_images.outputs.missing_tags != ''
env:
GH_TOKEN: ${{ steps.app_token.outputs.token }}
WORKFLOW: ${{ vars.BUILD_WORKFLOW || 'build-and-deploy.yml' }}
run: |
while read -r tag; do
echo "Triggering build-and-deploy for $tag"
gh workflow run "${BUILD_WORKFLOW}" --ref "$tag"
done <<< "${{ steps.check_images.outputs.missing_tags }}"