Merge pull request #3 from actions/v-mazhuk/improve-builds-runner

Improve builds runner
pull/6/head
MaksimZhukov 5 years ago committed by GitHub
commit 7f5f554579
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,14 +3,20 @@ class AzureDevOpsApi
[string] $BaseUrl
[string] $RepoOwner
[object] $AuthHeader
[UInt32] $RetryCount
[UInt32] $RetryIntervalSec
AzureDevOpsApi(
[string] $TeamFoundationCollectionUri,
[string] $ProjectName,
[string] $AccessToken
[string] $AccessToken,
[UInt32] $RetryCount,
[UInt32] $RetryIntervalSec
) {
$this.BaseUrl = $this.BuildBaseUrl($TeamFoundationCollectionUri, $ProjectName)
$this.AuthHeader = $this.BuildAuth($AccessToken)
$this.RetryCount = $RetryCount
$this.RetryIntervalSec = $RetryIntervalSec
}
[object] hidden BuildAuth([string]$AccessToken) {
@ -73,17 +79,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 = 3,
[UInt32] $RetryIntervalSec = 60
)
return [AzureDevOpsApi]::New($TeamFoundationCollectionUri, $ProjectName, $AccessToken)
return [AzureDevOpsApi]::New($TeamFoundationCollectionUri, $ProjectName, $AccessToken, $RetryCount, $RetryIntervalSec)
}

@ -3,26 +3,51 @@ param (
[Parameter(Mandatory)] [string] $AzureDevOpsProjectName,
[Parameter(Mandatory)] [string] $AzureDevOpsAccessToken,
[Parameter(Mandatory)] [string] $SourceBranch,
[Parameter(Mandatory)] [string] $ToolVersions,
[Parameter(Mandatory)] [UInt32] $DefinitionId,
[string] $SourceVersion
[Parameter(Mandatory)] [string] $SourceVersion,
[Parameter(Mandatory)] [string] $ManifestLink,
[Parameter(Mandatory)] [bool] $WaitForBuilds,
[string] $ToolVersions,
[UInt32] $RetryIntervalSec = 60,
[UInt32] $RetryCount = 3
)
Import-Module (Join-Path $PSScriptRoot "azure-devops-api.ps1")
Import-Module (Join-Path $PSScriptRoot "build-info.ps1")
function Get-ToolVersions {
param (
[Parameter(Mandatory)] [string] $ManifestLink,
[Parameter(Mandatory)] [UInt32] $RetryIntervalSec,
[Parameter(Mandatory)] [UInt32] $Retries,
[string] $ToolVersions
)
[string[]] $versionsList = @()
if ($ToolVersions) {
$versionsList = $ToolVersions.Split(',')
} else {
Write-Host "Get the list of releases from $ManifestLink"
$releases = Invoke-RestMethod $ManifestLink -MaximumRetryCount $Retries -RetryIntervalSec $RetryIntervalSec
$versionsList = $releases.version
}
Write-Host "Versions to build: $versionsList"
return $versionsList
}
function Queue-Builds {
param (
[Parameter(Mandatory)] [AzureDevOpsApi] $AzureDevOpsApi,
[Parameter(Mandatory)] [string] $ToolVersions,
[Parameter(Mandatory)] [string[]] $ToolVersions,
[Parameter(Mandatory)] [string] $SourceBranch,
[Parameter(Mandatory)] [string] $SourceVersion,
[Parameter(Mandatory)] [string] $DefinitionId
[Parameter(Mandatory)] [UInt32] $DefinitionId
)
[BuildInfo[]]$queuedBuilds = @()
$ToolVersions.Split(',') | ForEach-Object {
$ToolVersions | ForEach-Object {
$version = $_.Trim()
Write-Host "Queue build for $version..."
$queuedBuild = $AzureDevOpsApi.QueueBuild($version, $SourceBranch, $SourceVersion, $DefinitionId)
@ -36,10 +61,9 @@ function Queue-Builds {
function Wait-Builds {
param (
[Parameter(Mandatory)] [BuildInfo[]] $Builds
[Parameter(Mandatory)] [BuildInfo[]] $Builds,
[Parameter(Mandatory)] [UInt32] $RetryIntervalSec
)
$timeoutBetweenRefreshSec = 30
do {
# If build is still running - refresh its status
@ -55,7 +79,7 @@ function Wait-Builds {
$runningBuildsCount = ($builds | Where-Object { !$_.IsFinished() }).Length
Start-Sleep -Seconds $timeoutBetweenRefreshSec
Start-Sleep -Seconds $RetryIntervalSec
} while($runningBuildsCount -gt 0)
}
@ -64,7 +88,7 @@ function Make-BuildsOutput {
[Parameter(Mandatory)] [BuildInfo[]] $Builds
)
Write-Host "Builds info:"
Write-Host "`nBuilds info:"
$builds | Format-Table -AutoSize -Property Name,Id,Status,Result,Link | Out-String -Width 10000
# Return exit code based on status of builds
@ -74,21 +98,30 @@ function Make-BuildsOutput {
$failedBuilds | ForEach-Object -Process { Write-Host "##vso[task.logissue type=error;]Name: $($_.Name); Link: $($_.Link)" }
Write-Host "##vso[task.complete result=Failed]"
} else {
Write-host "##[section] All builds have been passed successfully"
Write-host "##[section]All builds have been passed successfully"
}
}
$azureDevOpsApi = Get-AzureDevOpsApi -TeamFoundationCollectionUri $TeamFoundationCollectionUri `
-ProjectName $AzureDevOpsProjectName `
-AccessToken $AzureDevOpsAccessToken
-AccessToken $AzureDevOpsAccessToken `
-RetryCount $RetryCount `
-RetryIntervalSec $RetryIntervalSec
$toolVersionsList = Get-ToolVersions -ManifestLink $ManifestLink `
-RetryIntervalSec $RetryIntervalSec `
-Retries $RetryCount `
-ToolVersions $ToolVersions
$queuedBuilds = Queue-Builds -AzureDevOpsApi $azureDevOpsApi `
-ToolVersions $ToolVersions `
-ToolVersions $toolVersionsList `
-SourceBranch $SourceBranch `
-SourceVersion $SourceVersion `
-DefinitionId $DefinitionId
Write-Host "Waiting results of builds ..."
Wait-Builds -Builds $queuedBuilds
Make-BuildsOutput -Builds $queuedBuilds
if ($WaitForBuilds) {
Write-Host "`nWaiting results of builds ..."
Wait-Builds -Builds $queuedBuilds -RetryIntervalSec $RetryIntervalSec
Make-BuildsOutput -Builds $queuedBuilds
}

Loading…
Cancel
Save