Skip to content

Instantly share code, notes, and snippets.

@tillig
Created November 5, 2020 16:16
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 tillig/0f918b574c46290cd5904745984a3f32 to your computer and use it in GitHub Desktop.
Save tillig/0f918b574c46290cd5904745984a3f32 to your computer and use it in GitHub Desktop.
Copies all tagged container images from one Azure Container Registry to another.
<#
.SYNOPSIS
Copies all the container images from one Azure Container Registry to another.
.DESCRIPTION
The az CLI offers the ability to import one container registry image into an
ACR instance at a time. The artifact transfer mechanism that might be used
to bulk copy container images is in preview and is difficult to set up. This
script offers the best of all worlds - a bulk execution of the `az import`
command to bring all the images from one ACR into another.
It is assumed you are authenticated and have access to perform the list
operations on the source and import operations on the destination.
.PARAMETER Source
The name of the container registry with the images to import into the destination.
.PARAMETER Destination
The name of the container registry where images should go.
.EXAMPLE
./Copy-AzureContainerImages.ps1 `
-Source "sourceacr" `
-Destination "destinationacr"
#>
[CmdletBinding(SupportsShouldProcess = $False)]
Param(
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[ValidateLength(5, 50)]
[ValidatePattern('[a-z0-9]+')]
[string]
$Source,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[ValidateLength(5, 50)]
[ValidatePattern('[a-z0-9]+')]
[string]
$Destination
)
Begin {
Write-Verbose "Checking for az CLI."
If ($null -eq (Get-Command "az")) {
Throw "The az CLI was not found. Install here: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli"
Exit 1
}
}
Process {
Write-Progress -Activity "Copying Azure Container Registry Images" -Status "Getting images from $Source" -CurrentOperation "Retrieving repository list" -PercentComplete 1
$SourceRepoListJson = az acr repository list -n $Source
If ($LASTEXITCODE -ne 0) {
Throw "Unable to read repository list from $Source."
Exit 1
}
$SourceRepoList = $SourceRepoListJson | ConvertFrom-Json -NoEnumerate
Write-Verbose "Found $($SourceRepoList.Length) repositories in $Source."
$ImagesToImport = @()
for($i = 1; $i -le $SourceRepoList.Length; $i++) {
$RepositoryName = $SourceRepoList[$i - 1]
$PercentComplete = 25 * ($i / $SourceRepoList.Length)
Write-Progress -Activity "Copying Azure Container Registry Images" -Status "Getting image tags from $Source" -CurrentOperation "Retrieving tags for $RepositoryName" -PercentComplete $PercentComplete
$TagListJson = az acr repository show-tags -n $Source --repository $RepositoryName --orderby time_asc
If ($LASTEXITCODE -ne 0) {
Throw "Unable to read repository tags for $RepositoryName from $Source."
Exit 1
}
$TagList = $TagListJson | ConvertFrom-Json -NoEnumerate
Write-Verbose "Found $($TagList.Length) tags for $RepositoryName."
$TagList | ForEach-Object {
$Tag = $_
$ImagesToImport += "$RepositoryName`:$Tag"
}
}
for ($i = 1; $i -le $ImagesToImport.Length; $i++) {
$ImageName = $ImagesToImport[$i - 1]
$PercentComplete = 25 + 75 * ($i / $ImagesToImport.Length)
Write-Progress -Activity "Copying Azure Container Registry Images" -Status "Importing images to $Destination" -CurrentOperation "Importing $ImageName" -PercentComplete $PercentComplete
az acr import --name $Destination --source "$Source.azurecr.io/$ImageName" --image $ImageName | Out-Null
If ($LASTEXITCODE -ne 0) {
Throw "Failed to import $ImageName to $Destination."
Exit 1
}
}
Write-Progress -Activity "Copying Azure Container Registry Images" -Completed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment