skip to Main Content

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


  1. 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 use az 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 inside projectReference with your project name.

    {
      "authorized": false,
      "description": "Holds general variables",
      "name": "Var1",
      "providerData": null,
      "type": "Vsts",
      "variables": {
        "Var1": {
          "isSecret": false,
          "value": "TestVar1"
            },
        "Var2": {
          "isSecret": false,
          "value": "TestVar2"
            },
        "Var3": {
          "isSecret": true,
          "value": "TestVar3"
        }
        },
      "variableGroupProjectReferences": [
            {
                "projectReference": {
                    "name": "{project name}"
                },
                "name": "Var1",
                "description": "Holds general variables"
            }
        ]
    }
    

    Run REST API in PowerShell

    Please replace the value of organization and token.

    # Read the JSON file and convert it into a string
    $jsonbody = Get-Content -Path "C:UsersAdministratorDesktopvariablegroup.json" | Out-String
    # Define organization name and PAT
    $organization = "{organization name}"
    $token = "{PAT}"
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    $url="https://dev.azure.com/$organization/_apis/distributedtask/variablegroups?api-version=7.1-preview.2"
    $head = @{ Authorization =" Basic $token" }
    Invoke-RestMethod -Uri $url -Method Post -Headers $head -Body $jsonbody -ContentType application/json
    

    Result

    Then you can see the variable group as shown below, which contains a secret variable.
    enter image description here

    Login or Signup to reply.
  2. How to import json file saved in local directory (desktop) to Azure DevOps Library Variable Group using az cli?

    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

        $json = Get-Content -Path "C:VenkatVenkatvar.json" | Out-String
        $variableGroup = $json | ConvertFrom-Json
    
        $groupName = $variableGroup.name
        $description = $variableGroup.description
        $variablesObject = $variableGroup.variables
        
        $variablesHashtable = @{}
        foreach ($property in $variablesObject.PSObject.Properties) {
            $variableName = $property.Name
            $variableValue = $property.Value.value
            $variablesHashtable[$variableName] = $variableValue
        }
        $variablesString = $variablesHashtable.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }
        
        # Create variable group using Azure CLI
        az pipelines variable-group create --name $groupName --variables $variablesString --description $description --detect false --org 'https://dev.azure.com/<org-Name>' --project '<Project-name>'
    

    Output:

    enter image description here

    Once the script is run, the variable group and variables are created in my project.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search