Sync eng/common directory with azure-sdk-tools for PR 8340 (#5669)
* Changes to scripts to deal with PATs and AccessTokens * swap access and auth for add-retention-lease * AuthToken to BearerToken and remove unused Base64EncodedAuthToken from the script parameters * remove unneccsary if not null check for the mandatory parameter --------- Co-authored-by: James Suplizio <jasupliz@microsoft.com>
This commit is contained in:
Родитель
1b8d54e396
Коммит
dc64ca6eb3
|
@ -18,7 +18,10 @@ param(
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[string]$OwnerId = "azure-sdk-pipeline-automation",
|
[string]$OwnerId = "azure-sdk-pipeline-automation",
|
||||||
|
|
||||||
[Parameter(Mandatory = $false)]
|
# This script shouldn't need anything other than the $System.AccessToken from
|
||||||
|
# from the build pipeline. The retain-run.yml template doesn't run outside
|
||||||
|
# of the pipeline it's manipulating the retention leases for.
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
[string]$AccessToken = $env:DEVOPS_PAT
|
[string]$AccessToken = $env:DEVOPS_PAT
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,21 +29,20 @@ Set-StrictMode -Version 3
|
||||||
|
|
||||||
. (Join-Path $PSScriptRoot common.ps1)
|
. (Join-Path $PSScriptRoot common.ps1)
|
||||||
|
|
||||||
$encodedAuthToken = Get-Base64EncodedToken $AccessToken
|
$Base64EncodedToken = Get-Base64EncodedToken $AccessToken
|
||||||
|
|
||||||
LogDebug "Checking for existing leases on run: $RunId"
|
LogDebug "Checking for existing leases on run: $RunId"
|
||||||
$existingLeases = Get-RetentionLeases -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -Base64EncodedAuthToken $encodedAuthToken
|
$existingLeases = Get-RetentionLeases -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -Base64EncodedToken $Base64EncodedToken
|
||||||
|
|
||||||
if ($existingLeases.count -ne 0) {
|
if ($existingLeases.count -ne 0) {
|
||||||
LogDebug "Found $($existingLeases.count) leases, will delete them first."
|
LogDebug "Found $($existingLeases.count) leases, will delete them first."
|
||||||
|
|
||||||
foreach ($lease in $existingLeases.value) {
|
foreach ($lease in $existingLeases.value) {
|
||||||
LogDebug "Deleting lease: $($lease.leaseId)"
|
LogDebug "Deleting lease: $($lease.leaseId)"
|
||||||
Delete-RetentionLease -Organization $Organization -Project $Project -LeaseId $lease.leaseId -Base64EncodedAuthToken $encodedAuthToken
|
Delete-RetentionLease -Organization $Organization -Project $Project -LeaseId $lease.leaseId -Base64EncodedToken $Base64EncodedToken
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LogDebug "Creating new lease on run: $RunId"
|
LogDebug "Creating new lease on run: $RunId"
|
||||||
$lease = Add-RetentionLease -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -DaysValid $DaysValid -Base64EncodedAuthToken $encodedAuthToken
|
$lease = Add-RetentionLease -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -DaysValid $DaysValid -Base64EncodedToken $Base64EncodedToken
|
||||||
LogDebug "Lease ID is: $($lease.value.leaseId)"
|
LogDebug "Lease ID is: $($lease.value.leaseId)"
|
|
@ -16,9 +16,27 @@ function Get-Base64EncodedToken([string]$AuthToken)
|
||||||
return $encodedAuthToken
|
return $encodedAuthToken
|
||||||
}
|
}
|
||||||
|
|
||||||
function Get-DevOpsApiHeaders ($Base64EncodedToken) {
|
# The Base64EncodedToken would be from a PAT that was passed in and the header requires Basic authorization
|
||||||
$headers = @{
|
# The AccessToken would be the querying the Azure resource with the following command:
|
||||||
Authorization = "Basic $Base64EncodedToken"
|
# az account get-access-token --resource "499b84ac-1321-427f-aa17-267ca6975798" --query "accessToken" --output tsv
|
||||||
|
# The header for an AccessToken requires Bearer authorization
|
||||||
|
function Get-DevOpsApiHeaders {
|
||||||
|
param (
|
||||||
|
$Base64EncodedToken=$null,
|
||||||
|
$BearerToken=$null
|
||||||
|
)
|
||||||
|
$headers = $null
|
||||||
|
if (![string]::IsNullOrWhiteSpace($Base64EncodedToken)) {
|
||||||
|
$headers = @{
|
||||||
|
Authorization = "Basic $Base64EncodedToken"
|
||||||
|
}
|
||||||
|
} elseif (![string]::IsNullOrWhiteSpace($BearerToken)) {
|
||||||
|
$headers = @{
|
||||||
|
Authorization = "Bearer $BearerToken"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LogError "Get-DevOpsApiHeaders::Unable to set the Authentication in the header because neither Base64EncodedToken nor BearerToken are set."
|
||||||
|
exit 1
|
||||||
}
|
}
|
||||||
return $headers
|
return $headers
|
||||||
}
|
}
|
||||||
|
@ -30,9 +48,8 @@ function Start-DevOpsBuild {
|
||||||
$SourceBranch,
|
$SourceBranch,
|
||||||
[Parameter(Mandatory = $true)]
|
[Parameter(Mandatory = $true)]
|
||||||
$DefinitionId,
|
$DefinitionId,
|
||||||
[ValidateNotNullOrEmpty()]
|
$Base64EncodedToken=$null,
|
||||||
[Parameter(Mandatory = $true)]
|
$BearerToken=$null,
|
||||||
$Base64EncodedAuthToken,
|
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[string]$BuildParametersJson
|
[string]$BuildParametersJson
|
||||||
)
|
)
|
||||||
|
@ -45,11 +62,13 @@ function Start-DevOpsBuild {
|
||||||
parameters = $BuildParametersJson
|
parameters = $BuildParametersJson
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedToken -BearerToken $BearerToken)
|
||||||
|
|
||||||
return Invoke-RestMethod `
|
return Invoke-RestMethod `
|
||||||
-Method POST `
|
-Method POST `
|
||||||
-Body ($parameters | ConvertTo-Json) `
|
-Body ($parameters | ConvertTo-Json) `
|
||||||
-Uri $uri `
|
-Uri $uri `
|
||||||
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
|
-Headers $headers `
|
||||||
-MaximumRetryCount 3 `
|
-MaximumRetryCount 3 `
|
||||||
-ContentType "application/json"
|
-ContentType "application/json"
|
||||||
}
|
}
|
||||||
|
@ -62,9 +81,8 @@ function Update-DevOpsBuild {
|
||||||
[Parameter(Mandatory = $true)]
|
[Parameter(Mandatory = $true)]
|
||||||
$BuildId,
|
$BuildId,
|
||||||
$Status, # pass canceling to cancel build
|
$Status, # pass canceling to cancel build
|
||||||
[ValidateNotNullOrEmpty()]
|
$Base64EncodedToken=$null,
|
||||||
[Parameter(Mandatory = $true)]
|
$BearerToken=$null
|
||||||
$Base64EncodedAuthToken
|
|
||||||
)
|
)
|
||||||
|
|
||||||
$uri = "$DevOpsAPIBaseURI" -F $Organization, $Project, "build", "builds/$BuildId", ""
|
$uri = "$DevOpsAPIBaseURI" -F $Organization, $Project, "build", "builds/$BuildId", ""
|
||||||
|
@ -72,11 +90,13 @@ function Update-DevOpsBuild {
|
||||||
|
|
||||||
if ($Status) { $parameters["status"] = $Status}
|
if ($Status) { $parameters["status"] = $Status}
|
||||||
|
|
||||||
|
$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedToken -BearerToken $BearerToken)
|
||||||
|
|
||||||
return Invoke-RestMethod `
|
return Invoke-RestMethod `
|
||||||
-Method PATCH `
|
-Method PATCH `
|
||||||
-Body ($parameters | ConvertTo-Json) `
|
-Body ($parameters | ConvertTo-Json) `
|
||||||
-Uri $uri `
|
-Uri $uri `
|
||||||
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
|
-Headers $headers `
|
||||||
-MaximumRetryCount 3 `
|
-MaximumRetryCount 3 `
|
||||||
-ContentType "application/json"
|
-ContentType "application/json"
|
||||||
}
|
}
|
||||||
|
@ -88,9 +108,8 @@ function Get-DevOpsBuilds {
|
||||||
$BranchName, # Should start with 'refs/heads/'
|
$BranchName, # Should start with 'refs/heads/'
|
||||||
$Definitions, # Comma seperated string of definition IDs
|
$Definitions, # Comma seperated string of definition IDs
|
||||||
$StatusFilter, # Comma seperated string 'cancelling, completed, inProgress, notStarted'
|
$StatusFilter, # Comma seperated string 'cancelling, completed, inProgress, notStarted'
|
||||||
[ValidateNotNullOrEmpty()]
|
$Base64EncodedToken=$null,
|
||||||
[Parameter(Mandatory = $true)]
|
$BearerToken=$null
|
||||||
$Base64EncodedAuthToken
|
|
||||||
)
|
)
|
||||||
|
|
||||||
$query = ""
|
$query = ""
|
||||||
|
@ -100,10 +119,12 @@ function Get-DevOpsBuilds {
|
||||||
if ($StatusFilter) { $query += "statusFilter=$StatusFilter&" }
|
if ($StatusFilter) { $query += "statusFilter=$StatusFilter&" }
|
||||||
$uri = "$DevOpsAPIBaseURI" -F $Organization, $Project , "build" , "builds", $query
|
$uri = "$DevOpsAPIBaseURI" -F $Organization, $Project , "build" , "builds", $query
|
||||||
|
|
||||||
|
$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedToken -BearerToken $BearerToken)
|
||||||
|
|
||||||
return Invoke-RestMethod `
|
return Invoke-RestMethod `
|
||||||
-Method GET `
|
-Method GET `
|
||||||
-Uri $uri `
|
-Uri $uri `
|
||||||
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
|
-Headers $headers `
|
||||||
-MaximumRetryCount 3
|
-MaximumRetryCount 3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,15 +133,18 @@ function Delete-RetentionLease {
|
||||||
$Organization,
|
$Organization,
|
||||||
$Project,
|
$Project,
|
||||||
$LeaseId,
|
$LeaseId,
|
||||||
$Base64EncodedAuthToken
|
$Base64EncodedToken=$null,
|
||||||
|
$BearerToken=$null
|
||||||
)
|
)
|
||||||
|
|
||||||
$uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?ids=$LeaseId&api-version=6.0-preview.1"
|
$uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?ids=$LeaseId&api-version=6.0-preview.1"
|
||||||
|
|
||||||
|
$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedToken -BearerToken $BearerToken)
|
||||||
|
|
||||||
return Invoke-RestMethod `
|
return Invoke-RestMethod `
|
||||||
-Method DELETE `
|
-Method DELETE `
|
||||||
-Uri $uri `
|
-Uri $uri `
|
||||||
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
|
-Headers $headers `
|
||||||
-MaximumRetryCount 3
|
-MaximumRetryCount 3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,15 +155,18 @@ function Get-RetentionLeases {
|
||||||
$DefinitionId,
|
$DefinitionId,
|
||||||
$RunId,
|
$RunId,
|
||||||
$OwnerId,
|
$OwnerId,
|
||||||
$Base64EncodedAuthToken
|
$Base64EncodedToken=$null,
|
||||||
|
$BearerToken=$null
|
||||||
)
|
)
|
||||||
|
|
||||||
$uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?ownerId=$OwnerId&definitionId=$DefinitionId&runId=$RunId&api-version=6.0-preview.1"
|
$uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?ownerId=$OwnerId&definitionId=$DefinitionId&runId=$RunId&api-version=6.0-preview.1"
|
||||||
|
|
||||||
|
$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedToken -BearerToken $BearerToken)
|
||||||
|
|
||||||
return Invoke-RestMethod `
|
return Invoke-RestMethod `
|
||||||
-Method GET `
|
-Method GET `
|
||||||
-Uri $uri `
|
-Uri $uri `
|
||||||
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
|
-Headers $headers `
|
||||||
-MaximumRetryCount 3
|
-MaximumRetryCount 3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,7 +178,8 @@ function Add-RetentionLease {
|
||||||
$RunId,
|
$RunId,
|
||||||
$OwnerId,
|
$OwnerId,
|
||||||
$DaysValid,
|
$DaysValid,
|
||||||
$Base64EncodedAuthToken
|
$Base64EncodedToken=$null,
|
||||||
|
$BearerToken=$null
|
||||||
)
|
)
|
||||||
|
|
||||||
$parameter = @{}
|
$parameter = @{}
|
||||||
|
@ -165,12 +193,13 @@ function Add-RetentionLease {
|
||||||
|
|
||||||
$uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?api-version=6.0-preview.1"
|
$uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?api-version=6.0-preview.1"
|
||||||
|
|
||||||
|
$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedToken -BearerToken $BearerToken)
|
||||||
|
|
||||||
return Invoke-RestMethod `
|
return Invoke-RestMethod `
|
||||||
-Method POST `
|
-Method POST `
|
||||||
-Body "[$body]" `
|
-Body "[$body]" `
|
||||||
-Uri $uri `
|
-Uri $uri `
|
||||||
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
|
-Headers $headers `
|
||||||
-MaximumRetryCount 3 `
|
-MaximumRetryCount 3 `
|
||||||
-ContentType "application/json"
|
-ContentType "application/json"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,21 +57,20 @@ param(
|
||||||
|
|
||||||
[string]$VsoQueuedPipelines,
|
[string]$VsoQueuedPipelines,
|
||||||
|
|
||||||
# Already base 64 encoded authentication token
|
# Unencoded authentication token from a PAT
|
||||||
[string]$Base64EncodedAuthToken,
|
[string]$AuthToken=$null,
|
||||||
|
|
||||||
# Unencoded authentication token
|
# Temp access token from the logged in az cli user for azure devops resource
|
||||||
[string]$AuthToken,
|
[string]$BearerToken=$null,
|
||||||
|
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[string]$BuildParametersJson
|
[string]$BuildParametersJson
|
||||||
)
|
)
|
||||||
|
|
||||||
. (Join-Path $PSScriptRoot common.ps1)
|
. (Join-Path $PSScriptRoot common.ps1)
|
||||||
|
$Base64EncodedToken=$null
|
||||||
if (!$Base64EncodedAuthToken)
|
if (![string]::IsNullOrWhiteSpace($AuthToken)) {
|
||||||
{
|
$Base64EncodedToken = Get-Base64EncodedToken $AuthToken
|
||||||
$Base64EncodedAuthToken = Get-Base64EncodedToken $AuthToken
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Skip if SourceBranch is empty because it we cannot generate a target branch
|
# Skip if SourceBranch is empty because it we cannot generate a target branch
|
||||||
|
@ -80,7 +79,7 @@ if ($CancelPreviousBuilds -and $SourceBranch)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$queuedBuilds = Get-DevOpsBuilds -BranchName "refs/heads/$SourceBranch" -Definitions $DefinitionId `
|
$queuedBuilds = Get-DevOpsBuilds -BranchName "refs/heads/$SourceBranch" -Definitions $DefinitionId `
|
||||||
-StatusFilter "inProgress, notStarted" -Base64EncodedAuthToken $Base64EncodedAuthToken
|
-StatusFilter "inProgress, notStarted" -Base64EncodedToken $Base64EncodedToken -BearerToken $BearerToken
|
||||||
|
|
||||||
if ($queuedBuilds.count -eq 0) {
|
if ($queuedBuilds.count -eq 0) {
|
||||||
LogDebug "There is no previous build still inprogress or about to start."
|
LogDebug "There is no previous build still inprogress or about to start."
|
||||||
|
@ -89,7 +88,7 @@ if ($CancelPreviousBuilds -and $SourceBranch)
|
||||||
foreach ($build in $queuedBuilds.Value) {
|
foreach ($build in $queuedBuilds.Value) {
|
||||||
$buildID = $build.id
|
$buildID = $build.id
|
||||||
LogDebug "Canceling build [ $($build._links.web.href) ]"
|
LogDebug "Canceling build [ $($build._links.web.href) ]"
|
||||||
Update-DevOpsBuild -BuildId $buildID -Status "cancelling" -Base64EncodedAuthToken $Base64EncodedAuthToken
|
Update-DevOpsBuild -BuildId $buildID -Status "cancelling" -Base64EncodedToken $Base64EncodedToken -BearerToken $BearerToken
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
|
@ -104,7 +103,8 @@ try {
|
||||||
-Project $Project `
|
-Project $Project `
|
||||||
-SourceBranch $SourceBranch `
|
-SourceBranch $SourceBranch `
|
||||||
-DefinitionId $DefinitionId `
|
-DefinitionId $DefinitionId `
|
||||||
-Base64EncodedAuthToken $Base64EncodedAuthToken `
|
-Base64EncodedToken $Base64EncodedToken `
|
||||||
|
-BearerToken $BearerToken `
|
||||||
-BuildParametersJson $BuildParametersJson
|
-BuildParametersJson $BuildParametersJson
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче