azureazure-devopsazure-devops-rest-apiazure-cli

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?


Solution

  • 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
    }