Skip to content

Retrieve image tags #1686

Retrieve image tags

Retrieve image tags #1686

name: Retrieve image tags
on:
workflow_dispatch:
schedule:
- cron: '0 9,13,17 * * *'
permissions:
actions: write
contents: write
pull-requests: write
jobs:
retrieve-image-tags:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0
with:
python-version: '3.10'
cache: 'pip' # caching pip dependencies
- name: Install Helm
env:
HELM_VERSION: "v3.13.0"
run: curl -sL https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz | tar xvzf - -C /usr/local/bin --strip-components=1
- name: Pip
working-directory: ./retrieve-image-tags
run: pip install -r requirements.txt
- id: gimme-versions
working-directory: ./retrieve-image-tags
run: |
OUTPUT=$(python retrieve-image-tags.py)
echo "JSON=${OUTPUT}" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- id: dispatch-add-tag-to-existing-image
run: |
JSON='${{ env.JSON }}'
IMAGE_KEYS=$(echo "${JSON}" | jq -r 'keys[]')
for IMAGE_KEY in $IMAGE_KEYS; do
echo "Image key: ${IMAGE_KEY}" >> $GITHUB_STEP_SUMMARY
# Check if key consists of images and tags or full_images
if echo "${JSON}" | jq -e --arg IMAGE_KEY "$IMAGE_KEY" '.[$IMAGE_KEY].full_images' >/dev/null 2>&1; then
FULL_IMAGES=$(echo "${JSON}" | jq -r --arg IMAGE_KEY "$IMAGE_KEY" '.[$IMAGE_KEY].full_images | join(",")')
PR_TITLE="Added image(s) ${FULL_IMAGES}"
EXISTING_PR=$(gh pr list --state all --limit 1500 --json title,url | jq --arg title "${PR_TITLE}" -r '.[] | select(.title==$title) | .url')
if [ -n "${FULL_IMAGES}" ] && [ -z "${EXISTING_PR}" ]; then
gh workflow run add-tag-to-existing-image.yml --ref $GITHUB_REF_NAME -f full_images=$FULL_IMAGES
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo "Workflow run failed for ${IMAGE_KEY}, exit code ${RETVAL}"
continue
fi
echo -e "Full Images: ${FULL_IMAGES}\n" >> $GITHUB_STEP_SUMMARY
else
if [ -z "${EXISTING_PR}" ]; then
echo -e "No new images found ${IMAGE_KEY}\n" >> $GITHUB_STEP_SUMMARY
else
echo -e "PR already exists (${EXISTING_PR}), rename existing PR title to recreate\n" >> $GITHUB_STEP_SUMMARY
fi
fi
else
IMAGES=$(echo "${JSON}" | jq -r --arg IMAGE_KEY "$IMAGE_KEY" '.[$IMAGE_KEY].images | join(",")')
TAGS=$(echo "${JSON}" | jq -r --arg IMAGE_KEY "$IMAGE_KEY" '.[$IMAGE_KEY].tags | join(",")')
PR_TITLE="Added tag(s) ${TAGS} for image(s) ${IMAGES}"
EXISTING_PR=$(gh pr list --state all --limit 1500 --json title,url | jq --arg title "${PR_TITLE}" -r '.[] | select(.title==$title) | .url')
if [ -n "${IMAGES}" ] && [ -n "${TAGS}" ] && [ -z "${EXISTING_PR}" ]; then
gh workflow run add-tag-to-existing-image.yml --ref $GITHUB_REF_NAME -f images=$IMAGES -f tags=$TAGS
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo "Workflow run failed for ${IMAGE_KEY}, exit code ${RETVAL}"
continue
fi
echo "Images: ${IMAGES}" >> $GITHUB_STEP_SUMMARY
echo -e "Tags: ${TAGS}\n" >> $GITHUB_STEP_SUMMARY
else
if [ -z "${EXISTING_PR}" ]; then
echo -e "No new images/tags found ${IMAGE_KEY}\n" >> $GITHUB_STEP_SUMMARY
else
echo -e "PR already exists (${EXISTING_PR}), rename existing PR title to recreate\n" >> $GITHUB_STEP_SUMMARY
fi
fi
fi
done
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}