pipeline file changes

This commit is contained in:
Brett Hewitson 2022-12-29 13:01:20 +10:00
parent 942fdb66d3
commit 7284e7ec55
4 changed files with 27 additions and 210 deletions

View file

@ -35,6 +35,11 @@ stages:
environment: github-servermanager-${{ lower(parameters.DeploymentType) }}
workspace:
clean: all
variables:
- name: NewVersionBuild
value: $(VersionBuild)
- name: NewVersionRevision
value: $(VersionRevision)
strategy:
runOnce:
@ -162,22 +167,31 @@ stages:
workingDirectory: '$(Build.SourcesDirectory)'
- ${{ if eq(parameters.DeploymentType, 'Beta') }}:
- template: update-pipeline-variable.yml@self
parameters:
VariableName: 'VersionRevision'
VariableValue: $(VersionRevision) + 1
- powershell: |
[int]$oldValue = ${{variables.NewVersionRevision}}
[int]$newValue = $oldValue + 1
Write-Host "##vso[task.setvariable variable=NewVersionRevision;]$newValue"
displayName: Set New Pipeline Version Variable Values
- ${{ if eq(parameters.DeploymentType, 'Production') }}:
- template: update-pipeline-variable.yml@self
parameters:
VariableName: 'VersionBuild'
VariableValue: $(VersionBuild) + 1
- powershell: |
[int]$oldValue = ${{variables.NewVersionBuild}}
[int]$newValue = $oldValue + 1
Write-Host "##vso[task.setvariable variable=NewVersionBuild;]$newValue"
- ${{ if eq(parameters.DeploymentType, 'Production') }}:
- template: update-pipeline-variable.yml@self
parameters:
VariableName: 'VersionRevision'
VariableValue: 1
[int]$newValue = 1
Write-Host "##vso[task.setvariable variable=NewVersionRevision;]$newValue"
displayName: Set New Pipeline Version Variable Values
- template: update-pipeline-variable.yml@self
parameters:
VariableName: 'VersionBuild'
VariableValue: ${{variables.NewVersionBuild}}
- template: update-pipeline-variable.yml@self
parameters:
VariableName: 'VersionRevision'
VariableValue: ${{variables.NewVersionRevision}}
- template: azure-pipelines-clean-agent.yml@self
parameters:

View file

@ -1,94 +0,0 @@
Param (
[string]$CollectionUri,
[int]$AgentId,
[string]$AgentName,
[string]$AccessToken,
[string]$DebugMode = 'false'
)
Function Get-AzureDevopsAgent() {
param(
[Parameter(Mandatory = $true)] [string]$baseUri,
[Parameter(Mandatory = $true)] [string]$accessToken,
[Parameter(Mandatory = $true)] [int]$agentId,
[Parameter(Mandatory = $true)] [string]$agentName
)
try {
[Net.ServicePointManager]::SecurityProtocol = "Tls12, Tls13"
$headers = @{
Authorization = "Bearer $accessToken"
}
$uri = "$($baseUri)/distributedtask/pools?api-version=6.0"
$responsePools = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers -UseBasicParsing
foreach ($pool in $responsePools.Value) {
$uri = "$($baseUri)/distributedtask/pools/$($pool.Id)/agents?api-version=6.0&includeCapabilities=true"
$responseAgents = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers -UseBasicParsing
$agents = $responseAgents.Value.Where({$_.id -eq $agentId -and $_.name -eq $agentName})
if (!($agents) -or $agents.Count -eq 0) {
continue
}
if ($agents.Count -gt 1) {
throw "Multiple agents ($($agents.Count)) found with id: $agentId and name: $agentName"
}
return $agents.Item(0)
}
Write-Host -ForeGroundColor Yellow 'Agent NOT found'
return $null
}
catch {
Write-Host -ForeGroundColor Red 'Unhandled exception occurred during agent fetch!'
Write-Host -ForegroundColor Red $_.Exception.Message
throw
}
}
Function Output-AgentCapabilities() {
param(
[Parameter(Mandatory = $true)] [PSCustomObject]$capabilities,
[Parameter(Mandatory = $true)] [string]$capabilityType
)
[int]$count = 0
foreach ($capability in $capabilities.PSObject.Properties) {
$envName = "AgentCapabilities.$($capabilityType).$($capability.Name)".Replace('_', '.')
[System.Environment]::SetEnvironmentVariable($envName, $($capability.Value))
$count = $count + 1
}
Write-Host -ForeGroundColor Cyan "Created $count AgentCapabilities.$capabilityType environment variables"
}
$ErrorActionPreference = 'Stop'
if ($debugMode -eq "true")
{
Write-Host "##[section]Starting: DEBUG INFORMATION"
Write-Host "##[debug]CollectionUri = $CollectionUri"
Write-Host "##[debug]AgentId = $AgentId"
Write-Host "##[debug]AgentName = $AgentName"
Write-Host "##[debug]AccessToken = $AccessToken"
Write-Host "##[section]Finishing: DEBUG INFORMATION"
Write-Host ""
}
$AgentData = Get-AzureDevopsAgent -baseUri "$($CollectionUri)_apis" -accessToken $AccessToken -agentId $AgentId -agentName $AgentName
if ($AgentData) {
Output-AgentCapabilities -capabilities $AgentData.systemCapabilities -capabilityType 'System'
Output-AgentCapabilities -capabilities $AgentData.userCapabilities -capabilityType 'User'
}
Write-Host 'Done'
exit 0

