Skip to content

Commit

Permalink
Merge pull request #914 from gounthar/patch-1
Browse files Browse the repository at this point in the history
chore(updatecli): Adds documentation to the script and enhances the `jq` command.
  • Loading branch information
dduportal authored Dec 7, 2024
2 parents 87f0a98 + d5986af commit c725374
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions updatecli/scripts/ubi9-latest-tag.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
#!/bin/bash

# This script fetches the latest tag from the Red Hat Container Catalog API for UBI9 images.
# It ensures that `jq` and `curl` are installed, fetches the tags, and processes them to find the unique tag.
# It ensures that `jq` and `curl` are installed, fetches the most recent tags, and processes them to find the unique tag associated to `latest`s.

# The Swagger API endpoints for the Red Hat Container Catalog API are documented at:
# https://catalog.redhat.com/api/containers/v1/ui/#/Repositories/graphql.images.get_images_by_repo

# The script uses the following parameters for the API request:
# - registry: registry.access.redhat.com
# - repository: ubi9
# - page_size: 100
# - page: 0
# - sort_by: last_update_date[desc]

# The curl command fetches the JSON data containing the tags for the UBI9 images, then parses it using `jq` to find the version associated with the "latest" tag.
# It focuses on tags that contain a hyphen, as these represent the long-form tag names.
# The script ensures that only one instance of each tag is kept, in case of duplicates.

# Correct URL of the Red Hat Container Catalog API for UBI9
URL="https://catalog.redhat.com/api/containers/v1/repositories/registry/registry.access.redhat.com/repository/ubi9/images?page_size=100&page=0&sort_by=last_update_date%5Bdesc%5D"

Expand All @@ -16,8 +27,8 @@ if ! command -v jq >/dev/null 2>&1 || ! command -v curl >/dev/null 2>&1; then
exit 1
fi

# Fetch the tags using curl
response=$(curl --silent --fail --location --connect-timeout 10 --max-time 30 --header 'accept: application/json' "$URL")
# Fetch `ubi9` from registry.access.redhat.com sorted by most recent update date, and keeping only the first page.
response=$(curl --silent --fail --location --connect-timeout 10 --retry 3 --retry-delay 2 --max-time 30 --header 'accept: application/json' "$URL")

# Check if the response is empty or null
if [ -z "$response" ] || [ "$response" == "null" ]; then
Expand All @@ -26,20 +37,14 @@ if [ -z "$response" ] || [ "$response" == "null" ]; then
fi

# Parse the JSON response using jq to find the version associated with the "latest" tag
latest_tag=$(echo "$response" | jq -r '.data[].repositories[] | select(.tags[].name == "latest") | .tags[] | select(.name != "latest") | .name')
latest_tag=$(echo "$response" | jq -r '.data[].repositories[] | select(.tags[].name == "latest") | .tags[] | select(.name != "latest" and (.name | contains("-"))) | .name' | sort -u | xargs)

# Check if the latest_tag is empty
if [ -z "$latest_tag" ]; then
echo "Error: No valid tags found."
exit 1
fi

# Sort and remove duplicates
unique_tag=$(echo "$latest_tag" | sort | uniq | grep -v latest | grep "-")

# Trim spaces
unique_tag=$(echo "$unique_tag" | xargs)

# Output the latest version
echo "$unique_tag"
# Output the latest tag version
echo "$latest_tag"
exit 0

0 comments on commit c725374

Please sign in to comment.