Move retry logic to the AzureDevOpsApi class

pull/3/head
MaksimZhukov 5 years ago
parent 93258f5088
commit b0414ba627

@ -3,14 +3,25 @@ class AzureDevOpsApi
[string] $BaseUrl
[string] $RepoOwner
[object] $AuthHeader
[UInt32] $RetryCount = 0
[UInt32] $RetryIntervalSec = 30
AzureDevOpsApi(
[string] $TeamFoundationCollectionUri,
[string] $ProjectName,
[string] $AccessToken
[string] $AccessToken,
[UInt32] $RetryCount,
[UInt32] $RetryIntervalSec
) {
$this.BaseUrl = $this.BuildBaseUrl($TeamFoundationCollectionUri, $ProjectName)
$this.AuthHeader = $this.BuildAuth($AccessToken)
if ($RetryCount -gt 0) {
$this.RetryCount = $RetryCount
}
if ($RetryIntervalSec -gt 0) {
$this.RetryIntervalSec = $RetryIntervalSec
}
}
[object] hidden BuildAuth([string]$AccessToken) {
@ -73,17 +84,21 @@ class AzureDevOpsApi
$params.Body = $Body
}
$params.RetryIntervalSec = $this.RetryIntervalSec
$params.MaximumRetryCount = $this.RetryCount
return Invoke-RestMethod @params
}
}
function Get-AzureDevOpsApi {
param (
[string] $TeamFoundationCollectionUri,
[string] $ProjectName,
[string] $AccessToken
[string] $AccessToken,
[UInt32] $RetryCount,
[UInt32] $RetryIntervalSec
)
return [AzureDevOpsApi]::New($TeamFoundationCollectionUri, $ProjectName, $AccessToken)
return [AzureDevOpsApi]::New($TeamFoundationCollectionUri, $ProjectName, $AccessToken, $RetryCount, $RetryIntervalSec)
}

@ -32,6 +32,7 @@ function Get-ToolVersions {
$versionsList = $releases.version
}
Write-Host "Versions to build: $versionsList"
return $versionsList
}
@ -41,42 +42,17 @@ function Queue-Builds {
[Parameter(Mandatory)] [string[]] $ToolVersions,
[Parameter(Mandatory)] [string] $SourceBranch,
[Parameter(Mandatory)] [string] $SourceVersion,
[Parameter(Mandatory)] [UInt32] $DefinitionId,
[Parameter(Mandatory)] [UInt32] $RetryIntervalSec,
[Parameter(Mandatory)] [UInt32] $Retries
[Parameter(Mandatory)] [UInt32] $DefinitionId
)
[BuildInfo[]]$queuedBuilds = @()
$ToolVersions | ForEach-Object {
$version = $_.Trim()
while ($Retries -gt 0)
{
try
{
Write-Host "Queue build for $version..."
$queuedBuild = $AzureDevOpsApi.QueueBuild($version, $SourceBranch, $SourceVersion, $DefinitionId)
$buildInfo = Get-BuildInfo -AzureDevOpsApi $AzureDevOpsApi -Build $queuedBuild
Write-Host "Queued build: $($buildInfo.Link)"
break
}
catch
{
Write-Host "There is an error during build starting:`n $_"
$Retries--
if ($Retries -eq 0)
{
Write-Host "Build can't be queued, please try later"
exit 1
}
Write-Host "Waiting 30 seconds before retrying. Retries left: $Retries"
Start-Sleep -Seconds $RetryIntervalSec
}
}
Write-Host "Queue build for $version..."
$queuedBuild = $AzureDevOpsApi.QueueBuild($version, $SourceBranch, $SourceVersion, $DefinitionId)
$buildInfo = Get-BuildInfo -AzureDevOpsApi $AzureDevOpsApi -Build $queuedBuild
Write-Host "Queued build: $($buildInfo.Link)"
$queuedBuilds += $buildInfo
}
@ -128,7 +104,9 @@ function Make-BuildsOutput {
$azureDevOpsApi = Get-AzureDevOpsApi -TeamFoundationCollectionUri $TeamFoundationCollectionUri `
-ProjectName $AzureDevOpsProjectName `
-AccessToken $AzureDevOpsAccessToken
-AccessToken $AzureDevOpsAccessToken `
-RetryCount $RetryCount `
-RetryIntervalSec $RetryIntervalSec
$toolVersionsList = Get-ToolVersions -ManifestLink $ManifestLink `
-RetryIntervalSec $RetryIntervalSec `
@ -139,9 +117,7 @@ $queuedBuilds = Queue-Builds -AzureDevOpsApi $azureDevOpsApi `
-ToolVersions $toolVersionsList `
-SourceBranch $SourceBranch `
-SourceVersion $SourceVersion `
-DefinitionId $DefinitionId `
-RetryIntervalSec $RetryIntervalSec `
-Retries $RetryCount
-DefinitionId $DefinitionId
if ($WaitForBuilds) {
Write-Host "`nWaiting results of builds ..."

Loading…
Cancel
Save