-
Notifications
You must be signed in to change notification settings - Fork 0
/
acr-purge.ps1
99 lines (75 loc) · 3.48 KB
/
acr-purge.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
[CmdletBinding()]
Param(
#Flag for bypassing Azure CLI Login
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[Switch] $BypassLogin = $false,
#Service Prinicipal Id for Azure
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String] $ServicePrincipalId,
#Service Prinicial key for Azure
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String] $ServicePrincipalPassword,
#Tenant ID for Azure
[Parameter (Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String] $ServicePrincipalTenant,
#Azure Subscription Name
[Parameter (Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String] $Subscription,
#Azure Container Registry
[Parameter (Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String] $ContainerRegistry,
# Number of last images to keep in repository, default: 5
[Parameter (Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String] $LastImagesToKeep = "5",
# Days older than will be deleted
[Parameter (Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String] $DeleteDaysLimit
)
Write-Host "Azure Container Registry purge is started"
if (-Not $BypassLogin) {
if ($ServicePrincipalId -and $ServicePrincipalPassword -and $ServicePrincipalTenant) {
Write-Host "Connecting Azure"
az login --service-principal -u $ServicePrincipalId -p $ServicePrincipalPassword --tenant $ServicePrincipalTenant
}
Write-Host "Azure Auth. credentials are missing!"
exit
}
if ($Subscription) {
Write-Host "Setting subscription to: $Subscription"
az account set --subscription $Subscription
}
Write-Host "Checking container registry: $ContainerRegistry"
$RepoList = az acr repository list --name $ContainerRegistry --output table
for($index=2; $index -lt $RepoList.length; $index++) { #First 2 element are column definition, so skip them
$RepositoryName = $RepoList[$index]
Write-Host "Checking repository: $RepositoryName"
if ($DeleteDaysLimit) { #Delete images by date
$Images = az acr repository show-tags --name $ContainerRegistry --repository $RepositoryName --detail --orderby time_desc --output table
for($item=2; $item -lt $Images.length; $item++){ #First 2 element are column definition, so skip them
$ImageDetailRow = $Images[$item].ToString().Split(' ')
$ImageCreatedDate = $ImageDetailRow[0]
$ImageCreatedDate = [datetime]::Parse($ImageCreatedDate)
if($ImageCreatedDate -lt $((Get-Date).AddDays(-$DeleteDaysLimit))) {
$ImageName = $RepositoryName + ':' + $ImageDetailRow[3]
az acr repository delete --name $ContainerRegistry --image $ImageName --yes
Write-Host "Deleted image: $ImageName"
}
}
} Else { #Delete images by count
$RepositoryTags = az acr repository show-tags --name $ContainerRegistry --repository $RepositoryName --orderby time_desc --output table
for($item=2+$LastImagesToKeep; $item -lt $RepositoryTags.length; $item++) { #First 2 element are column definition, so skip them
$ImageName = $RepositoryName + ":" + $RepositoryTags[$item]
az acr repository delete --name $ContainerRegistry --image $ImageName --yes
Write-Host "Deleted image: $ImageName"
}
}
}
Write-Host "Azure Container Registry purge is finished"