Private/Set-AzurePipelinesVariable.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function Set-AzurePipelinesVariable {
    <#
    .SYNOPSIS
        Set a envrionment variable in VSTS/Azure Pipelines that will persist between tasks

    .DESCRIPTION
        This command uses the VSTS/Azure Pipelines command task.setvariable to create an
        envrionment variable which will be available in all following tasks
        within the same stage.

    .EXAMPLE
        Set-AzurePipelinesVariable -Name ProjectName -Value (Get-ProjectName)

    .EXAMPLE
        Set-AzurePipelinesVariable -Name ProjectName -Value (Get-ProjectName) -Secret

    .LINK
        https://github.com/Microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md
    #>

    [CmdletBinding( SupportsShouldProcess = $false )]
    [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')]
    [System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingWriteHost', 'Azure Pipelines does not listen to Out-Host')]
    param (
        # Name of the variable
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        # Value of the variable
        [string]$Value,

        # The value of the variable will be saved as secret and masked out from log.
        # Secret variables are not passed into tasks as environment variables and must be passed as inputs.
        [switch]$Secret
    )

    Process {
        $_secret = ""
        if ($Secret) { $_secret = ";issecret=true" }

        Write-Verbose "storing [$Name] with Azure Pipelines task.setvariable command"
        Write-Host "##vso[task.setvariable variable=$Name$_secret]$Value"
    }
}