Created
January 28, 2022 19:21
-
-
Save gillissm/6c92237757a2b528c8bee29e324903de to your computer and use it in GitHub Desktop.
Script to search for local docker images, retag them for a specific registry with a tag, then push these images to the registery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true, HelpMessage="value used to filter to the specific images for pushing")] | |
[string] | |
$imageFilter, | |
[Parameter(Mandatory = $true, HelpMessage="full registry path the images will be pushed to")] | |
[string] | |
$targetRegistry, | |
[Parameter(Mandatory = $false, HelpMessage="image tag value if not 'latest'")] | |
[string] | |
$targetTagValue='latest' | |
) | |
$imageFilter = $imageFilter.ToLowerInvariant(); | |
$targetRegistry = $targetRegistry.ToLowerInvariant(); | |
if(!$targetRegistry.EndsWith('/')) | |
{ | |
Write-Host "Target Registry does NOT end with a slash, will be added now." -ForegroundColor Red -BackgroundColor DarkGray | |
$targetRegistry = "$targetRegistry/" | |
} | |
$imgList = docker images --format "{{.Repository}}:{{.Tag}}" | Where-Object{ $_.ToLower().Contains($imageFilter) } | Sort-Object $_ | |
$imgList | |
$ans = Read-Host "Is this the correct list of images for re-tagging and pushing? (y/n)" | |
if($ans -ieq 'y'){ | |
Write-Host "Retagging images" -ForegroundColor Green -BackgroundColor DarkGray | |
$imgList = $imgList | ForEach-Object{$nn = "$targetRegistry$($($_.split('/') | Select-Object -Last 1).split(':') | Select-Object -First 1):$targetTagValue"; docker image tag $_ $nn; $nn;} | |
$imgList | |
$ans = Read-Host "Is this the retag list correct? (y/n)" | |
if($ans -ieq 'y'){ | |
$imgList | ForEach-Object{docker push $_} | |
Write-Host "Image Retaging and Pushing is completed." -ForegroundColor DarkBlue -BackgroundColor DarkGray | |
} | |
else { | |
Write-Host "Retag and Push process completed with only retagging occuring." -ForegroundColor DarkBlue -BackgroundColor DarkGray | |
} | |
} | |
else { | |
Write-Host "Retag and Push process completed with no actions taken." -ForegroundColor DarkBlue -BackgroundColor DarkGray | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment