Skip to content

Instantly share code, notes, and snippets.

@larstobi
Created October 28, 2024 09:22
Show Gist options
  • Save larstobi/48ce278fab30f38062d6b294b897f9ce to your computer and use it in GitHub Desktop.
Save larstobi/48ce278fab30f38062d6b294b897f9ce to your computer and use it in GitHub Desktop.
# azure-pipelines.yml
variables:
currentSemver: '1.2.3' # Replace with your current semver
ACR_NAME: 'myregistry' # Replace with your ACR name
REPO_NAME: 'myrepository' # Replace with your repository name
steps:
- task: AzureCLI@2
inputs:
azureSubscription: 'MyAzureSubscription' # Replace with your Azure subscription
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
set -e
# Authenticate with ACR
az acr login --name $(ACR_NAME)
# Define variables
CURRENT_SEMVER=$(currentSemver)
echo "Current semver: $CURRENT_SEMVER"
# Retrieve all tags from the repository
TAGS=$(az acr repository show-tags --name $(ACR_NAME) --repository $(REPO_NAME) --output tsv)
echo "Tags: $TAGS"
# Loop through each tag
for TAG in $TAGS
do
echo "Processing tag: $TAG"
# Check if the tag is a valid semver
if [[ $TAG =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]
then
echo "Tag $TAG is a valid semver"
# Compare the tag with the current semver
if [ "$(printf '%s\n' "$CURRENT_SEMVER" "$TAG" | sort -V | head -n1)" = "$TAG" ] && [ "$TAG" != "$CURRENT_SEMVER" ]; then
echo "Tag $TAG is older than current semver $CURRENT_SEMVER. Deleting..."
# Delete the image with the older tag
az acr repository delete --name $(ACR_NAME) --image $(REPO_NAME):$TAG --yes
else
echo "Tag $TAG is equal to or newer than current semver $CURRENT_SEMVER. Keeping it."
fi
else
echo "Tag $TAG is not a valid semver. Skipping..."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment