VSTS - 在发布期间删除以前的部署

时间:2017-08-21 10:31:27

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

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

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

到目前为止,我尝试了一些事情,例如使用内联PowerShell命令 Remove-AzureRmResourceGroupDeployment ,在资源部署阶段删除 Microsoft.Resouces / deployments 类型的资源组但非他们工作了。

  

[error]创建部署'azuredeploy-2017721-715'将超过'800'的配额。当前部署计数为“800”,请在创建新部署之前删除部分部署。有关使用详情,请参阅https://aka.ms/arm-deploy

2 个答案:

答案 0 :(得分:0)

使用Azure PowerShell任务。它负责您的身份验证 - 无需致电Login-AzureRmAccount

答案 1 :(得分:0)

这是一个脚本,允许您以并行方式删除此类部署。您还可以将其用于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..."