Using a container registry to store SAS container images is optional but recommended. You can use the SAS Mirror Manager to create the mirror registry. If you use the mirror manager to create a mirror each time you upgrade your environment, your mirror registry will fill up quickly with multiple versions of the same image. This can potentially start to incur extra costs.
At this point in time, there is no option to clean a mirror registry with the SAS Mirror Manager.
Azure Container Registry offers built-in functionalities to manage older image versions. This functionality is only available with the premium tier, however.
To clean the images, you can use the following script that searches an Azure Container Registry for all SAS related repositories and deletes all images but the last two versions. The number of versions to keep can easily be adjusted. Note that the script relies, quite heavily, on the magic that jq provides. If you run the script in Azure Cloud Shell, jq will already be installed. In other environments you may have to install it beforehand.
acr=<name of your ACR>
subscription_id=<subscription that contains your ACR>
images_to_keep=2
az login
az account set --subscription $subscription_id
repositories=$(az acr repository list --name $acr)
sas_repositories=$(echo $repositories | jq -c 'map(select(.|startswith("viya-4-x64_oci_linux_2-docker")))')
echo $sas_repositories | jq -r '.[]' | while read repository; do
tags=$(az acr repository show-tags --name $acr --repository $repository)
sorted_tags=$(echo $tags | jq 'sort_by(.|split("\\.|-";"")|map(tonumber))')
delete_tags=$(echo $sorted_tags | jq ".[:-$images_to_keep]")
echo "For repository: $repository"
echo "Found tags: $sorted_tags"
echo "Will delete tags: $delete_tags"
echo $delete_tags | jq -r '.[]' | while read tag; do
az acr repository delete --yes --name $acr --image $repository:$tag
done
done
If you want to include this script in an Azure DevOps pipeline, that is possible as well. Due to the way Azure DevOps handles variables and character escaping, a slightly different script is needed.
name: clean_registry
schedules:
- cron: '0 2 1 * *'
displayName: Monthly Registry Clean
branches:
include:
- main
always: true
- name: acr_name
value: <name of your ACR>
- name: subscription
value: <name of subscription that contains your ACR>
- name: images_to_keep
value: 2
stages:
- stage: CleanRegistry
displayName: Clean ACR
jobs:
- job: CleanACR
displayName: Clean ACR
steps:
- script: curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
displayName: Install Azure CLI
- script: sudo apt install -y jq
displayName: Install jq
- task: AzureCLI@2
displayName: Clean ACR
inputs:
azureSubscription: $(subscription)
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
repositories=`az acr repository list --name $(acr_name)`
sas_repositories=`echo $repositories | jq -c 'map(select(.|startswith("viya-4-x64_oci_linux_2-docker")))'`
echo $sas_repositories | jq -r '.[]' | while read repository; do
tags=`az acr repository show-tags --name $(acr_name) --repository $repository`
sorted_tags=`echo $tags | jq 'sort_by(.|split("\\\.|-";"")|map(tonumber))'`
delete_tags=`echo $sorted_tags | jq '.[:-$(images_to_keep)]'`
echo "For repository: $repository"
echo "Found tags: $sorted_tags"
echo "Will delete tags: $delete_tags"
echo $delete_tags | jq -r '.[]' | while read tag; do
az acr repository delete --yes --name $(acr_name) --image $repository:$tag
done
done
I hope this script helps you get a good start in 2024, nice and clean!
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.