Skip to content
Merged
97 changes: 93 additions & 4 deletions .github/workflows/docker-release2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ jobs:
org.opencontainers.image.vendor=DragonflyDB LTD
org.opencontainers.image.title=Dragonfly Production Image
org.opencontainers.image.description=The fastest in-memory store
org.opencontainers.image.version=${{ env.TAG_NAME }}

- name: Build image
id: build
Expand Down Expand Up @@ -151,16 +152,104 @@ jobs:
local tag_name=$5
local is_prerelease=$6

# Function for semantic version comparison
# Returns true if current_version >= latest_version
semver_cmp() {
local current_version=$1
local latest_version=$2
local should_update=true

# Extract major.minor.patch components
IFS='.' read -ra CURRENT_PARTS <<< "$current_version"
IFS='.' read -ra LATEST_PARTS <<< "$latest_version"

# Pad arrays to same length for comparison
while [ ${#CURRENT_PARTS[@]} -lt 3 ]; do CURRENT_PARTS+=(0); done
while [ ${#LATEST_PARTS[@]} -lt 3 ]; do LATEST_PARTS+=(0); done

# Compare major.minor.patch numerically
if (( 10#${CURRENT_PARTS[0]} < 10#${LATEST_PARTS[0]} )); then
should_update=false
elif (( 10#${CURRENT_PARTS[0]} == 10#${LATEST_PARTS[0]} )) && (( 10#${CURRENT_PARTS[1]} < 10#${LATEST_PARTS[1]} )); then
should_update=false
elif (( 10#${CURRENT_PARTS[0]} == 10#${LATEST_PARTS[0]} )) && (( 10#${CURRENT_PARTS[1]} == 10#${LATEST_PARTS[1]} )) && (( 10#${CURRENT_PARTS[2]} < 10#${LATEST_PARTS[2]} )); then
should_update=false
fi

# Log debug info to stderr instead of stdout
echo "Version comparison: current=${CURRENT_PARTS[0]}.${CURRENT_PARTS[1]}.${CURRENT_PARTS[2]} vs latest=${LATEST_PARTS[0]}.${LATEST_PARTS[1]}.${LATEST_PARTS[2]}" >&2

# Return only the result
echo $should_update
}

if [[ "$is_prerelease" == 'true' ]]; then
# Create and push the manifest like dragonfly:alpha-ubuntu
tag="${registry}:alpha-${flavor}"
docker manifest create ${tag} --amend ${sha_amd} --amend ${sha_arm}
docker manifest push ${tag}
elif [[ "$flavor" == 'ubuntu' ]]; then
tag="${registry}:latest"
# Create and push the manifest like dragonfly:latest
docker manifest create ${tag} --amend ${sha_amd} --amend ${sha_arm}
docker manifest push ${tag}
# Checking if this version should be tagged as latest
echo "Checking if ${tag_name} should be tagged as latest..."

# Remove 'v' prefix if present for semantic comparison
current_version=${tag_name#v}

# Get the current latest version by running the latest image
latest_version=""
if docker pull ${registry}:latest &>/dev/null; then
echo "Found latest tag, checking its version..."

# First try to get version from image labels using docker inspect
echo "Method 1: Trying to get version from image labels..."
label_version=$(docker image inspect --format '{{ index .Config.Labels "org.opencontainers.image.version" }}' ${registry}:latest 2>/dev/null || echo "")

if [[ -n "$label_version" ]]; then
echo "Found version from image labels: $label_version"

# Extract version from format like "ubuntu-1.28.1-arm64"
if [[ $label_version == ubuntu-*-* ]]; then
# Extract the middle part (version) from ubuntu-VERSION-arch
latest_full_version=$(echo "$label_version" | cut -d'-' -f2)
else
# Use the label as is
latest_full_version=$label_version
fi

echo "Extracted version: $latest_full_version"
else
# Fallback to running the container if label inspect failed
echo "Method 2: Falling back to container execution..."
latest_full_version=$(docker run --rm --entrypoint /bin/sh ${registry}:latest -c "dragonfly --version | cut -d' ' -f2 | head -n 1")
fi

echo "Latest full version: ${latest_full_version}"

# Extract only the semantic version part (before any dash)
latest_version=$(echo "${latest_full_version}" | cut -d'-' -f1)
# Remove 'v' prefix if present
latest_version=${latest_version#v}
echo "Current latest version: ${latest_version}"
else
echo "No latest tag found yet or couldn't pull it"
fi

# Compare versions only if we have a latest version
should_update_latest=true
if [[ -n "$latest_version" ]]; then
# Call our semver comparison function
should_update_latest=$(semver_cmp "$current_version" "$latest_version")
fi

if [[ "$should_update_latest" == true ]]; then
echo "Version ${tag_name} is newer than or equal to current latest, updating latest tag"
tag="${registry}:latest"
# Create and push the manifest like dragonfly:latest
docker manifest create ${tag} --amend ${sha_amd} --amend ${sha_arm}
docker manifest push ${tag}
else
echo "Version ${tag_name} is older than current latest (${latest_version}), NOT updating latest tag"
fi
fi

# Create and push the manifest like dragonfly:v1.26.4
Expand Down
Loading