File tree Expand file tree Collapse file tree 3 files changed +131
-0
lines changed
pipelines/templates/steps Expand file tree Collapse file tree 3 files changed +131
-0
lines changed Original file line number Diff line number Diff line change 1+ parameters :
2+ - name : DaysValid
3+ default : 365
4+ type : number
5+
6+ steps :
7+ - task : PowerShell@2
8+ displayName : Retain pipeline run
9+ condition : ${{ parameters.Condition }}
10+ inputs :
11+ pwsh : true
12+ filePath : $(Build.SourcesDirectory)/eng/common/scripts/Add-RetentionLease.ps1
13+ arguments : >
14+ -Organization azure-sdk
15+ -Project $(System.TeamProject)
16+ -DefinitionId $(System.DefinitionId)
17+ -RunId $(Build.BuildId)
18+ -OwnerId Pipeline
19+ -DaysValid ${{parameters.DaysValid}}
20+ -Base64EncodedAuthToken $(System.AccessToken)
21+ -Debug
Original file line number Diff line number Diff line change 1+ [CmdletBinding (SupportsShouldProcess = $true )]
2+ param (
3+ [Parameter (Mandatory = $true )]
4+ [string ]$Organization ,
5+
6+ [Parameter (Mandatory = $true )]
7+ [string ]$Project ,
8+
9+ [Parameter (Mandatory = $true )]
10+ [int ]$DefinitionId ,
11+
12+ [Parameter (Mandatory = $true )]
13+ [int ]$RunId ,
14+
15+ [Parameter (Mandatory = $true )]
16+ [string ]$OwnerId ,
17+
18+ [Parameter (Mandatory = $true )]
19+ [int ]$DaysValid ,
20+
21+ [Parameter (Mandatory = $true )]
22+ [string ]$Base64EncodedAuthToken
23+ )
24+
25+ . (Join-Path $PSScriptRoot common.ps1)
26+
27+ LogDebug " Checking for existing leases on run: $RunId "
28+ $existingLeases = Get-RetentionLeases - Organization $Organization - Project $Project - DefinitionId $DefinitionId - RunId $RunId - OwnerId $OwnerId - Base64EncodedAuthToken $Base64EncodedAuthToken
29+
30+ if ($existingLeases.count -ne 0 ) {
31+ LogDebug " Found $ ( $existingLeases.count ) leases, will delete them first."
32+
33+ foreach ($lease in $existingLeases.value ) {
34+ LogDebug " Deleting lease: $ ( $lease.leaseId ) "
35+ Delete- RetentionLease - Organization $Organization - Project $Project - LeaseId $lease.leaseId - Base64EncodedAuthToken $Base64EncodedAuthToken
36+ }
37+
38+ }
39+
40+ LogDebug " Creating new lease on run: $RunId "
41+ $lease = Add-RetentionLease - Organization $Organization - Project $Project - DefinitionId $DefinitionId - RunId $RunId - OwnerId $OwnerId - DaysValid $DaysValid - Base64EncodedAuthToken $Base64EncodedAuthToken
42+ LogDebug " Lease ID is: $ ( $lease.value.leaseId ) "
Original file line number Diff line number Diff line change @@ -90,3 +90,71 @@ function Get-DevOpsBuilds {
9090 - Headers (Get-DevOpsApiHeaders - Base64EncodedToken $Base64EncodedAuthToken ) `
9191 - MaximumRetryCount 3
9292}
93+
94+ function Delete-RetentionLease {
95+ param (
96+ $Organization ,
97+ $Project ,
98+ $LeaseId ,
99+ $Base64EncodedAuthToken
100+ )
101+
102+ $uri = " https://dev.azure.com/$Organization /$Project /_apis/build/retention/leases?ids=$LeaseId &api-version=6.0-preview.1"
103+
104+ return Invoke-RestMethod `
105+ - Method DELETE `
106+ - Uri $uri `
107+ - Headers (Get-DevOpsApiHeaders - Base64EncodedToken $Base64EncodedAuthToken ) `
108+ - MaximumRetryCount 3
109+ }
110+
111+ function Get-RetentionLeases {
112+ param (
113+ $Organization ,
114+ $Project ,
115+ $DefinitionId ,
116+ $RunId ,
117+ $OwnerId ,
118+ $Base64EncodedAuthToken
119+ )
120+
121+ $uri = " https://dev.azure.com/$Organization /$Project /_apis/build/retention/leases?ownerId=$OwnerId &definitionId=$DefinitionId &runId=$RunId &api-version=6.0-preview.1"
122+
123+ return Invoke-RestMethod `
124+ - Method GET `
125+ - Uri $uri `
126+ - Headers (Get-DevOpsApiHeaders - Base64EncodedToken $Base64EncodedAuthToken ) `
127+ - MaximumRetryCount 3
128+ }
129+
130+ function Add-RetentionLease {
131+ param (
132+ $Organization ,
133+ $Project ,
134+ $DefinitionId ,
135+ $RunId ,
136+ $OwnerId ,
137+ $DaysValid ,
138+ $Base64AuthToken
139+ )
140+
141+ $parameter = @ {}
142+ $parameter [" definitionId" ] = $DefinitionId
143+ $parameter [" runId" ] = $RunId
144+ $parameter [" ownerId" ] = $OwnerId
145+ $parameter [" daysValid" ] = $DaysValid
146+
147+
148+ $body = $parameter | ConvertTo-Json
149+
150+ $uri = " https://dev.azure.com/$Organization /$Project /_apis/build/retention/leases?api-version=6.0-preview.1"
151+
152+ return Invoke-RestMethod `
153+ - Method POST `
154+ - Body " [$body ]" `
155+ - Uri $uri `
156+ - Headers (Get-DevOpsApiHeaders - Base64EncodedToken $Base64EncodedAuthToken ) `
157+ - MaximumRetryCount 3 `
158+ - ContentType " application/json"
159+
160+ }
You can’t perform that action at this time.
0 commit comments