1

I am trying to create a variable group in an Azure DevOps project using the REST APIs and Python scripting. The API used is:

`POST https://dev.azure.com/{organization}/_apis/distributedtask/variablegroups?api-version=7.0`

The payload in use is attached:

`vargroup_data = {
            "name": "MyVariableGroup",
            "description": "This is my variable group",
            "type": "Vsts",
            "variables": {
                "myVariable": {
                "value": "myValue",
                "isSecret": False
                },
                "myPublicVariable": {
                "value": "myPublicValue",
                "isSecret": False
                }
            },
            "variableGroupProjectReferences": {
                "projectReference": {
                    "id": project_id,
                    "name": project
                }
            }
        }`

I am receiving an error: Failed to create variable group.

`Status code: 500

Response: {"$id":"1","innerException":null,"message":"Atleast one variable group project reference is required.","typeName":"Microsoft.TeamFoundation.DistributedTask.Server.Exceptions.InvalidRequestException,Microsoft.TeamFoundation.DistributedTask.Server","typeKey":"InvalidRequestException","errorCode":0,"eventId":3000}`

What might be the problem here?

The intended output was a new variable group being created in the project.

vsk95
  • 23
  • 6

2 Answers2

1

With this payload I created Variable Group

{
  "variables": {
    "ConnectionString": {
      "value": "YourConnectionString",
      "isSecret": true
    },
    "APIKey": {
      "value": "YourAPIKey",
      "isSecret": true
    },
    "Environment": {
      "value": "Production"
    }
  },
  "variableGroupProjectReferences": [
    {
      "name": "NewVariableGroup",
      "projectReference": {
        "id": "kmadej",
        "name": "kmadej"
      }
    }
  ],
  "name": "NewVariableGroup",
  "description": "Description of your variable group"
}

Here is the part of the response I got

  "id": 4,
    "type": "Vsts",
    "name": "NewVariableGroup",
    "description": "Description of your variable group",
    "createdBy": {
        "displayName": null,
        "id": "5123c05b-58f3-6001-ad47-f3f723127d84"
    },
    "createdOn": "2023-08-31T12:32:18.6066667Z",
    "modifiedBy": {
        "displayName": null,
        "id": "5123c05b-58f3-6001-ad47-f3f723127d84"
    },
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
1
vargroup_data = {
            "description": "This is a test variable group",
            "name": "test_group",
            "providerData": None,
            "type": "Vsts",
            "variables": {"ResourceGroupLocation": {
                "isSecret": False,
                "value": "location_name"
            },
            "ResourceGroupName": {
                "isSecret": False,
                "value": "resourcegroup_name"
            }}, 
            "variableGroupProjectReferences": [{
                "description": "This is a test variable group",
                "name": "test_group",
                "projectReference": {
                    "id": self.project_id,
                    "name": self.project
                }
            }]
        }

I was able to create the variable group by modifying the payload to this. Hope this will be helpful to someone in the future.

vsk95
  • 23
  • 6