Skip to content

Commit e86ef48

Browse files
authored
feat: Added switch to set variable as secret (#380)
Co-authored-by: Pim Simons <pim.simons@codit.eu>
1 parent e963158 commit e86ef48

3 files changed

Lines changed: 33 additions & 8 deletions

File tree

docs/preview/03-Features/powershell/azure-devops.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,26 @@ PS> Install-Module -Name Arcus.Scripting.DevOps -Repository PSGallery -AllowClob
1818

1919
Assign a value to a DevOps pipeline variable during the execution of this pipeline.
2020

21-
| Parameter | Mandatory | Description |
22-
| --------------- | --------- | ------------------------------------------------- |
23-
| `Name` | yes | The name of the variable to set in the pipeline |
24-
| `Value` | yes | The value of the variable to set in the pipeline |
21+
| Parameter | Mandatory | Description |
22+
| --------------------- | --------- | ------------------------------------------------- |
23+
| `Name` | yes | The name of the variable to set in the pipeline |
24+
| `Value` | yes | The value of the variable to set in the pipeline |
25+
| `AsSecret` | no | The switch to set the variable as a secret |
2526

2627
**Example**
2728

29+
Setting a variable:
30+
2831
```powershell
2932
PS> Set-AzDevOpsVariable "my-variable" "my-variable-value"
30-
# #vso[task.setvariable variable=my-variable] my-variable-value
33+
##vso[task.setvariable variable=my-variable] my-variable-value
34+
```
35+
36+
Setting a variable as a secret:
37+
38+
```powershell
39+
PS> Set-AzDevOpsVariable "my-variable" "my-variable-value" -AsSecret
40+
##vso[task.setvariable variable=my-variable;issecret=true] ***
3141
```
3242

3343
## Setting ARM outputs to Azure DevOps variable group

src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
function Set-AzDevOpsVariable {
1515
param(
1616
[parameter(Mandatory=$true)][string] $Name = $(throw "Name is required"),
17-
[parameter(Mandatory=$true)][string] $Value = $(throw "Value is required")
17+
[parameter(Mandatory=$true)][string] $Value = $(throw "Value is required"),
18+
[parameter(Mandatory=$false)][switch] $AsSecret = $false
1819
)
1920

20-
Write-Host "#vso[task.setvariable variable=$Name] $Value"
21+
if ($AsSecret) {
22+
Write-Host "##vso[task.setvariable variable=$Name;issecret=true] $Value"
23+
} else {
24+
Write-Host "##vso[task.setvariable variable=$Name] $Value"
25+
}
2126
}
2227

2328
Export-ModuleMember -Function Set-AzDevOpsVariable

src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.DevOps.tests.ps1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@ InModuleScope Arcus.Scripting.DevOps {
55
Context "Setting ARM outputs to Azure DevOps variable group" {
66
It "Setting DevOps variable should write to host" {
77
# Arrange
8-
Mock Write-Host { $Object | Should -Be "#vso[task.setvariable variable=test] value" } -Verifiable
8+
Mock Write-Host { $Object | Should -Be "##vso[task.setvariable variable=test] value" } -Verifiable
99

1010
# Act
1111
Set-AzDevOpsVariable "test" "value"
1212

1313
# Assert
1414
Assert-VerifiableMock
1515
}
16+
It "Setting DevOps variable as secret should write to host" {
17+
# Arrange
18+
Mock Write-Host { $Object | Should -Be "##vso[task.setvariable variable=test;issecret=true] value" } -Verifiable
19+
20+
# Act
21+
Set-AzDevOpsVariable "test" "value" -AsSecret
22+
23+
# Assert
24+
Assert-VerifiableMock
25+
}
1626
It "Setting DevOps variable group from ARM outputs should send info to DevOps project" {
1727
# Arrange
1828
$variableGroupName = "some-variable-group-name"

0 commit comments

Comments
 (0)