From 295060833d3c1f74cd887b5f9a08d4fa79d1e0af Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Tue, 11 Apr 2023 14:11:53 +0200 Subject: [PATCH 1/2] added switch to set variable as secret --- .../03-Features/powershell/azure-devops.md | 20 ++++++++++++++----- .../Arcus.Scripting.DevOps.psm1 | 9 +++++++-- .../Arcus.Scripting.DevOps.tests.ps1 | 12 ++++++++++- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/docs/preview/03-Features/powershell/azure-devops.md b/docs/preview/03-Features/powershell/azure-devops.md index 3b459364..ecd60103 100644 --- a/docs/preview/03-Features/powershell/azure-devops.md +++ b/docs/preview/03-Features/powershell/azure-devops.md @@ -18,16 +18,26 @@ PS> Install-Module -Name Arcus.Scripting.DevOps -Repository PSGallery -AllowClob Assign a value to a DevOps pipeline variable during the execution of this pipeline. -| Parameter | Mandatory | Description | -| --------------- | --------- | ------------------------------------------------- | -| `Name` | yes | The name of the variable to set in the pipeline | -| `Value` | yes | The value of the variable to set in the pipeline | +| Parameter | Mandatory | Description | +| --------------------- | --------- | ------------------------------------------------- | +| `Name` | yes | The name of the variable to set in the pipeline | +| `Value` | yes | The value of the variable to set in the pipeline | +| `SetVariableAsSecret` | no | The switch to set the variable as a secret | **Example** +Setting a variable: + ```powershell PS> Set-AzDevOpsVariable "my-variable" "my-variable-value" -# #vso[task.setvariable variable=my-variable] my-variable-value +##vso[task.setvariable variable=my-variable] my-variable-value +``` + +Setting a variable as a secret: + +```powershell +PS> Set-AzDevOpsVariable "my-variable" "my-variable-value" -SetVariableAsSecret +##vso[task.setvariable variable=my-variable;issecret=true] *** ``` ## Setting ARM outputs to Azure DevOps variable group diff --git a/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 b/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 index bf51fa00..a27bde4f 100644 --- a/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 +++ b/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 @@ -14,10 +14,15 @@ function Set-AzDevOpsVariable { param( [parameter(Mandatory=$true)][string] $Name = $(throw "Name is required"), - [parameter(Mandatory=$true)][string] $Value = $(throw "Value is required") + [parameter(Mandatory=$true)][string] $Value = $(throw "Value is required"), + [parameter(Mandatory=$false)][switch] $SetVariableAsSecret = $false ) - Write-Host "#vso[task.setvariable variable=$Name] $Value" + if ($SetVariableAsSecret) { + Write-Host "##vso[task.setvariable variable=$Name;issecret=true] $Value" + } else { + Write-Host "##vso[task.setvariable variable=$Name] $Value" + } } Export-ModuleMember -Function Set-AzDevOpsVariable diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.DevOps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.DevOps.tests.ps1 index f59c5ffa..ca25e13c 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.DevOps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.DevOps.tests.ps1 @@ -5,7 +5,7 @@ InModuleScope Arcus.Scripting.DevOps { Context "Setting ARM outputs to Azure DevOps variable group" { It "Setting DevOps variable should write to host" { # Arrange - Mock Write-Host { $Object | Should -Be "#vso[task.setvariable variable=test] value" } -Verifiable + Mock Write-Host { $Object | Should -Be "##vso[task.setvariable variable=test] value" } -Verifiable # Act Set-AzDevOpsVariable "test" "value" @@ -13,6 +13,16 @@ InModuleScope Arcus.Scripting.DevOps { # Assert Assert-VerifiableMock } + It "Setting DevOps variable as secret should write to host" { + # Arrange + Mock Write-Host { $Object | Should -Be "##vso[task.setvariable variable=test;issecret=true] value" } -Verifiable + + # Act + Set-AzDevOpsVariable "test" "value" -SetVariableAsSecret + + # Assert + Assert-VerifiableMock + } It "Setting DevOps variable group from ARM outputs should send info to DevOps project" { # Arrange $variableGroupName = "some-variable-group-name" From 7096e07d7cca0c16a901ba66fcf5fe9f441a83cb Mon Sep 17 00:00:00 2001 From: Pim Simons Date: Wed, 12 Apr 2023 08:49:53 +0200 Subject: [PATCH 2/2] renamed switch to AsSecret --- docs/preview/03-Features/powershell/azure-devops.md | 4 ++-- src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 | 4 ++-- .../Arcus.Scripting.DevOps.tests.ps1 | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/preview/03-Features/powershell/azure-devops.md b/docs/preview/03-Features/powershell/azure-devops.md index ecd60103..68fc702f 100644 --- a/docs/preview/03-Features/powershell/azure-devops.md +++ b/docs/preview/03-Features/powershell/azure-devops.md @@ -22,7 +22,7 @@ Assign a value to a DevOps pipeline variable during the execution of this pipeli | --------------------- | --------- | ------------------------------------------------- | | `Name` | yes | The name of the variable to set in the pipeline | | `Value` | yes | The value of the variable to set in the pipeline | -| `SetVariableAsSecret` | no | The switch to set the variable as a secret | +| `AsSecret` | no | The switch to set the variable as a secret | **Example** @@ -36,7 +36,7 @@ PS> Set-AzDevOpsVariable "my-variable" "my-variable-value" Setting a variable as a secret: ```powershell -PS> Set-AzDevOpsVariable "my-variable" "my-variable-value" -SetVariableAsSecret +PS> Set-AzDevOpsVariable "my-variable" "my-variable-value" -AsSecret ##vso[task.setvariable variable=my-variable;issecret=true] *** ``` diff --git a/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 b/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 index a27bde4f..43834480 100644 --- a/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 +++ b/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1 @@ -15,10 +15,10 @@ function Set-AzDevOpsVariable { param( [parameter(Mandatory=$true)][string] $Name = $(throw "Name is required"), [parameter(Mandatory=$true)][string] $Value = $(throw "Value is required"), - [parameter(Mandatory=$false)][switch] $SetVariableAsSecret = $false + [parameter(Mandatory=$false)][switch] $AsSecret = $false ) - if ($SetVariableAsSecret) { + if ($AsSecret) { Write-Host "##vso[task.setvariable variable=$Name;issecret=true] $Value" } else { Write-Host "##vso[task.setvariable variable=$Name] $Value" diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.DevOps.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.DevOps.tests.ps1 index ca25e13c..6b423afc 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.DevOps.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.DevOps.tests.ps1 @@ -18,7 +18,7 @@ InModuleScope Arcus.Scripting.DevOps { Mock Write-Host { $Object | Should -Be "##vso[task.setvariable variable=test;issecret=true] value" } -Verifiable # Act - Set-AzDevOpsVariable "test" "value" -SetVariableAsSecret + Set-AzDevOpsVariable "test" "value" -AsSecret # Assert Assert-VerifiableMock