Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added switch to set variable as secret #380

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions docs/preview/03-Features/powershell/azure-devops.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
pim-simons marked this conversation as resolved.
Show resolved Hide resolved

**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
Expand Down
9 changes: 7 additions & 2 deletions src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ 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"

# 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"
Expand Down