'what is the way to get the variable group id of AzureDevops using CLI command

We are using AzureDevops server and automated our pipeline variable group and variables creation using a pre-build task which will run bash script below and will gerate the variabel and Variable group together

Since its a sensitive information, we would need to hide this variables value, so came across the article about the secret vraiable.. But its identified that we cant use the same az command which we were using the VG and variables together for the secret variable creation and would need to call the existing created VG by its group id and need to pass the secret value as a second step.

So here the problem i am facing is

In my curret bash script, we are using the below commands to create variable group and variable together.

az pipelines variable-group create --project=myproject --name $variable_group_name --authorize true --variables mynKey=$my_key

So if I want to split this to commands, not sure how I can obtain the group id for the created variable group.

az pipelines variable-group create  --project=$projectname --name $variable_group_name --authorize true

az pipelines variable-group variable create --group-id <?????> --name myKey --project=$projectname --secret  true --value $my_key


Solution 1:[1]

In the Response of the Azure DevOps CLI(az pipelines variable-group create), it can contain the created Variable Group ID.

enter image description here

You can directly get the ID in the CLI response.

Here is an example:

ID=$(az pipelines variable-group create --org "https://dev.azure.com/xx/" --project "azure" --name "xx" --authorize true --variables "key=value" --query 'id' -o  json)


echo  $ID

enter image description here

You can also set the Groupid as Pipeline variable with logging command.

echo "##vso[task.setvariable variable=GroupID]$ID"

Here is the doc about get value in CLI response.

Solution 2:[2]

You can use the list command for this. If you provide the full group name of the variable group you've just created, you should get its ID back:

az pipelines variable-group list -p myproject --group-name $variable_group_name

Solution 3:[3]

$group_id = $(az pipelines variable-group list -p $(System.TeamProject) --group-name "variable_group_name" --query '[0].id' -o json)

This fetches the variable group id into the $group_id variable

If the variable group does not exists then the above will simply returns empty result

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kevin Lu-MSFT
Solution 2 Yan Sklyarenko
Solution 3 Guru Prasad