Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an array variable defined in Azure DevOps "variable group"

I want to define some variables in Azure devops "variable group" which will be used in Powershell, but when the variable type is string it works, but some is array or object went wrong. mine look like below. left is the name,right is the value

vmAlertedArray_backup => @("wbubuntu","wbubuntu2")

1.when in azure devops script I use, it went wrong

$vmAlertedArray_backup = $env:vmAlertedArray_backup foreach($c in $vmAlertedArray_backup){ Write-Host "$c" }

2.below in powershell in local works

$vmAlertedArray_backup = @("wbubuntu","wbubuntu2") foreach($c in $vmAlertedArray_backup){ Write-Host "$c" }

Can any one show some experience about this? thanks

like image 403
wenbo Avatar asked Jul 24 '19 03:07

wenbo


People also ask

What are variable groups in Azure DevOps?

Variable groups store values and secrets that you might want to be passed into a YAML pipeline or make available across multiple pipelines. You can share and use variable groups in multiple pipelines in the same project. Variable groups are protected resources.

How do I get Azure DevOps variable group ID?

In the Response of the Azure DevOps CLI( az pipelines variable-group create ), it can contain the created Variable Group ID. You can directly get the ID in the CLI response. You can also set the Groupid as Pipeline variable with logging command. Here is the doc about get value in CLI response.

How do we set different environment variables for individual agents in Azure DevOps?

There are 3 ways to get an environment variable value on your build server: Set the value on the build machine. Set the value in the YAML build script. Set the value in Azure DevOps for the build pipeline definition.


2 Answers

It is suggested to only pass variables as string. If you want to pass an object to other tasks, you can use "ConvertTo-Json -Compress" to convert it to a json string.

$objectString = $object | ConvertTo-Json -Compress
Write-Host "##vso[task.setvariable variable=objectString;]$objectString"

And in the next PS task, you can pass it as environment variable. But, please enclose the variables in single quotes.

enter image description here

And then you can use "ConvertFrom-Json" to convert the string to an object.

$getFromEnv = $env:objectString | ConvertFrom-Json
foreach( $obj in $getFromEnv){
    Write-Host ("displayName:{0} Id:{1}" -f $obj.displayName, $obj.Id)
} 
like image 118
Jack Jia Avatar answered Oct 13 '22 21:10

Jack Jia


I just pass the variable in as a string and then split it to create an array in PowerShell

Variable = Prod1,Prod2,Prod3

$array = $variable.Split(',)

If need be you can add a Trim to the end in case there are spaces

Variable = Prod1,Prod2 ,Prod3

$array = $workspaces.Split(',').trim()
like image 40
SQLDBAWithABeard Avatar answered Oct 13 '22 21:10

SQLDBAWithABeard