【问题标题】:VSTS - Deleting previous deployments during a releaseVSTS - 在发布期间删除以前的部署
【发布时间】:2017-08-21 10:31:27
【问题描述】:

我正在为一个 Azure 项目工作,该项目只能使用 Visual Studio CI 中的 ARM 模板进行部署,并且我们对 Azure 门户只有读取权限。

目前我收到以下错误,无法发布。我也无法从 Portal 中删除部署,因为我只有配置构建和发布阶段的权限,我想知道是否可以创建任何阶段来删除以前的部署。

到目前为止,我尝试了几件事,例如使用内联 PowerShell 命令 Remove-AzureRmResourceGroupDeployment ,在资源部署阶段之前删除类型为 Microsoft.Resouces/deployments 的资源组,但不是其中有工作。

[错误]创建部署“azuredeploy-2017721-715”将超过“800”的配额。当前部署计数为“800”,请在创建新部署之前删除一些部署。使用详情请见https://aka.ms/arm-deploy。

【问题讨论】:

标签: powershell azure continuous-integration azure-devops azure-pipelines-release-pipeline


【解决方案1】:

这是一个允许您以并行方式删除此类部署的脚本。您还可以将其用于 Azure DevOps 中的 Azure PowerShell 任务。如果您有关于身份验证的问题,请查看此处:Azure credentials have not been set up or have expired, please run Connect-AzAccount

Param(
    [string] 
    [Parameter(Mandatory = $true)]
    $subscriptionId,

    [string] 
    [Parameter(Mandatory = $true)]
    $tenantId,

    [string] 
    [Parameter(Mandatory = $true)]
    $resourceGroupName,

    [int] 
    [Parameter(Mandatory = $true)]
    $numberOfDeploymentsToKeep,

    [int] 
    [Parameter(Mandatory = $true)]
    $batchSize
)

try {
    $c = Get-AzContext
}
catch {
    $c = $null
}

if (!$c -or !$c.Account) {
    Connect-AzAccount -Subscription $subscriptionId -Tenant $tenantId
} else {
    Select-AzSubscription -Subscription $subscriptionId -Tenant $tenantId
}

# ----------------------------------
# Get Deployments
# ----------------------------------

#$dateBeforeDeleteDeployments = Get-Date -Year 2018 -Month 06 -Day 30
#$deploymentsToDelete = Get-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName | Where-Object { $_.Timestamp -le $dateBeforeDeleteDeployments }

$currentDeployments = Get-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName

$currentNumberOfDeployments = ($currentDeployments | Measure-Object).Count
$numberOfDeploymentsToRemove = $currentNumberOfDeployments - $numberOfDeploymentsToKeep

if ($numberOfDeploymentsToRemove -lt 0) {
    throw "Number of deployments to remove is < 0..."
} 
if ($numberOfDeploymentsToRemove -eq 0) {
    Write-Host "Number of deployments to remove is 0..."
    return
}

Write-Host "Number of Deployments to remove: '$numberOfDeploymentsToRemove'..."

$deploymentsToDelete = $currentDeployments | Sort-Object -Property Timestamp | Select-Object -First $numberOfDeploymentsToRemove

$deploymentsToDelete | ForEach-Object {$i=0; $j=0; $deploymentsToDeleteBatched=@{}} {
    if($i -ne $batchSize -and $deploymentsToDeleteBatched["Batch $j"]) { 
        $deploymentsToDeleteBatched["Batch $j"]+=$_
        $i+=1 
    }
    else {
        $i=1
        $j+=1
        $deploymentsToDeleteBatched["Batch $j"]=@($_)
    }
}

Write-Host "Created $($deploymentsToDeleteBatched.Count) batches..."

# ----------------------------------
# Execute deletion in parallel
# ----------------------------------
$jobNames = @()
foreach ($batchkey in $deploymentsToDeleteBatched.Keys) {
    $deploymentsToDeleteBatch = $deploymentsToDeleteBatched.$batchkey

    $logic = {
        Param(
            [object] 
            [Parameter(Mandatory = $true)]
            $ctx,

            [object] 
            [Parameter(Mandatory = $true)]
            $deploymentsToDeleteBatch,

            [string] 
            [Parameter(Mandatory = $true)]
            $resourceGroupName
        )

        foreach ($deploymentToDelete in $deploymentsToDeleteBatch) {
            $deploymentName = $deploymentToDelete.DeploymentName
            Remove-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -Name $deploymentName -DefaultProfile $ctx -ErrorAction Stop
            Write-Host "Deleted Deployment '$deploymentName' from '$($deploymentToDelete.Timestamp)'..."
        }  
    }

    $jobName = ([System.Guid]::NewGuid()).Guid
    $jobNames += $jobName
    $jobObject = Start-Job $logic -Name $jobName -ArgumentList (Get-AzContext), $deploymentsToDeleteBatch, $resourceGroupName
}

while (Get-Job -State "Running") {
    Write-Host "---------------------------------------------------------------"
    Write-Host "Jobs still running..."
    Get-Job | Format-Table
    Write-Host "---------------------------------------------------------------"
    Start-Sleep -Seconds 10
}

Write-Host "Jobs completed, getting output..."
Write-Host "---------------------------------------------------------------"

foreach ($jobName in $jobNames) {
    Write-Host "Output of Job '$jobName'..."
    Receive-Job -Name $jobName
    Write-Host "---------------------------------------------------------------"
}

Write-Host "Done..."

【讨论】:

    【解决方案2】:

    使用 Azure PowerShell 任务。它会为您处理身份验证——无需调用Login-AzureRmAccount。

    【讨论】:

    • 我之前使用 PowerShell++ 任务内联编写脚本,但没有成功。但是,将其放入脚本文件并使用 Azure PowerShell 任务确实有效。我只是使用Get-AzureRmResourceGroupDeployment -ResourceGroupName MyResourceGroupName | Remove-AzureRmResourceGroupDeployment 现在它正在删除所有内容。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    相关资源
    最近更新 更多