I know you can clone Variable Groups, but this is limited within the Project. Is it possible to clone it to a different Project?
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.
An approach - using curl, jq and xargs - that has worked for me:
curl -H "Content-Type: application/json" -X GET -u 'username':'<PAT>' 'https://dev.azure.com/<organization>/<url encoded source project-name>/_apis/distributedtask/variablegroups?api-version=6.1-preview.1' \
| jq -c '.value[] | del(.id, .createdBy, .modifiedBy, .createdOn, .modifiedOn)' \
| xargs -d'\n' -L1 -I'{}' \
curl -H "Content-Type: application/json" -X POST -u 'username':'<PAT>' -d "{}" 'https://dev.azure.com/<organization>/<url encoded target project-name>/_apis/distributedtask/variablegroups?api-version=6.1-preview.1'
curl command gets all available variable groups from the source projectjq removes the specified keys from each json object and outputs them line by line (-c)xargs splits the input by newline (-d'\n') and passes each line (-L1) to the curl command, that creates a new variable group in the target project