是否可以在项目之间克隆变量组?

mic*_*hai 4 azure-devops azure-pipelines

我知道您可以克隆变量组,但这仅限于项目内。是否可以将其克隆到不同的项目?

Lev*_*SFT 9

azure devops UI 门户中没有这样的功能可以在项目之间克隆变量组。

但是,您可以使用变量组rest api来实现此目的。

首先,您需要调用getvariablegrouprestapi来获取变量内容。

GET https://dev.azure.com/{organization}/{project}/_apis/distributedtask/variablegroups/{groupId}?api-version=5.1-preview.1

然后使用添加变量组休息 api 将新变量组添加到另一个项目,其中包含获取休息 api 中的变量内容。

POST https://dev.azure.com/{organization}/{project}/_apis/distributedtask/variablegroups?api-version=5.1-preview.1

请下面的 powershell 脚本示例:

# 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
Run Code Online (Sandbox Code Playgroud)

您还可以使用 azure Devops 命令行az pipelines variable-group create。浏览此处获取更多信息。