I know you can clone Variable Groups, but this is limited within the Project. Is it possible to clone it to a different Project?
There is no such a feature to clone variable groups between Projects in azure devops UI portal.
However, you can achieve this using variable group rest api.
First, you need to call get variable group rest api to get the variables content.
GET https://dev.azure.com/{organization}/{project}/_apis/distributedtask/variablegroups/{groupId}?api-version=5.1-preview.1
Then use add variable group rest api to add the a new variable group to another project with the variable content from the Get rest api.
POST https://dev.azure.com/{organization}/{project}/_apis/distributedtask/variablegroups?api-version=5.1-preview.1
Please below powershell script example:
# Get the variable group in projectA
$url = "https://dev.azure.com/{organization}/{projectA}/_apis/distributedtask/variablegroups/{groupId}?api-version=5.1-preview.1"
$PAT = "Personal access token"
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$result=Invoke-RestMethod -Uri $url -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} -Method get -ContentType "application/json"
# Call add variable group rest api to add variable group in ProjectB
$updateurl = "https://dev.azure.com/{organization}/{projectB}/_apis/distributedtask/variablegroups?api-version=5.1-preview.1"
$body = $result | ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri $updateurl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -ContentType "application/json" -Method post -Body $body
You can also use azure devops command line az pipelines variable-group create. See here for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With