Skip to content

Instantly share code, notes, and snippets.

@bartvdbraak
Created May 31, 2023 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartvdbraak/9b20e906fdbc81522b736f580e85180d to your computer and use it in GitHub Desktop.
Save bartvdbraak/9b20e906fdbc81522b736f580e85180d to your computer and use it in GitHub Desktop.
A Bash script to move all images from one Azure Container Registry to another, with prompts for repository selection and confirmation, while retaining the order or date based on creation time.
#!/bin/bash
# Function to prompt for user input with a default value
prompt_input() {
local prompt_message="$1"
local default_value="$2"
local user_input
read -p "$prompt_message [$default_value]: " user_input
echo "${user_input:-$default_value}"
}
# Function to authenticate with Azure CLI
authenticate_azure_cli() {
echo "Authenticating with Azure CLI..."
if ! az login; then
echo "Azure CLI authentication failed. Please make sure you have Azure CLI installed and logged in."
exit 1
fi
}
# Function to set the active context to the specified Azure Container Registry
set_active_context() {
local registry_name="$1"
echo "Setting active context to the registry: $registry_name"
if ! az acr login --name "$registry_name"; then
echo "Failed to set the active context to the specified Azure Container Registry."
exit 1
fi
}
# Function to retrieve the list of repositories in the specified Azure Container Registry
get_repository_list() {
local registry_name="$1"
az acr repository list --name "$registry_name" --output tsv
}
# Function to retrieve the list of image tags for a repository in the specified Azure Container Registry
get_image_tags() {
local registry_name="$1"
local repository_name="$2"
az acr repository show-tags --name "$registry_name" --repository "$repository_name" --orderby time_asc --output tsv
}
# Function to copy an image from the source registry to the target registry
copy_image() {
local source_registry_name="$1"
local target_registry_name="$2"
local repository_name="$3"
local image_tag="$4"
echo "Copying image: $repository_name:$image_tag"
if ! az acr import --name "$target_registry_name" --source "$source_registry_name.azurecr.io/$repository_name:$image_tag" --image "$repository_name:$image_tag"; then
echo "Failed to copy the image: $repository_name:$image_tag"
fi
}
# Main script
# Prompt for source registry or use as argument
if [ -z "$1" ]; then
source_registry_name=$(prompt_input "Enter the source registry name" "")
else
source_registry_name=$1
fi
# Prompt for target registry or use as argument
if [ -z "$2" ]; then
target_registry_name=$(prompt_input "Enter the target registry name" "")
else
target_registry_name=$2
fi
# Authenticate with Azure CLI
authenticate_azure_cli
# Set context to the source registry
set_active_context "$source_registry_name"
# Get the list of repositories in the source registry
repositories=$(get_repository_list "$source_registry_name")
# Iterate through each repository
for repository in $repositories; do
echo "Processing repository: $repository"
# Get the list of image tags in ascending order based on creation time
image_tags=$(get_image_tags "$source_registry_name" "$repository")
# Prompt for confirmation to proceed with moving images
read -p "Move images from repository '$repository'? [Y/n]: " confirm
if [[ ! $confirm =~ ^[Yy]$ ]]; then
echo "Skipping repository: $repository"
continue
fi
# Display the list of images that will be moved
echo "Images to be moved:"
for tag in $image_tags; do
echo "$repository:$tag"
done
# Prompt for final confirmation before moving images
read -p "Confirm moving images from repository '$repository'? [Y/n]: " confirm
if [[ ! $confirm =~ ^[Yy]$ ]]; then
echo "Skipping repository: $repository"
continue
fi
# Copy images to the target registry
for tag in $image_tags; do
copy_image "$source_registry_name" "$target_registry_name" "$repository" "$tag"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment