diff --git a/eng/common/pipelines/templates/steps/retain-run.yml b/eng/common/pipelines/templates/steps/retain-run.yml new file mode 100644 index 0000000000000..0ba622dcff856 --- /dev/null +++ b/eng/common/pipelines/templates/steps/retain-run.yml @@ -0,0 +1,21 @@ +parameters: + - name: DaysValid + default: 365 + type: number + +steps: + - task: PowerShell@2 + displayName: Retain pipeline run + condition: ${{ parameters.Condition }} + inputs: + pwsh: true + filePath: $(Build.SourcesDirectory)/eng/common/scripts/Add-RetentionLease.ps1 + arguments: > + -Organization azure-sdk + -Project $(System.TeamProject) + -DefinitionId $(System.DefinitionId) + -RunId $(Build.BuildId) + -OwnerId Pipeline + -DaysValid ${{parameters.DaysValid}} + -Base64EncodedAuthToken $(System.AccessToken) + -Debug \ No newline at end of file diff --git a/eng/common/scripts/Add-RetentionLease.ps1 b/eng/common/scripts/Add-RetentionLease.ps1 new file mode 100644 index 0000000000000..c368b255436aa --- /dev/null +++ b/eng/common/scripts/Add-RetentionLease.ps1 @@ -0,0 +1,42 @@ +[CmdletBinding(SupportsShouldProcess = $true)] +param( + [Parameter(Mandatory = $true)] + [string]$Organization, + + [Parameter(Mandatory = $true)] + [string]$Project, + + [Parameter(Mandatory = $true)] + [int]$DefinitionId, + + [Parameter(Mandatory = $true)] + [int]$RunId, + + [Parameter(Mandatory = $true)] + [string]$OwnerId, + + [Parameter(Mandatory = $true)] + [int]$DaysValid, + + [Parameter(Mandatory = $true)] + [string]$Base64EncodedAuthToken +) + +. (Join-Path $PSScriptRoot common.ps1) + +LogDebug "Checking for existing leases on run: $RunId" +$existingLeases = Get-RetentionLeases -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -Base64EncodedAuthToken $Base64EncodedAuthToken + +if ($existingLeases.count -ne 0) { + LogDebug "Found $($existingLeases.count) leases, will delete them first." + + foreach ($lease in $existingLeases.value) { + LogDebug "Deleting lease: $($lease.leaseId)" + Delete-RetentionLease -Organization $Organization -Project $Project -LeaseId $lease.leaseId -Base64EncodedAuthToken $Base64EncodedAuthToken + } + +} + +LogDebug "Creating new lease on run: $RunId" +$lease = Add-RetentionLease -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -DaysValid $DaysValid -Base64EncodedAuthToken $Base64EncodedAuthToken +LogDebug "Lease ID is: $($lease.value.leaseId)" \ No newline at end of file diff --git a/eng/common/scripts/Invoke-DevOpsAPI.ps1 b/eng/common/scripts/Invoke-DevOpsAPI.ps1 index 024594785a3db..32c3569a3cd2f 100644 --- a/eng/common/scripts/Invoke-DevOpsAPI.ps1 +++ b/eng/common/scripts/Invoke-DevOpsAPI.ps1 @@ -90,3 +90,71 @@ function Get-DevOpsBuilds { -Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) ` -MaximumRetryCount 3 } + +function Delete-RetentionLease { + param ( + $Organization, + $Project, + $LeaseId, + $Base64EncodedAuthToken + ) + + $uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?ids=$LeaseId&api-version=6.0-preview.1" + + return Invoke-RestMethod ` + -Method DELETE ` + -Uri $uri ` + -Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) ` + -MaximumRetryCount 3 +} + +function Get-RetentionLeases { + param ( + $Organization, + $Project, + $DefinitionId, + $RunId, + $OwnerId, + $Base64EncodedAuthToken + ) + + $uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?ownerId=$OwnerId&definitionId=$DefinitionId&runId=$RunId&api-version=6.0-preview.1" + + return Invoke-RestMethod ` + -Method GET ` + -Uri $uri ` + -Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) ` + -MaximumRetryCount 3 +} + +function Add-RetentionLease { + param ( + $Organization, + $Project, + $DefinitionId, + $RunId, + $OwnerId, + $DaysValid, + $Base64AuthToken + ) + + $parameter = @{} + $parameter["definitionId"] = $DefinitionId + $parameter["runId"] = $RunId + $parameter["ownerId"] = $OwnerId + $parameter["daysValid"] = $DaysValid + + + $body = $parameter | ConvertTo-Json + + $uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?api-version=6.0-preview.1" + + return Invoke-RestMethod ` + -Method POST ` + -Body "[$body]" ` + -Uri $uri ` + -Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) ` + -MaximumRetryCount 3 ` + -ContentType "application/json" + +} \ No newline at end of file