2

We have an Azure Devops release that builds our repo, packages it, and deploys it to our Azure Batch service with the following PowerShell script:

New-AzureRmBatchApplicationPackage -AccountName "$(BatchAccountName)" -ResourceGroupName "$(ResourceGroupName)" -ApplicationId "$(ApplicationId)" -ApplicationVersion "$(Build.BuildNumber)-$(Release.ReleaseId)" -Format zip -FilePath "$(System.DefaultWorkingDirectory)/_artifact/artifact/bin/theapplication.zip"

Set-AzureRmBatchApplication -AccountName "$(BatchAccountName)" -ResourceGroupName "$(ResourceGroupName)" -ApplicationId "$(ApplicationId)" -DefaultVersion "$(Build.BuildNumber)-$(Release.ReleaseId)"

$Context = Get-AzureRmBatchAccount -AccountName "$(BatchAccountName)" -ResourceGroupName "$(ResourceGroupName)"

Get-AzureBatchComputeNode -PoolId "$(PoolId)" -BatchContext $Context | Restart-AzureBatchComputeNode -BatchContext $Context

The problem is, this doesn't seem to "clean up" old versions of the application package, and I don't see a way in the API documentation to do so. So every few weeks I get this sort of error message:

The maximum allowed number of application packages have already been added for the specified application.

How can I change this script to deploy the application and clean up old versions so I don't have periodic failures in my deployment?

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254

3 Answers3

0

Depending on if you want to keep any history and how you version you should be able to call Get-AzBatchApplicationPackage to list application packages associated with an Application (https://learn.microsoft.com/en-us/powershell/module/az.batch/get-azbatchapplicationpackage?view=azps-1.7.0). Note that ApplicationVersion is actually optional for this call.

Then Remove-AzBatchApplicationPackage (https://learn.microsoft.com/en-us/powershell/module/Az.Batch/Remove-AzBatchApplicationPackage?view=azps-1.7.0) on any application packages you do not want to keep around, whether that is all application packages other than the one you added or some function on the version is up to you.

brklein
  • 310
  • 1
  • 6
  • The documentation does not indicate that ApplicationVersion is optional... it indicates that the parameter name is optional, as far as I can tell. I tried not including it and I was prompted for it; trying to pass on it generated an error. – Jeremy Holovacs Apr 23 '19 at 17:11
  • That is my mistake as I assumed you were on the latest release of the Batch powershell module(listed as preview). For the current bundled release, I believe the list is a property of GetApplication (https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.commands.batch.models.psapplication?view=azurerm-ps) – brklein Apr 23 '19 at 19:20
  • 1
    Get-AzBatchApplication brings back an empty object for AccountPackages? – Jeremy Holovacs Apr 23 '19 at 20:03
  • I believe that is a bug with the toString of the returned object. I was able to view them with: `PS C:\WINDOWS\system32> $tmp = Get-AzBatchApplication -AccountName "sdktest2" -ResourceGroupName "sdktest" -ApplicationId "new"` `PS C:\WINDOWS\system32> $tmp.ApplicationPackages` – brklein Apr 23 '19 at 20:38
0

We use Azure CLI (in PowerShell), so the syntax is a bit different than yours. This is our script to achieve what you are asking for (and more). The key is to use the argument --query on some of the calls, to be able to count number of application versions, and get the oldest version:

$Environment = "dev"
$ResourceGroup="rg-name-$Environment"
$BatchAccount="batchname$Environment"
$BatchApplicationName="MyApplication"
$BatchApplicationVersion="1.2.3.4"
$SetAsDefaultVersion=$true
$MaxNumOfAppVersions=39

Write-Host "Uploading new version of application to batch service..."
$filename = "folder/file.zip"

$numOfVersions = az batch application package list --resource-group "$ResourceGroup" --name "$BatchAccount" --application-name "$BatchApplicationName" --query "length([].{name:name})"
Write-Host "There are currently $numOfVersions versions of the Application in the Batch Account"
if ($numOfVersions -gt $MaxNumOfAppVersions) {
  # Do not automatically delete old version in the prod environment:
  if (($Environment.ToLower() -eq "prod")) {
    throw "The Batch Application $BatchApplicationName contains too many versions. Delete the oldest, after you have made sure that it is not in use in production."
  }

  $oldestAppVersion = az batch application package list --resource-group "$ResourceGroup" --name "$BatchAccount" --application-name "$BatchApplicationName" --query "sort_by([].{name:name, lastActivationTime:lastActivationTime}, &lastActivationTime)[0].{name:name}" -o tsv
  Write-Host "Deleting the oldest version (by lastActivationTime) of the Application, $oldestAppVersion, to avoid the max limit"
  az batch application package delete --resource-group "$ResourceGroup" --name "$BatchAccount" --application-name "$BatchApplicationName" --version-name $oldestAppVersion --yes
}

Write-Host "Uploading $filename..."
az batch application package create --resource-group "$ResourceGroup" --name "$BatchAccount" --application-name "$BatchApplicationName" --package-file "$filename" --version-name "$BatchApplicationVersion"

if ($SetAsDefaultVersion) {
  Write-Host "Setting this new version $BatchApplicationVersion as the default version..."
  az batch application set --resource-group "$ResourceGroup" --name "$BatchAccount" --application-name "$BatchApplicationName" --default-version "$BatchApplicationVersion"
}

Write-Host "All done!"
Eirik W
  • 3,086
  • 26
  • 29
0

I know the question is old, but I wanted to point out that your are sending this parameter with a variable name, which means it'll create a new app version every time:

-ApplicationVersion "$(Build.BuildNumber)-$(Release.ReleaseId)"

Also, as a temporary fix, instead of using Azure PowerShell commands, you can go to Azure and delete the packages listed under you app name:

enter image description here

Luis Hernandez
  • 453
  • 5
  • 8