Binary file not shown.

View file

@ -1,103 +0,0 @@
Param (
[string]$repositoryUrl = $env:SYSTEM_COLLECTIONURI,
[string]$repositoryName = $env:SYSTEM_TEAMPROJECT,
[string]$definitionsFilter = $env:BUILD_DEFINITIONNAME,
[string]$variableName = 'VersionRevision',
[string]$authorization = $env:SYSTEM_ACCESSTOKEN,
[string]$debugMode = $env:SYSTEM_DEBUG
)
$ErrorActionPreference = 'Stop'
if ($debugMode -eq 'true') {
Write-Host '##[section]Starting: DEBUG INFORMATION'
Write-Host "##[debug]repositoryUrl = $repositoryUrl"
Write-Host "##[debug]repositoryName = $repositoryName"
Write-Host "##[debug]definitionsFilter = $definitionsFilter"
Write-Host "##[debug]variableName = $variableName"
Write-Host '##[section]Finishing: DEBUG INFORMATION'
Write-Host ''
}
$null = [Reflection.Assembly]::LoadFile("$pwd\Newtonsoft.Json.dll")
# Remove the branch name '[<branch>]' from the definitions filter.
# This was we can check the build numbers in sync acrosss the branches
$defFilter = $definitionsFilter
$defFilterParts = $defFilter.Split("(", [StringSplitOptions]'RemoveEmptyEntries')
$defFilter = $defFilterParts[0].Trim() + "*"
if ($debugMode -eq 'true') {
Write-Host "##[debug]defFilter = $defFilter"
}
# Get the value from the environment variable (if exists)
$variableValue = get-content env:$variableName -ErrorAction SilentlyContinue
if ($debugMode -eq 'true') {
Write-Host "##[debug]variableValue = $variableValue"
}
if($repositoryUrl.EndsWith('/')) {
$repositoryUrl = $repositoryUrl.TrimEnd('/')
}
# Get an overview of all build definitions in this team project
$definitionsOverviewUrl = "$repositoryUrl/$repositoryName/_apis/build/Definitions"
if ($debugMode -eq 'true') {
Write-Host "##[debug]definitionsOverviewUrl = $definitionsOverviewUrl"
}
$headers = @{
'Authorization' = $authorization;
'Accept' = 'application/json; api-version=5.1'
}
$definitionsOverviewResponse = Invoke-WebRequest -Uri $definitionsOverviewUrl -Method Get -ContentType 'application/json' -Headers $headers
$definitionsOverview = (ConvertFrom-Json -InputObject $definitionsOverviewResponse.Content).value
# Process all builds that have <definitionsFilter> in their name
foreach($definitionEntry in ($definitionsOverview | Where-Object { $_.name -like $defFilter })) {
$buildDefinitionUrl = $definitionEntry.url
if ($debugMode -eq 'true') {
Write-Host "##[debug]buildDefinitionUrl = $buildDefinitionUrl"
}
$headers = @{
'Authorization' = $authorization;
'Accept' = 'application/json; api-version=5.1'
}
$buildDefinitionResponse = Invoke-WebRequest -Uri $buildDefinitionUrl -Method Get -ContentType 'application/json' -Headers $headers
$buildDefinition = [Newtonsoft.Json.JsonConvert]::DeserializeObject($buildDefinitionresponse.Content)
# If the build has the variable, update it.
if($buildDefinition.variables.$variableName) {
[int]$value = 0
# Check if the environment variable value was set
if($variableValue -eq $null -or $variableValue.Trim() -eq '') {
# use the value from the build definition
$value = $buildDefinition.variables.$variableName.value
Write-Host 'Variable value used from build definition.'
} else {
$value = $variableValue
Write-Host 'Variable value used from environment variable.'
}
$buildDefinition.variables.$variableName.value = $value + 1
[int]$newValue = $buildDefinition.variables.$variableName.value
Write-Output -InputObject "Updating ""$($definitionEntry.name)"" $variableName from $($value) to $($newValue)..."
$serialized = [Newtonsoft.Json.JsonConvert]::SerializeObject($buildDefinition)
$postData = [Text.Encoding]::UTF8.GetBytes($serialized)
$headers = @{
'Authorization' = $authorization;
'Accept' = 'application/json; api-version=5.1'
}
$response = Invoke-WebRequest -UseDefaultCredentials -Uri $buildDefinitionUrl -Method Put -ContentType 'application/json' -Headers $headers -Body $postData
Write-Host "Response Status = $($response.StatusDescription)"
}
}
Write-Host 'Done'
exit 0