From 78e1c19ea9d4062e3202511292bc78468e4390c6 Mon Sep 17 00:00:00 2001 From: Mitch Denny Date: Wed, 24 Mar 2021 16:15:00 +1100 Subject: [PATCH 1/4] Add lease to runs. --- eng/common/scripts/Add-RetentionLease.ps1 | 87 +++++++++++++++++++++++ eng/common/scripts/Invoke-DevOpsAPI.ps1 | 68 ++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 eng/common/scripts/Add-RetentionLease.ps1 diff --git a/eng/common/scripts/Add-RetentionLease.ps1 b/eng/common/scripts/Add-RetentionLease.ps1 new file mode 100644 index 0000000000000..0e1723dd07551 --- /dev/null +++ b/eng/common/scripts/Add-RetentionLease.ps1 @@ -0,0 +1,87 @@ +[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)" + +#Add-RetentionLease -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -DaysValid $DaysValid -Base64AuthToken $Base64AuthToken + +# if ($CancelPreviousBuilds) +# { +# try { +# $queuedBuilds = Get-DevOpsBuilds -BranchName "refs/heads/$SourceBranch" -Definitions $DefinitionId ` +# -StatusFilter "inProgress, notStarted" -Base64EncodedAuthToken $Base64EncodedAuthToken + +# if ($queuedBuilds.count -eq 0) { +# LogDebug "There is no previous build still inprogress or about to start." +# } + +# foreach ($build in $queuedBuilds.Value) { +# $buildID = $build.id +# LogDebug "Canceling build [ $($build._links.web.href) ]" +# Update-DevOpsBuild -BuildId $buildID -Status "cancelling" -Base64EncodedAuthToken $Base64EncodedAuthToken +# } +# } +# catch { +# LogError "Call to DevOps API failed with exception:`n$_" +# exit 1 +# } +# } + +# try { +# $resp = Start-DevOpsBuild -SourceBranch $SourceBranch -DefinitionId $DefinitionId -Base64EncodedAuthToken $Base64EncodedAuthToken +# } +# catch { +# LogError "Start-DevOpsBuild failed with exception:`n$_" +# exit 1 +# } + +# LogDebug "Pipeline [ $($resp.definition.name) ] queued at [ $($resp._links.web.href) ]" + +# if ($VsoQueuedPipelines) { +# $enVarValue = [System.Environment]::GetEnvironmentVariable($VsoQueuedPipelines) +# $QueuedPipelineLinks = if ($enVarValue) { +# "$enVarValue
[$($resp.definition.name)]($($resp._links.web.href))" +# }else { +# "[$($resp.definition.name)]($($resp._links.web.href))" +# } +# $QueuedPipelineLinks +# Write-Host "##vso[task.setvariable variable=$VsoQueuedPipelines]$QueuedPipelineLinks" +# } \ 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 From d70ddd1e75bc27bb1674e6c4b8ba59e1f0798107 Mon Sep 17 00:00:00 2001 From: Mitch Denny Date: Wed, 24 Mar 2021 16:28:54 +1100 Subject: [PATCH 2/4] Add YAML file. --- .../pipelines/templates/steps/retain-run.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 eng/common/pipelines/templates/steps/retain-run.yml 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..f1a21071209bc --- /dev/null +++ b/eng/common/pipelines/templates/steps/retain-run.yml @@ -0,0 +1,16 @@ +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 365 + -Base64EncodedAuthToken $(System.AccessToken) + -Debug \ No newline at end of file From 307696002b40fb5fa78f9bbbc4a8c86fefa52ed3 Mon Sep 17 00:00:00 2001 From: Mitch Denny Date: Wed, 24 Mar 2021 16:29:21 +1100 Subject: [PATCH 3/4] Remove commented out code. --- eng/common/scripts/Add-RetentionLease.ps1 | 47 +---------------------- 1 file changed, 1 insertion(+), 46 deletions(-) diff --git a/eng/common/scripts/Add-RetentionLease.ps1 b/eng/common/scripts/Add-RetentionLease.ps1 index 0e1723dd07551..c368b255436aa 100644 --- a/eng/common/scripts/Add-RetentionLease.ps1 +++ b/eng/common/scripts/Add-RetentionLease.ps1 @@ -39,49 +39,4 @@ if ($existingLeases.count -ne 0) { 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)" - -#Add-RetentionLease -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -DaysValid $DaysValid -Base64AuthToken $Base64AuthToken - -# if ($CancelPreviousBuilds) -# { -# try { -# $queuedBuilds = Get-DevOpsBuilds -BranchName "refs/heads/$SourceBranch" -Definitions $DefinitionId ` -# -StatusFilter "inProgress, notStarted" -Base64EncodedAuthToken $Base64EncodedAuthToken - -# if ($queuedBuilds.count -eq 0) { -# LogDebug "There is no previous build still inprogress or about to start." -# } - -# foreach ($build in $queuedBuilds.Value) { -# $buildID = $build.id -# LogDebug "Canceling build [ $($build._links.web.href) ]" -# Update-DevOpsBuild -BuildId $buildID -Status "cancelling" -Base64EncodedAuthToken $Base64EncodedAuthToken -# } -# } -# catch { -# LogError "Call to DevOps API failed with exception:`n$_" -# exit 1 -# } -# } - -# try { -# $resp = Start-DevOpsBuild -SourceBranch $SourceBranch -DefinitionId $DefinitionId -Base64EncodedAuthToken $Base64EncodedAuthToken -# } -# catch { -# LogError "Start-DevOpsBuild failed with exception:`n$_" -# exit 1 -# } - -# LogDebug "Pipeline [ $($resp.definition.name) ] queued at [ $($resp._links.web.href) ]" - -# if ($VsoQueuedPipelines) { -# $enVarValue = [System.Environment]::GetEnvironmentVariable($VsoQueuedPipelines) -# $QueuedPipelineLinks = if ($enVarValue) { -# "$enVarValue
[$($resp.definition.name)]($($resp._links.web.href))" -# }else { -# "[$($resp.definition.name)]($($resp._links.web.href))" -# } -# $QueuedPipelineLinks -# Write-Host "##vso[task.setvariable variable=$VsoQueuedPipelines]$QueuedPipelineLinks" -# } \ No newline at end of file +LogDebug "Lease ID is: $($lease.value.leaseId)" \ No newline at end of file From 985d8fd33e98150c958a5f714edfebc9ea603074 Mon Sep 17 00:00:00 2001 From: Mitch Denny Date: Fri, 9 Apr 2021 14:43:44 +1000 Subject: [PATCH 4/4] Move days valid into template with a default. --- eng/common/pipelines/templates/steps/retain-run.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/eng/common/pipelines/templates/steps/retain-run.yml b/eng/common/pipelines/templates/steps/retain-run.yml index f1a21071209bc..0ba622dcff856 100644 --- a/eng/common/pipelines/templates/steps/retain-run.yml +++ b/eng/common/pipelines/templates/steps/retain-run.yml @@ -1,3 +1,8 @@ +parameters: + - name: DaysValid + default: 365 + type: number + steps: - task: PowerShell@2 displayName: Retain pipeline run @@ -11,6 +16,6 @@ steps: -DefinitionId $(System.DefinitionId) -RunId $(Build.BuildId) -OwnerId Pipeline - -DaysValid 365 + -DaysValid ${{parameters.DaysValid}} -Base64EncodedAuthToken $(System.AccessToken) -Debug \ No newline at end of file