albbla91
albbla91

Reputation: 99

How to export multiple Azure DevOps Variable Groups?

We have multiple variable groups in our project and I am trying to find ways how I can export them to my local desktop.

So far I have this az script that exports a variable group but I can only do one at a time. I have several variables and I am trying to do all of them at once. Could someone please help on how I can export multiple variables and use the same name of that variable on Azure DevOps to match it when it's saved on the desktop?

az pipelines variable-group show --group-id "id number" --org "https://dev.azure.com/Organization" -p "Project" --output json > test.json

Also when I use the script, the value for secret is showing as "null" instead of "false"? Any reason why it's doing that?

Upvotes: 3

Views: 11677

Answers (2)

Maks Kost
Maks Kost

Reputation: 36

To download variable groups in different json file, I've created a script that collects all variable groups, gets their names, and creates folder and files for each variable group. It will create a subfolder VariableGroups in current folder.

$ProjectName = "projectName"
$Organization = "https://dev.azure.com/organizationName" 
# Get variable-groups list
$vars = az pipelines variable-group list --organization $Organization --project $ProjectName

# Get variable-groups names list
$v = $vars | ConvertFrom-Json -AsHashtable
$variable_group_names = $v.name

# Go though every variable-group
foreach ($variable_group_name in $variable_group_names) {
    $group = az pipelines variable-group list --organization $Organization --project $ProjectName --group-name $variable_group_name
    
    # Get every variable-group Id
    $g = $group | ConvertFrom-Json -AsHashtable
    $groupId = $g.id
   
    
    # Check if VariableGroups folder exist, if not - create it
    if (!(Test-Path .\VariableGroups)){
        New-Item -itemType Directory -Path .\VariableGroups
    } 
    
    # Get every variable-group content and place it in corresponding folder
    az pipelines variable-group show --organization $Organization --project $ProjectName --id $groupId  --output json>.\VariableGroups\$variable_group_name.json
    Write-Host created .\VariableGroups\$variable_group_name.json
}

Upvotes: 2

Edward Han-MSFT
Edward Han-MSFT

Reputation: 3195

You can use below command to export multiple variable groups at a time. See: az pipelines variable-group for details.

az pipelines variable-group list --org "https://dev.azure.com/Organization" --project "Project" --output json>test.json

In addition, this secret variable is encrypted in variable group by reference to this doc: Add & use variable groups, thus we can only get "null" from return result, which is by design. If you want to use secret variables in Azure Pipelines, please see: Set secret variables for guidance.

Upvotes: 5

Related Questions