mirror of
https://github.com/tribufu/ServerManagers
synced 2026-06-01 09:42:39 +00:00
pipeline file changes
This commit is contained in:
parent
be2730ea23
commit
d394c57a10
2 changed files with 99 additions and 16 deletions
|
|
@ -1,10 +1,91 @@
|
||||||
|
parameters:
|
||||||
|
- name: VariableName
|
||||||
|
displayName: Variable Name
|
||||||
|
- name: VariableValue
|
||||||
|
displayName: Variable Value
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- task: PowerShell@2
|
- task: PowerShell@2
|
||||||
displayName: Create AgentCapabilities Environment Variables
|
displayName: Create AgentCapabilities Environment Variables
|
||||||
env:
|
env:
|
||||||
|
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
|
||||||
USER_ACCESSTOKEN: $(AuthorizationToken)
|
USER_ACCESSTOKEN: $(AuthorizationToken)
|
||||||
inputs:
|
inputs:
|
||||||
targetType: filePath
|
targetType: inline
|
||||||
filePath: './.azure/scripts/CreateAgentCapabilitiesEnvironmentVariables.ps1'
|
script: |
|
||||||
arguments: "-CollectionUri '$(System.CollectionUri)' -AgentId '$(Agent.Id)' -AgentName '$(Agent.Name)' -AccessToken '$env:USER_ACCESSTOKEN' -DebugMode $env:SYSTEM_DEBUG"
|
[string]$CollectionUri = $env:SYSTEM_COLLECTIONURI
|
||||||
|
[string]$AgentId = $env:AGENT_ID
|
||||||
|
[string]$AgentName = $env:AGENT_NAME
|
||||||
|
[string]$AccessToken = $env:USER_ACCESSTOKEN
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($CollectionUri.EndsWith('/')) {
|
||||||
|
$CollectionUri = $CollectionUri.TrimEnd('/')
|
||||||
|
}
|
||||||
|
|
||||||
|
$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'
|
||||||
|
}
|
||||||
pwsh: true
|
pwsh: true
|
||||||
|
|
|
||||||
|
|
@ -12,33 +12,35 @@ steps:
|
||||||
inputs:
|
inputs:
|
||||||
targetType: inline
|
targetType: inline
|
||||||
script: |
|
script: |
|
||||||
[string]$repositoryUrl = $env:SYSTEM_COLLECTIONURI
|
[string]$CollectionUri = $env:SYSTEM_COLLECTIONURI
|
||||||
[string]$repositoryName = $env:SYSTEM_TEAMPROJECT
|
[string]$ProjectName = $env:SYSTEM_TEAMPROJECT
|
||||||
[string]$definitionId = $env:SYSTEM_DEFINITIONID
|
[string]$DefinitionId = $env:SYSTEM_DEFINITIONID
|
||||||
[string]$variableName = '${{parameters.VariableName}}'
|
[string]$variableName = '${{parameters.VariableName}}'
|
||||||
[string]$variableValue = '${{parameters.VariableValue}}'
|
[string]$variableValue = '${{parameters.VariableValue}}'
|
||||||
[string]$accessToken = $env:SYSTEM_ACCESSTOKEN
|
[string]$accessToken = $env:SYSTEM_ACCESSTOKEN
|
||||||
|
|
||||||
if ($repositoryUrl.EndsWith('/')) {
|
if ($CollectionUri.EndsWith('/')) {
|
||||||
$repositoryUrl = $repositoryUrl.TrimEnd('/')
|
$CollectionUri = $CollectionUri.TrimEnd('/')
|
||||||
}
|
}
|
||||||
|
|
||||||
$headers = @{
|
$headers = @{
|
||||||
'Authorization' = "Bearer $accessToken"
|
'Authorization' = "Bearer $AccessToken"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Net.ServicePointManager]::SecurityProtocol = "Tls12, Tls13"
|
||||||
|
|
||||||
# Get an overview of the build definition
|
# Get an overview of the build definition
|
||||||
$definitionUrl = "$repositoryUrl/$repositoryName/_apis/build/Definitions/$($definitionId)?api-version=5.0"
|
$definitionUrl = "$CollectionUri/$repositoryName/_apis/build/Definitions/$($definitionId)?api-version=5.0"
|
||||||
$definitionResponse = Invoke-RestMethod -Method Get -Uri $definitionUrl -Headers $headers -UseBasicParsing
|
$definitionResponse = Invoke-RestMethod -Method Get -Uri $definitionUrl -Headers $headers -UseBasicParsing
|
||||||
|
|
||||||
# If the build has the variable, update it.
|
# If the build has the variable, update it.
|
||||||
if ($definitionResponse.variables.$variableName) {
|
if ($definitionResponse.variables.$VariableName) {
|
||||||
$oldValue = $definitionResponse.variables.$variableName.value
|
$oldValue = $definitionResponse.variables.$variableName.value
|
||||||
$definitionResponse.variables.$variableName.value = $variableValue
|
$definitionResponse.variables.$variableName.value = $VariableValue
|
||||||
|
|
||||||
Write-Output -InputObject "Updating $variableName from $($oldValue) to $($variableValue)..."
|
Write-Output -InputObject "Updating $variableName from $($oldValue) to $($variableValue)..."
|
||||||
|
|
||||||
$body = ConvertTo-Json -InputObject $definitionResponse -Depth 100 -Compress
|
$body = ConvertTo-Json -InputObject $definitionResponse -Depth 100 -Compress
|
||||||
$response = Invoke-RestMethod -Method Put -Uri $definitionUrl -Headers $headers -ContentType 'application/json' -Body $body -UseBasicParsing
|
$response = Invoke-RestMethod -Method Put -Uri $definitionUrl -Headers $headers -ContentType 'application/json' -Body $body -UseBasicParsing
|
||||||
}
|
}
|
||||||
pwsh: true
|
pwsh: true
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue