Managing Variables in Microsoft DevOps

Automating tasks in Microsoft DevOps using Python

DevOps pipelines

The problem

The Solution

The Code

import requests
import base64
import argparse
URI_BASE = "https://dev.azure.com/{orgname}"
PROJECT = "{projectname}"
VARIABLE_GROUP_ID = {variablegroupid}
parser = argparse.ArgumentParser(description='Update the environment file for testing.')parser.add_argument("--variable", type=str, dest="variable", help="Variable to update", required=True)
parser.add_argument("--PAT", type=str, dest="pat", help="Personal Access Token (PAT) with permissions to update the Variable library", required=True)
args = parser.parse_args()
def getB64String(string: str):
sbytes = string.encode("ascii")
b64bytes = base64.b64encode(sbytes)
return b64bytes.decode("ascii")
encoded_authorisation = getB64String(f":{args.pat}")base_uri = f"{URI_BASE}/{PROJECT}/_apis/distributedtask/variablegroups/{VARIABLE_GROUP_ID}?api-version=5.0-preview.1"headers = {
"Authorization": f"Basic {encoded_authorisation}"
}
response = requests.get(base_uri, headers=headers)
response_json = response.json()
response_json["variables"][f"{args.variable}"]["value"] = int(response_json["variables"][f"{args.variable}"]["value"]) + 1
update_response = requests.put(base_uri, json=response_json, headers=headers)print(f"##vso[task.setvariable variable={args.variable}]{new_value}")

All Together

import requests
import base64
import argparse
URI_BASE = "https://dev.azure.com/{orgname}"
PROJECT = "{projectname}"
VARIABLE_GROUP_ID = {variablegroupid}
parser = argparse.ArgumentParser(description='Update the environment file for testing.')
parser.add_argument("--variable", type=str, dest="variable", help="Variable to update", required=True)
parser.add_argument("--PAT", type=str, dest="pat", help="Personal Access Token (PAT) with permissions to update the Variable library", required=True)
args = parser.parse_args()def getB64String(string: str):
sbytes = string.encode("ascii")
b64bytes = base64.b64encode(sbytes)
return b64bytes.decode("ascii")
encoded_authorisation = getB64String(f":{args.pat}")
base_uri = f"{URI_BASE}/{PROJECT}/_apis/distributedtask/variablegroups/{VARIABLE_GROUP_ID}?api-version=5.0-preview.1"
headers = {
"Authorization": f"Basic {encoded_authorisation}"
}

print("URI", base_uri)
response = requests.get(base_uri, headers=headers)
response_json = response.json()
old_value = response_json["variables"][f"{args.variable}"]["value"]
response_json["variables"][f"{args.variable}"]["value"] = int(response_json["variables"][f"{args.variable}"]["value"]) + 1
new_value = response_json["variables"][f"{args.variable}"]["value"]
print(f"Updating '{args.variable}' from '{old_value}' to '{new_value}'")update_response = requests.put(base_uri, json=response_json, headers=headers)print("Update Response:", update_response.status_code)
print(f"##vso[task.setvariable variable={args.variable}]{new_value}")

Software | Security | Analytics | Risk

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store