This is contents for the json file
{
"authorized": false,
"description": "Holds general variables",
"name": "Var",
"providerData": null,
"type": "Vsts",
"variables": {
"Var1": {
"isSecret": false,
"value": "TestVar1"
},
"Var2": {
"isSecret": false,
"value": "TestVar2"
}
}
}
This is what I was trying to run.
$json = Get-Content -Path C:variablesvar.json out-string | out-string
$name = $json | ConvertFrom-Json
$varname = $name.variables
az pipelines variable-group create --name 'Var' --variables $varname --description 'This is a test variable' --detect false --org 'https://dev.azure.com/{organization}' --project '{project name or id}'
Any help would be appreciated as I have multiple variables to import to Azure DevOps.
2
Answers
To use az pipelines variable-group create, variables in format key=value space separated pairs. For example,
--variables 'var1=1' 'var2=2'
. Besides, you will need to useaz pipelines variable-group variable
commands to manage secret variables. Using the az CLI not only requires parsing the variables into the key=value format from the json file, but also processes the secret variables separately, which is a bit complicated.As an easy way, it’s suggested to use REST API Variablegroups – Add. Then you only need to make simple modifications to your json files and do not need to deal with secret variables separately. Refer to the steps below.
Modify your json files
Add
variableGroupProjectReferences
to your Json file. Replace the name insideprojectReference
with your project name.Run REST API in PowerShell
Please replace the value of
organization
andtoken
.Result
Then you can see the variable group as shown below, which contains a secret variable.
Here is the updated script for creating a variable group with multiple variables inside the group, collected from the ‘var.json’ file using
Azure CLI
Output:
Once the script is run, the
variable group
and variables are created in my project.