mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +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:
|
||||
- task: PowerShell@2
|
||||
displayName: Create AgentCapabilities Environment Variables
|
||||
env:
|
||||
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
|
||||
USER_ACCESSTOKEN: $(AuthorizationToken)
|
||||
inputs:
|
||||
targetType: filePath
|
||||
filePath: './.azure/scripts/CreateAgentCapabilitiesEnvironmentVariables.ps1'
|
||||
arguments: "-CollectionUri '$(System.CollectionUri)' -AgentId '$(Agent.Id)' -AgentName '$(Agent.Name)' -AccessToken '$env:USER_ACCESSTOKEN' -DebugMode $env:SYSTEM_DEBUG"
|
||||
targetType: inline
|
||||
script: |
|
||||
[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
|
||||
|
|
|
|||
|
|
@ -12,33 +12,35 @@ steps:
|
|||
inputs:
|
||||
targetType: inline
|
||||
script: |
|
||||
[string]$repositoryUrl = $env:SYSTEM_COLLECTIONURI
|
||||
[string]$repositoryName = $env:SYSTEM_TEAMPROJECT
|
||||
[string]$definitionId = $env:SYSTEM_DEFINITIONID
|
||||
[string]$CollectionUri = $env:SYSTEM_COLLECTIONURI
|
||||
[string]$ProjectName = $env:SYSTEM_TEAMPROJECT
|
||||
[string]$DefinitionId = $env:SYSTEM_DEFINITIONID
|
||||
[string]$variableName = '${{parameters.VariableName}}'
|
||||
[string]$variableValue = '${{parameters.VariableValue}}'
|
||||
[string]$accessToken = $env:SYSTEM_ACCESSTOKEN
|
||||
|
||||
if ($repositoryUrl.EndsWith('/')) {
|
||||
$repositoryUrl = $repositoryUrl.TrimEnd('/')
|
||||
if ($CollectionUri.EndsWith('/')) {
|
||||
$CollectionUri = $CollectionUri.TrimEnd('/')
|
||||
}
|
||||
|
||||
$headers = @{
|
||||
'Authorization' = "Bearer $accessToken"
|
||||
'Authorization' = "Bearer $AccessToken"
|
||||
}
|
||||
|
||||
[Net.ServicePointManager]::SecurityProtocol = "Tls12, Tls13"
|
||||
|
||||
# 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
|
||||
|
||||
# If the build has the variable, update it.
|
||||
if ($definitionResponse.variables.$variableName) {
|
||||
$oldValue = $definitionResponse.variables.$variableName.value
|
||||
$definitionResponse.variables.$variableName.value = $variableValue
|
||||
if ($definitionResponse.variables.$VariableName) {
|
||||
$oldValue = $definitionResponse.variables.$variableName.value
|
||||
$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
|
||||
$response = Invoke-RestMethod -Method Put -Uri $definitionUrl -Headers $headers -ContentType 'application/json' -Body $body -UseBasicParsing
|
||||
$body = ConvertTo-Json -InputObject $definitionResponse -Depth 100 -Compress
|
||||
$response = Invoke-RestMethod -Method Put -Uri $definitionUrl -Headers $headers -ContentType 'application/json' -Body $body -UseBasicParsing
|
||||
}
|
||||
pwsh: true
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue