无法使用表达式将 json 文件导入到 Azure Devops 库

问题描述 投票:0回答:1

如何将 json 文件导入到 Azure DevOps 库变量组,其中变量名称和值包含 $() 或 $[] 等表达式。

这是我用来导入 json 文件的脚本。

$jsonFilesDirectory = "C:\variables" 
$jsonFiles = Get-ChildItem -Path $jsonFilesDirectory -Filter *.json 
foreach ($json in $jsonFiles) 
{ $jsonnames = $json.Name 
$jsonContent = Get-Content -Path "C:\variables\$jsonnames" | Out-String 

    $variableGroup = $jsonContent | ConvertFrom-Json

    $groupName = $variableGroup.name
    $description = $variableGroup.description
    $variablesObject = $variableGroup.variables
    
    $variablesHashtable = @{}
    foreach ($property in $variablesObject.PSObject.Properties) {
        $variableName = $property.Name
        $variableValue = $property.Value.value
        $variablesHashtable[$variableName] = $variableValue
    }
    $variablesString = $variablesHashtable.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" } 
    
    # Create variable group using Azure CLI
    az pipelines variable-group create --name $groupName --variables $variablesString --description $description --detect false --org 'https://dev.azure.com/<org>' --project '<project>'

}

我收到以下错误。

az : --description was unexpected at this time.
At line:22 char:5
+     az pipelines variable-group create --name $groupName --variables  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (--description w...d at this time.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
C:\Users\******>  "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\\..\python.exe" -IBm azure.cli pipelines variable-group create --name "Test Variable" --variables BusinessRulesAssemblyLocalStoragePath=test-$(env) --description "Test upload" --detect false --org https://dev.azure.com/****** --project "*******"

这是json文件。

{
  "authorized": false,
  "description": "Test upload",
  "name": "Test",
  "project": "****",
  "type": "Vsts",
  "variables": {
    "BusinessRulesAssemblyLocalStoragePath": {
      "isSecret": false,
      "value": "test-$(env)"
    }
  }
}

如果我删除表达式就可以了。

$(环境)

azure azure-devops azure-cli
1个回答
0
投票

根据我的测试,az CLI 似乎不支持 $()。作为解决方法,您可以使用 REST API Variablegroups - Add 创建变量组。

variableGroupProjectReferences
添加到您的 Json 文件中。将
projectReference
内的名称替换为您的项目名称。例如,

{
  "authorized": false,
  "description": "Test upload",
  "name": "Test",
  "type": "Vsts",
  "variables": {
    "BusinessRulesAssemblyLocalStoragePath": {
      "isSecret": false,
      "value": "test-$(env)"
    }
  },
  "variableGroupProjectReferences": [
        {
            "projectReference": {
                "name": "{project name}"
            },
            "name": "Test",
            "description": "Test upload"
        }
    ]
}

然后运行以下 PowerShell 脚本:

$jsonFilesDirectory = "C:\variables" 
$jsonFiles = Get-ChildItem -Path $jsonFilesDirectory -Filter *.json 
foreach ($json in $jsonFiles) 
{ $jsonnames = $json.Name 
  $jsonContent = Get-Content -Path "C:\variables\$jsonnames" | Out-String 
  $organization = "{Organization name}"
  $token = "{PAT}"
  $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
  $url="https://dev.azure.com/$organization/_apis/distributedtask/variablegroups?api-version=7.1-preview.2"
  $head = @{ Authorization =" Basic $token" }
  Invoke-RestMethod -Uri $url -Method Post -Headers $head -Body $jsonContent -ContentType application/json

}
© www.soinside.com 2019 - 2024. All rights reserved.