我们有一个Azure Devops版本,它使用以下PowerShell脚本来构建我们的存储库,打包并将其部署到我们的Azure Batch服务:
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
问题是,这似乎无法“清理”应用程序包的旧版本,而且我没有在API文档中看到这样做的方法。因此,每隔几周我会收到这种错误消息:
已经允许的最大应用程序包数 为指定的应用程序添加了。
如何更改此脚本以部署应用程序并清理旧版本,以使部署不会出现周期性故障?
答案 0 :(得分: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://docs.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://docs.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.
答案 1 :(得分:0)
我们使用Azure CLI(在PowerShell中),因此语法与您的语法有些不同。这是我们的脚本,可满足您的要求(以及更多)。关键是在某些调用上使用参数--query
,以便能够计算应用程序版本数并获取最早的版本:
$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!"