generated from arcus-azure/arcus.github.template
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added optional DaysToKeep parameter to Save-AzDevOpsBuild (#383)
Co-authored-by: Stijn Moreels <[email protected]> Co-authored-by: Pim Simons <[email protected]>
- Loading branch information
1 parent
6207b8b
commit 2baf459
Showing
5 changed files
with
101 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 19 additions & 10 deletions
29
src/Arcus.Scripting.DevOps/Scripts/Save-AzDevOpsBuild.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,35 @@ | ||
param( | ||
[Parameter(Mandatory = $true)][string] $ProjectId = $(throw "ProjectId is required"), | ||
[Parameter(Mandatory = $true)][string] $BuildId = $(throw "BuildId is required") | ||
[Parameter(Mandatory = $true)][string] $BuildId = $(throw "BuildId is required"), | ||
[Parameter(Mandatory = $false)][int] $DaysToKeep | ||
) | ||
|
||
$retentionPayload = @{ | ||
keepforever='true' | ||
if ($DaysToKeep -eq '' -Or $DaysToKeep -eq 0) { | ||
$daysValid = 99999 | ||
} else { | ||
$daysValid = $DaysToKeep | ||
} | ||
|
||
$requestBody = $retentionPayload | ConvertTo-Json -Depth 1 -Compress | ||
$retentionPayload = @{ daysValid = $daysValid; definitionId = $env:SYSTEM_DEFINITIONID; ownerId = "User:$env:BUILD_REQUESTEDFORID"; protectPipeline = $true; runId = $BuildId }; | ||
$requestBody = ConvertTo-Json @($retentionPayload); | ||
|
||
$collectionUri = $env:SYSTEM_COLLECTIONURI | ||
if ($collectionUri.EndsWith('/') -eq $false) { | ||
$collectionUri = $collectionUri + '/' | ||
$collectionUri = $collectionUri + '/' | ||
} | ||
|
||
$requestUri = "$collectionUri" + "$ProjectId/_apis/build/builds/" + $BuildId + "?api-version=6.0" | ||
$urlEncodedProjectId = [uri]::EscapeDataString($ProjectId) | ||
$requestUri = "$collectionUri" + "$urlEncodedProjectId/_apis/build/retention/leases?api-version=7.0" | ||
|
||
Write-Verbose "Saving Azure DevOps build with build ID $BuildId in project $ProjectId by posting '$requestBody' to '$requestUri'..." | ||
$response = Invoke-WebRequest -Uri $requestUri -Method Patch -Body $requestBody -ContentType "application/json" -Headers @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } | ||
Write-Verbose "Saving Azure DevOps build for $daysValid days with build ID $BuildId in project $ProjectId by posting '$requestBody' to '$requestUri'..." | ||
$response = Invoke-WebRequest -Uri $requestUri -Method Post -Body $requestBody -ContentType "application/json" -Headers @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } | ||
|
||
if ($response.StatusCode -ne 200) { | ||
throw "Unable to retain Azure DevOps build indefinetely with build ID $BuildId in project $ProjectId. API request returned statuscode $($response.StatusCode)" | ||
throw "Unable to retain Azure DevOps build with build ID $BuildId in project $ProjectId. API request returned statuscode $($response.StatusCode)" | ||
} | ||
|
||
Write-Host "Saved Azure DevOps build with build ID $BuildId in project $ProjectId" -ForegroundColor Green | ||
if ($DaysToKeep -eq '') { | ||
Write-Host "Saved Azure DevOps build indefinitely with build ID $BuildId in project $ProjectId" -ForegroundColor Green | ||
} else { | ||
Write-Host "Saved Azure DevOps build for $DaysToKeep days with build ID $BuildId in project $ProjectId" -ForegroundColor Green | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters