From 0a54ba2a34171c731e98356ddf173f31f8033934 Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 15 Nov 2024 07:36:46 +0100 Subject: [PATCH 1/9] feat: set az devops group var --- .../03-Features/powershell/azure-devops.md | 16 +++ .../Arcus.Scripting.DevOps.psd1 | Bin 7770 -> 7880 bytes .../Arcus.Scripting.DevOps.psm1 | 32 +++++- .../Arcus.Scripting.DevOps.pssproj | 1 + .../Scripts/Set-AzDevOpsArmOutputs.ps1 | 55 +---------- .../Scripts/Set-AzDevOpsGroupVariable.ps1 | 48 +++++++++ .../Arcus.Scripting.DevOps.tests.ps1 | 93 ++++++++++++++++++ 7 files changed, 189 insertions(+), 56 deletions(-) create mode 100644 src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 diff --git a/docs/preview/03-Features/powershell/azure-devops.md b/docs/preview/03-Features/powershell/azure-devops.md index e8821856..bc8ce134 100644 --- a/docs/preview/03-Features/powershell/azure-devops.md +++ b/docs/preview/03-Features/powershell/azure-devops.md @@ -40,6 +40,22 @@ PS> Set-AzDevOpsVariable "my-variable" "my-variable-value" -AsSecret ##vso[task.setvariable variable=my-variable;issecret=true] *** ``` +## Setting a variable in an Azure DevOps variable group + +Assign a value to a DevOps variable group during the execution of a pipeline. + +| Parameter | Mandatory | Description | +| --------------------- | --------- | ------------------------------------------------------ | +| `VariableGroupName` | yes | The name of the remote variable group on Azure DevOps | +| `VariableName` | yes | The name of the variable to set in the variable group | +| `VariableValue` | yes | The value of the variable to set in the variable group | + +**Example** + +```powershell +PS> Set-AzDevOpsGroupVariable -VariableGroupName "product-v1-dev" -VariableName "Product.Api.Url" -VariableValue "https://product/api/" +``` + ## Setting ARM outputs to Azure DevOps variable group Stores the Azure Resource Management (ARM) outputs in a variable group on Azure DevOps. diff --git a/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psd1 b/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psd1 index 728c1b72ee0013e7aa0290bc20646fed76fbf1c3..b7faba8cbb0839e92a7f5cc2b02bc632225f6b46 100644 GIT binary patch delta 140 zcmca*bHaAR5`J!81}+8#2%Nl_U)NuU0ZD{GogtVZm7#<|m%)*tiopfQE@SX#C}1dN zaAzoD$Y&@8vcnh>f$U5mnZ%F-RH4p*WX5D=0n^EB0&<*iQ-LDU2;SyefxEl_!crGI delta 40 vcmX?Md&_3S68^~>_;r{S7<4AD=a-)BAz(In9ls2amYBRpKyC9IfjhhaElv() diff --git a/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 b/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 index 87586675..c414814d 100644 --- a/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 +++ b/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 @@ -5,10 +5,10 @@ .Description Assign a value to a DevOps pipeline variable during the execution of this pipeline. - .Parameter VariableName + .Parameter Name The name of the variable to set in the pipeline. - .Parameter VariableValue + .Parameter Value The value of the variable to set in the pipeline. #> function Set-AzDevOpsVariable { @@ -27,6 +27,34 @@ function Set-AzDevOpsVariable { Export-ModuleMember -Function Set-AzDevOpsVariable +<# + .Synopsis + Set a variable in the Azure DevOps variable group. + + .Description + Assign a value to a DevOps variable group during the execution of an Azure DevOps pipeline. + + .Parameter VariableGroupName + The name of the Azure DevOps variable group to updat with a new variable. + + .Parameter VariableName + The name of the variable to set in the variable group. + + .Parameter VariableValue + The value of the variable to set in the variable group. +#> +function Set-AzDevOpsGroupVariable { + param( + [string][parameter(Mandatory = $true)]$VariableGroupName, + [string][parameter(Mandatory = $true)]$VariableName, + [string][parameter(Mandatory = $true)]$VariableValue + ) + + . $PSScriptRoot\Scripts\Set-AzDevOpsGroupVariable.ps1 -VariableGroupName $VariableGroupName -VariableName $VariableName -VariableValue $VariableValue +} + +Export-ModuleMember -Function Set-AzDevOpsGroupVariable + <# .Synopsis Sets the ARM outputs as a variable group on Azure DevOps. diff --git a/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.pssproj b/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.pssproj index 1ba027ce..4b1290c2 100644 --- a/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.pssproj +++ b/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.pssproj @@ -34,6 +34,7 @@ + diff --git a/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsArmOutputs.ps1 b/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsArmOutputs.ps1 index f044a88d..462811a0 100644 --- a/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsArmOutputs.ps1 +++ b/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsArmOutputs.ps1 @@ -5,59 +5,6 @@ param( [switch][parameter(Mandatory = $false)] $UpdateVariablesForCurrentJob = $false ) -function Add-VariableGroupVariable() { - [CmdletBinding()] - param( - [string][parameter(Mandatory = $true)]$VariableGroupName, - [string][parameter(Mandatory = $true)]$variableName, - [string][parameter(Mandatory = $true)]$variableValue - ) - BEGIN { - Write-Verbose "Retrieving Azure DevOps project details for variable group '$VariableGroupName'..." - [String]$project = "$env:SYSTEM_TEAMPROJECT" - [String]$projectUri = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI" - [String]$apiVersion = "4.1-preview.1" - Write-Debug "Using Azure DevOps project: $project, project URI: $projectUri" - - - Write-Verbose "Setting authorization headers to retrieve potential existing Azure DevOps variable group '$VariableGroupName'..." - if ([string]::IsNullOrEmpty($env:SYSTEM_ACCESSTOKEN)) { - Write-Error "The SYSTEM_ACCESSTOKEN environment variable is empty. Remember to explicitly allow the build job to access the OAuth Token!" - } - $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } - - - Write-Verbose "Getting Azure DevOps variable group '$VariableGroupName'..." - $getVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups?api-version=" + $apiVersion + "&groupName=" + $VariableGroupName - $variableGroup = (Invoke-RestMethod -Uri $getVariableGroupUrl -Headers $headers -Verbose) - - $releaseName = $env:RELEASE_RELEASENAME - if ([string]::IsNullOrEmpty($releaseName)) { - $releaseName = $env:BUILD_DEFINITIONNAME + " " + $env:BUILD_BUILDNUMBER - } - - if ($variableGroup.value) { - Write-Host "Set properties for update of existing Azure DevOps variable group '$variableGroupName'" - $variableGroup = $variableGroup.value[0] - $variableGroup | Add-Member -Name "description" -MemberType NoteProperty -Value "Variable group that got auto-updated by release '$releaseName'." -Force - $method = "Put" - $upsertVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups/" + $variableGroup.id + "?api-version=" + $apiVersion - } else { - Write-Host "Set properties for creation of new Azure DevOps variable group '$VariableGroupName'" - $variableGroup = @{name = $VariableGroupName; type = "Vsts"; description = "Variable group that got auto-updated by release '$releaseName'."; variables = New-Object PSObject; } - $method = "Post" - $upsertVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups?api-version=" + $apiVersion - } - - $variableGroup.variables | Add-Member -Name $variableName -MemberType NoteProperty -Value @{value = $variableValue } -Force - - Write-Verbose "Upserting Azure DevOps variable group '$variableGroupName'..." - $body = $variableGroup | ConvertTo-Json -Depth 10 -Compress - Write-Debug $body - Invoke-RestMethod $upsertVariableGroupUrl -Method $method -Body $body -Headers $headers -ContentType 'application/json; charset=utf-8' -Verbose - } -} - Write-Verbose "Geting ARM outputs from '$ArmOutputsEnvironmentVariableName' environment variable..." $json = [System.Environment]::GetEnvironmentVariable($ArmOutputsEnvironmentVariableName) $armOutputs = ConvertFrom-Json $json @@ -68,7 +15,7 @@ foreach ($output in $armOutputs.PSObject.Properties) { if ($UpdateVariableGroup) { Write-Host Adding variable $output.Name with value $variableValue to variable group $VariableGroupName - Add-VariableGroupVariable -VariableGroupName $VariableGroupName -variableName $variableName -variableValue $variableValue + . $PSScriptRoot\Set-AzDevOpsGroupVariable.ps1 -VariableGroupName $VariableGroupName -VariableName $variableName -VariableValue $variableValue } if ($UpdateVariablesForCurrentJob) { diff --git a/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 b/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 new file mode 100644 index 00000000..6e61ceb5 --- /dev/null +++ b/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 @@ -0,0 +1,48 @@ +param( + [string][parameter(Mandatory = $true)]$VariableGroupName, + [string][parameter(Mandatory = $true)]$VariableName, + [string][parameter(Mandatory = $true)]$VariableValue +) + +Write-Verbose "Retrieving Azure DevOps project details for variable group '$VariableGroupName'..." +[String]$project = "$env:SYSTEM_TEAMPROJECT" +[String]$projectUri = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI" +[String]$apiVersion = "7.1" +Write-Debug "Using Azure DevOps project: $project, project URI: $projectUri" + + +Write-Verbose "Setting authorization headers to retrieve potential existing Azure DevOps variable group '$VariableGroupName'..." +if ([string]::IsNullOrEmpty($env:SYSTEM_ACCESSTOKEN)) { + Write-Error "The SYSTEM_ACCESSTOKEN environment variable is empty. Remember to explicitly allow the build job to access the OAuth Token!" +} +$headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } + + +Write-Verbose "Getting Azure DevOps variable group '$VariableGroupName'..." +$getVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups?api-version=" + $apiVersion + "&groupName=" + $VariableGroupName +$variableGroup = (Invoke-RestMethod -Uri $getVariableGroupUrl -Headers $headers -Verbose) + +$releaseName = $env:RELEASE_RELEASENAME +if ([string]::IsNullOrEmpty($releaseName)) { + $releaseName = $env:BUILD_DEFINITIONNAME + " " + $env:BUILD_BUILDNUMBER +} + +if ($variableGroup.value) { + Write-Host "Set properties for update of existing Azure DevOps variable group '$variableGroupName'" + $variableGroup = $variableGroup.value[0] + $variableGroup | Add-Member -Name "description" -MemberType NoteProperty -Value "Variable group that got auto-updated by release '$releaseName'." -Force + $method = "Put" + $upsertVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups/" + $variableGroup.id + "?api-version=" + $apiVersion +} else { + Write-Host "Set properties for creation of new Azure DevOps variable group '$VariableGroupName'" + $variableGroup = @{name = $VariableGroupName; type = "Vsts"; description = "Variable group that got auto-updated by release '$releaseName'."; variables = New-Object PSObject; } + $method = "Post" + $upsertVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups?api-version=" + $apiVersion +} + +$variableGroup.variables | Add-Member -Name $VariableName -MemberType NoteProperty -Value @{value = $VariableValue } -Force + +Write-Verbose "Upserting Azure DevOps variable group '$variableGroupName'..." +$body = $variableGroup | ConvertTo-Json -Depth 10 -Compress +Write-Debug $body +Invoke-RestMethod $upsertVariableGroupUrl -Method $method -Body $body -Headers $headers -ContentType 'application/json; charset=utf-8' -Verbose diff --git a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 index 5ccb0192..dcede3ad 100644 --- a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 @@ -1,5 +1,61 @@ Import-Module -Name $PSScriptRoot\..\Arcus.Scripting.DevOps -ErrorAction Stop +function Get-AzDevOpsGroup { + params($VariableGroupName) + + $VariableGroupName = $VariableGroupName -replace ' ', '%20' + $projectId = $env:SYSTEM_TEAMPROJECTID + $collectionUri = $env:SYSTEM_COLLECTIONURI + $getUri = "$collectionUri" + "$projectId/_apis/distributedtask/variablegroups?groupName=" + $VariableGroupName + "&api-version=7.1" + $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } + + Write-Host "GET -> $getUri" + $getResponse = Invoke-WebRequest -Uri $getUri -Method Get -Headers $headers + Write-Host "$($getResponse.StatusCode) <- $getUri" + $json = ConvertFrom-Json $getResponse.Content + + return $json.value[0] +} + +function Remove-AzDevOpsGroup { + params($VariableGroupName) + + $variableGroup = Get-AzDevOpsGroup -VariableGroupName $VariableGroupName + + $VariableGroupName = $VariableGroupName -replace ' ', '%20' + $projectId = $env:SYSTEM_TEAMPROJECTID + $collectionUri = $env:SYSTEM_COLLECTIONURI + $deleteUri = "$collectionUri" + "/_apis/distributedtask/variablegroups/" + $variableGroup.id + "?projectIds=$projectId&api-version=7.1" + $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } + + Write-Host "DELETE -> $deleteUri" + $deleteResponse = Invoke-WebRequest -Uri $deleteUri -Method Delete -Headers $headers + Write-Host "$($deleteResponse.StatusCode) <- $deleteUri" +} + +function Get-AzDevOpsGroupVariable { + params($VariableGroupName, $VariableName) + + $json = Get-AzDevOpsGroup -VariableGroupName $VariableGroupName + $variable = $json.variables.PSObject.Properties | where { $_.Name -eq $VariableName } + + return $variable +} + +function Remove-AzDevOpsGroupVariable { + params($VariableGroupName, $VariableName) + + $json = Get-AzDevOpsGroup -VariableGroupName $VariableGroupName + $json.variables.PSObject.Properties.Remove($VariableName) + + $upsertVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups/$($json.id)?api-version=7.1" + $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } + + Write-Host "PUT -> $upsertVariableGroupUrl" + $putResponse = Invoke-WebRequest -Uri $upsertVariableGroupUrl -Method Put -Headers $headers + Write-Host "$($putResponse.StatusCode) <- $upsertVariableGroupUrl" +} + InModuleScope Arcus.Scripting.DevOps { Describe "Arcus Azure DevOps integration tests" { BeforeEach { @@ -43,6 +99,7 @@ InModuleScope Arcus.Scripting.DevOps { $collectionUri = $env:SYSTEM_COLLECTIONURI $requestUri = "$collectionUri" + "$projectId/_apis/build/builds/" + $buildId + "/leases?api-version=7.0" $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } + try { # Act Save-AzDevOpsBuild -ProjectId $projectId -BuildId $buildId -DaysToKeep 10 @@ -66,6 +123,8 @@ InModuleScope Arcus.Scripting.DevOps { } } } + } + Context "Azure DevOps variable group" { It "Sets the DevOps variable group description with the release name" { # Arrange $variableGroupName = $config.Arcus.DevOps.VariableGroup.Name @@ -86,6 +145,40 @@ InModuleScope Arcus.Scripting.DevOps { $json = ConvertFrom-Json $getResponse.Content $json.value[0].description | Should -BeLike "*$env:Build_DefinitionName*$env:Build_BuildNumber*" } + It "Sets a new variable to an existing DevOps variable group" { + # Arrange + $variableGroupName = $config.Arcus.DevOps.VariableGroup.Name + $variableName = [System.Guid]::NewGuid().ToString() + $expectedValue = [System.Guid]::NewGuid().ToString() + try { + # Act + Set-AzDevOpsGroupVariable -VariableGroupName $variableGroupName -VariableName $variableName -VariableValue $expectedValue + + # Assert + $actualValue = Get-AzDevOpsGroupVariable -VariableGroupName $variableGroupName -VariableName $variableName + $actualValue | Should -Be $variableValue + + } finally { + Remove-AzDevOpsGroupVariable -VariableGroupName $variableGroupName -VariableName $variableName + } + } + It "Sets a new variable to a new DevOps variable group" { + # Arrange + $variableGroupName = [System.Guid]::NewGuid() + $variableName = [System.Guid]::NewGuid() + $variableValue = [System.Guid]::NewGuid() + try { + # Act + Set-AzDevOpsVariable -VariableGroupName $variableGroupName -VariableName $variableName -VariableValue $variableValue + + # Assert + $actualValue = Get-AzDevOpsGroupVariable -VariableGroupName $variableGroupName -VariableName $variableName + $actualValue | Should -Be $variableValue + + } finally { + Remove-AzDevOpsVariableGroup -VariableGroupName $variableGroupName + } + } } } } From 20d42a7647892dad501ffa6a76e3839ca1a94a91 Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 15 Nov 2024 07:44:08 +0100 Subject: [PATCH 2/9] pr-fix: make functions public --- .../Arcus.Scripting.DevOps.tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 index dcede3ad..3a9f5444 100644 --- a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 @@ -1,6 +1,6 @@ Import-Module -Name $PSScriptRoot\..\Arcus.Scripting.DevOps -ErrorAction Stop -function Get-AzDevOpsGroup { +function global:Get-AzDevOpsGroup { params($VariableGroupName) $VariableGroupName = $VariableGroupName -replace ' ', '%20' @@ -17,7 +17,7 @@ function Get-AzDevOpsGroup { return $json.value[0] } -function Remove-AzDevOpsGroup { +function global:Remove-AzDevOpsGroup { params($VariableGroupName) $variableGroup = Get-AzDevOpsGroup -VariableGroupName $VariableGroupName @@ -33,7 +33,7 @@ function Remove-AzDevOpsGroup { Write-Host "$($deleteResponse.StatusCode) <- $deleteUri" } -function Get-AzDevOpsGroupVariable { +function global:Get-AzDevOpsGroupVariable { params($VariableGroupName, $VariableName) $json = Get-AzDevOpsGroup -VariableGroupName $VariableGroupName @@ -42,7 +42,7 @@ function Get-AzDevOpsGroupVariable { return $variable } -function Remove-AzDevOpsGroupVariable { +function global:Remove-AzDevOpsGroupVariable { params($VariableGroupName, $VariableName) $json = Get-AzDevOpsGroup -VariableGroupName $VariableGroupName From e5b4404789fd0c30e766b3723641a87a327978ab Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 15 Nov 2024 07:49:20 +0100 Subject: [PATCH 3/9] pr-fix: use 'param' --- .../Arcus.Scripting.DevOps.tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 index 3a9f5444..3abbfffa 100644 --- a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 @@ -1,7 +1,7 @@ Import-Module -Name $PSScriptRoot\..\Arcus.Scripting.DevOps -ErrorAction Stop function global:Get-AzDevOpsGroup { - params($VariableGroupName) + param($VariableGroupName) $VariableGroupName = $VariableGroupName -replace ' ', '%20' $projectId = $env:SYSTEM_TEAMPROJECTID @@ -18,7 +18,7 @@ function global:Get-AzDevOpsGroup { } function global:Remove-AzDevOpsGroup { - params($VariableGroupName) + param($VariableGroupName) $variableGroup = Get-AzDevOpsGroup -VariableGroupName $VariableGroupName @@ -34,7 +34,7 @@ function global:Remove-AzDevOpsGroup { } function global:Get-AzDevOpsGroupVariable { - params($VariableGroupName, $VariableName) + param($VariableGroupName, $VariableName) $json = Get-AzDevOpsGroup -VariableGroupName $VariableGroupName $variable = $json.variables.PSObject.Properties | where { $_.Name -eq $VariableName } @@ -43,7 +43,7 @@ function global:Get-AzDevOpsGroupVariable { } function global:Remove-AzDevOpsGroupVariable { - params($VariableGroupName, $VariableName) + param($VariableGroupName, $VariableName) $json = Get-AzDevOpsGroup -VariableGroupName $VariableGroupName $json.variables.PSObject.Properties.Remove($VariableName) From 6d10294d057ef0a5ea7c4c07a622be4c42fc3f74 Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 15 Nov 2024 07:49:44 +0100 Subject: [PATCH 4/9] pr-fix: correct logging w/ 'pipeline name' io 'relase name' --- .../Scripts/Set-AzDevOpsGroupVariable.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 b/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 index 6e61ceb5..878fa1f2 100644 --- a/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 +++ b/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 @@ -30,12 +30,12 @@ if ([string]::IsNullOrEmpty($releaseName)) { if ($variableGroup.value) { Write-Host "Set properties for update of existing Azure DevOps variable group '$variableGroupName'" $variableGroup = $variableGroup.value[0] - $variableGroup | Add-Member -Name "description" -MemberType NoteProperty -Value "Variable group that got auto-updated by release '$releaseName'." -Force + $variableGroup | Add-Member -Name "description" -MemberType NoteProperty -Value "Variable group that got auto-updated by pipeline '$releaseName'." -Force $method = "Put" $upsertVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups/" + $variableGroup.id + "?api-version=" + $apiVersion } else { Write-Host "Set properties for creation of new Azure DevOps variable group '$VariableGroupName'" - $variableGroup = @{name = $VariableGroupName; type = "Vsts"; description = "Variable group that got auto-updated by release '$releaseName'."; variables = New-Object PSObject; } + $variableGroup = @{name = $VariableGroupName; type = "Vsts"; description = "Variable group that got auto-updated by pipeline '$releaseName'."; variables = New-Object PSObject; } $method = "Post" $upsertVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups?api-version=" + $apiVersion } From 6eb52594e3a74a5c2fe6d45e5f3cacf5f98d457c Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 15 Nov 2024 08:03:21 +0100 Subject: [PATCH 5/9] pr-fix: correct names and logging --- .../Scripts/Set-AzDevOpsGroupVariable.ps1 | 6 ++++-- .../Arcus.Scripting.DevOps.tests.ps1 | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 b/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 index 878fa1f2..4d11274a 100644 --- a/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 +++ b/src/Arcus.Scripting.DevOps/Scripts/Set-AzDevOpsGroupVariable.ps1 @@ -32,7 +32,7 @@ if ($variableGroup.value) { $variableGroup = $variableGroup.value[0] $variableGroup | Add-Member -Name "description" -MemberType NoteProperty -Value "Variable group that got auto-updated by pipeline '$releaseName'." -Force $method = "Put" - $upsertVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups/" + $variableGroup.id + "?api-version=" + $apiVersion + $upsertVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups/" + $variableGroup.id + "?api-version=" + $apiVersion } else { Write-Host "Set properties for creation of new Azure DevOps variable group '$VariableGroupName'" $variableGroup = @{name = $VariableGroupName; type = "Vsts"; description = "Variable group that got auto-updated by pipeline '$releaseName'."; variables = New-Object PSObject; } @@ -44,5 +44,7 @@ $variableGroup.variables | Add-Member -Name $VariableName -MemberType NoteProper Write-Verbose "Upserting Azure DevOps variable group '$variableGroupName'..." $body = $variableGroup | ConvertTo-Json -Depth 10 -Compress -Write-Debug $body +Write-Host $body + +Write-Host "$method -> $upsertVariableGroupUrl" Invoke-RestMethod $upsertVariableGroupUrl -Method $method -Body $body -Headers $headers -ContentType 'application/json; charset=utf-8' -Verbose diff --git a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 index 3abbfffa..34f8501d 100644 --- a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 @@ -17,7 +17,7 @@ function global:Get-AzDevOpsGroup { return $json.value[0] } -function global:Remove-AzDevOpsGroup { +function global:Remove-AzDevOpsVariableGroup { param($VariableGroupName) $variableGroup = Get-AzDevOpsGroup -VariableGroupName $VariableGroupName @@ -48,6 +48,8 @@ function global:Remove-AzDevOpsGroupVariable { $json = Get-AzDevOpsGroup -VariableGroupName $VariableGroupName $json.variables.PSObject.Properties.Remove($VariableName) + $project = "$env:SYSTEM_TEAMPROJECT" + $projectUri = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI" $upsertVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups/$($json.id)?api-version=7.1" $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } @@ -169,7 +171,7 @@ InModuleScope Arcus.Scripting.DevOps { $variableValue = [System.Guid]::NewGuid() try { # Act - Set-AzDevOpsVariable -VariableGroupName $variableGroupName -VariableName $variableName -VariableValue $variableValue + Set-AzDevOpsGroupVariable -VariableGroupName $variableGroupName -VariableName $variableName -VariableValue $variableValue # Assert $actualValue = Get-AzDevOpsGroupVariable -VariableGroupName $variableGroupName -VariableName $variableName From dcfd1dcdac46080e15746c8599fea343167719cd Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 15 Nov 2024 08:23:29 +0100 Subject: [PATCH 6/9] pr-fix: add body logging --- .../Arcus.Scripting.DevOps.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 index 34f8501d..aa437254 100644 --- a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 @@ -11,8 +11,8 @@ function global:Get-AzDevOpsGroup { Write-Host "GET -> $getUri" $getResponse = Invoke-WebRequest -Uri $getUri -Method Get -Headers $headers - Write-Host "$($getResponse.StatusCode) <- $getUri" $json = ConvertFrom-Json $getResponse.Content + Write-Host "$($getResponse.StatusCode) $json <- $getUri" return $json.value[0] } From 843ad5b8cc3b77ad9ae95f4affddabfddeeb9988 Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 15 Nov 2024 08:45:20 +0100 Subject: [PATCH 7/9] pr-fix: add body logging --- .../Arcus.Scripting.DevOps.tests.ps1 | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 index aa437254..86955389 100644 --- a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 @@ -12,9 +12,11 @@ function global:Get-AzDevOpsGroup { Write-Host "GET -> $getUri" $getResponse = Invoke-WebRequest -Uri $getUri -Method Get -Headers $headers $json = ConvertFrom-Json $getResponse.Content - Write-Host "$($getResponse.StatusCode) $json <- $getUri" + + $variableGroup = $json.value[0] + Write-Host "$($getResponse.StatusCode) $variableGroup <- $getUri" - return $json.value[0] + return $variableGroup } function global:Remove-AzDevOpsVariableGroup { @@ -25,7 +27,7 @@ function global:Remove-AzDevOpsVariableGroup { $VariableGroupName = $VariableGroupName -replace ' ', '%20' $projectId = $env:SYSTEM_TEAMPROJECTID $collectionUri = $env:SYSTEM_COLLECTIONURI - $deleteUri = "$collectionUri" + "/_apis/distributedtask/variablegroups/" + $variableGroup.id + "?projectIds=$projectId&api-version=7.1" + $deleteUri = "$collectionUri" + "$projectId/_apis/distributedtask/variablegroups/" + $variableGroup.id + "?api-version=7.1" $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } Write-Host "DELETE -> $deleteUri" From b1814494645b44c30463f9fa435cfa8efba7f16d Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 15 Nov 2024 08:53:19 +0100 Subject: [PATCH 8/9] pr-fix: add body to put request --- .../Arcus.Scripting.DevOps.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 index 86955389..ae399816 100644 --- a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 @@ -56,7 +56,7 @@ function global:Remove-AzDevOpsGroupVariable { $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } Write-Host "PUT -> $upsertVariableGroupUrl" - $putResponse = Invoke-WebRequest -Uri $upsertVariableGroupUrl -Method Put -Headers $headers + $putResponse = Invoke-WebRequest -Uri $upsertVariableGroupUrl -Method Put -Headers $headers -Body $json -ContentType 'application/json; charset=utf-8' Write-Host "$($putResponse.StatusCode) <- $upsertVariableGroupUrl" } From 9d8bca92e992eaec03dfb4bcb407dfc61c605c63 Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 15 Nov 2024 09:05:14 +0100 Subject: [PATCH 9/9] pr-fix: add body to put request --- .../Arcus.Scripting.DevOps.tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 index ae399816..4646a00c 100644 --- a/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.DevOps.tests.ps1 @@ -55,7 +55,8 @@ function global:Remove-AzDevOpsGroupVariable { $upsertVariableGroupUrl = $projectUri + $project + "/_apis/distributedtask/variablegroups/$($json.id)?api-version=7.1" $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } - Write-Host "PUT -> $upsertVariableGroupUrl" + $json = $json | ConvertTo-Json -Depth 10 -Compress + Write-Host "PUT $json -> $upsertVariableGroupUrl" $putResponse = Invoke-WebRequest -Uri $upsertVariableGroupUrl -Method Put -Headers $headers -Body $json -ContentType 'application/json; charset=utf-8' Write-Host "$($putResponse.StatusCode) <- $upsertVariableGroupUrl" }