BookmarkSubscribeRSS Feed

New Year's Resolution - Cleanup!

Started ‎01-01-2024 by
Modified ‎12-19-2023 by
Views 228

Container Image Cleanup in Azure Container Registry

 

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!

Version history
Last update:
‎12-19-2023 07:11 AM
Updated by:
Contributors

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags