Compare Two Variable Groups

Max Yermakhanov
ObjectSharp (a Centrilogic Company)
2 min readMar 10, 2020

Recently, I found myself in a situation where I need to compare two very large variable groups and let me tell you it was not fun. Not fun at all! So, I got bored very quickly comparing two very dull set of screens to one another and decided to write a script to do it for me. After all, that’s why we have (and love) automation, right?

Below is a simple bash script that gets variables from two variable groups in Azure DevOps and compares them using Azure DevOps REST API and the magic of jq. Thus, saving me tons of time and helping me to keep my sanity:

ORGANIZATION=ENTER_YOUR_ORGANIZATION_HERE
PROJECT=ENTER_YOUR_PROJECT_HERE
TOKEN=ENTER_YOUR_TOKEN_HERE
variablegroup1="ENTER_VARIABLE_GROUP_1_HERE"
variablegroup2="ENTER_VARIABLE_GROUP_2_HERE"
echo "getting variable group ID for the first variable group"
variablegroup1_id_URL="https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/distributedtask/variablegroups?groupName=$variablegroup1*&queryOrder=IdDescending&api-version=5.1-preview.1"
variablegroup1_id=$(curl -s -X GET -u PAT:$PAT $variablegroup1_id_URL | jq -r '.value[].id')
echo "getting variable group ID for the second variable group"
variablegroup2_id_URL="https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/distributedtask/variablegroups?groupName=$variablegroup2*&queryOrder=IdDescending&api-version=5.1-preview.1"
variablegroup2_id=$(curl -s -X GET -u PAT:$PAT $variablegroup2_id_URL | jq -r '.value[].id')
echo "getting variables from the first variable group"
variablegroup1_URL="https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/distributedtask/variablegroups?groupIds=$variablegroup1_id&api-version=5.1-preview.1"
curl -s -X GET -u PAT:$PAT $variablegroup1_URL | jq -r '.value[].variables' > variablegroup1_content.json
echo "getting variables from the second variable group"
variablegroup2_URL="https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/distributedtask/variablegroups?groupIds=$variablegroup2_id&api-version=5.1-preview.1"
curl -s -X GET -u PAT:$PAT $variablegroup2_URL | jq -r '.value[].variables' > variablegroup2_content.json
# compare two variable groups and output the differences to the console
echo "$variablegroup1 vs. $variablegroup2"
diff <(jq -S '.' variablegroup1_content.json) <(jq -S '.' variablegroup2_content.json)
#cleanup
rm -f variablegroup1_content.json variablegroup2_content.json

That’s all. Hopefully, you will find this script helpful one day and it will help you minimize the amount of boredom in your day to day.

#DevOpsGuy

--

--