Azure 管道 - 在 json 数组 VSTS 管道中添加新元素 (Appsettings.json)
Azure pipelines -Add new element in json array VSTS pipelines (Appsettings.json)
是否可以在 Azure Release Pipeline 的 appsetting.json
数组中添加新元素?
在 appsetting.json
中,我有一个数组变量,我需要在通过 Azure Pipeline 部署期间用另一个元素填充它。
"Array": [
{
"Name": "AD1",
"IsDefault": "true",
"IdPEntityId": "URL1",
"Metadata": "XMLpath1"
},
{
"Name": "AD2",
"IsDefault": "false",
"IdPEntityId": "URL2",
"Metadata": "XMLPath2"
}
]
在上面的 JSON 数组中,我需要添加另一个元素的最后位置 (array-Index:2)。
您可以使用 JSON 变量替换。此功能替换 JSON 配置文件中的值。它使用与发布管道和阶段变量名称匹配的值覆盖指定的 JSON 配置文件(例如 appsettings.json)中的值。
在 "Deploy Azure App Service" 发布任务中,您应该看到“文件转换和变量替换”部分。在这里,您将提供要交换变量值的 json 文件的路径。
[![在此处输入图片描述][1]][1]
然后您只需在发布管道或阶段变量中定义所需的替换值。从这里您可以添加要修改的 json 属性 作为变量。
[![在此处输入图片描述][2]][2]
最后在改造后,JSON会包含新的。 Azure DevOps 将在部署时为你换出这些值。
更多细节你可以在这里参考我们的官方教程:[文件转换和变量替换参考][3]
更新:
它只能调整 appsettings.json 文件中的现有条目,似乎无法添加任何新条目。您还可以查看 JSON variable substitution notes
Variable substitution is applied for only the JSON keys predefined in
the object hierarchy. It does not create new keys.
作为解决方法,您可以选择使用 File Creator 扩展:https://marketplace.visualstudio.com/items?itemName=eliostruyf.build-task 将整个新 appsettings.json 文件推送到管道中。
更新2
OP 最终使用他编写的 PS 脚本在 Appsettings.json
的数组中添加新元素
[CmdletBinding()]
param(
[string] $AdName,
[bool] $AdIsDefault,
[string] $AdIdPEntityId,
[string] $AdMetadata,
[string] $AppSettingFilePath
)
clear-Host
Write-Host 'Updating appsettings.json...' -ForegroundColor Yellow
function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
$indent = 0;
($json -Split '\n' |
% {
if ($_ -match '[\}\]]') {
# This line contains ] or }, decrement the indentation level
$indent--
}
$line = (' ' * $indent * 2) + $_.TrimStart().Replace(': ', ': ')
if ($_ -match '[\{\[]') {
# This line contains [ or {, increment the indentation level
$indent++
}
$line
}) -Join "`n"
}
$JsonDataAdd=@"
{
"Name":"$AdName",
"IsDefault": "$AdIsDefault",
"IdPEntityId":"$AdIdPEntityId",
"Metadata": "$AdMetadata"
}
"@
Write-Host ' Active directory details :' -ForegroundColor Yellow
Write-Host `n $JsonDataAdd -ForegroundColor Green
$jsonData = Get-Content "$AppSettingFilePath" | Out-String | ConvertFrom-Json -ErrorAction Stop
$jsonData.IdentitySettings.ExternalProviders.Saml2Providers += (ConvertFrom-Json $JsonDataAdd)
$jsonData | ConvertTo-Json -Depth 10 | Format-Json | Set-Content "$AppSettingFilePath" -Encoding UTF8
Write-Host 'Successfully Updated -appSettings.json !' -ForegroundColor Yellow
是否可以在 Azure Release Pipeline 的 appsetting.json
数组中添加新元素?
在 appsetting.json
中,我有一个数组变量,我需要在通过 Azure Pipeline 部署期间用另一个元素填充它。
"Array": [
{
"Name": "AD1",
"IsDefault": "true",
"IdPEntityId": "URL1",
"Metadata": "XMLpath1"
},
{
"Name": "AD2",
"IsDefault": "false",
"IdPEntityId": "URL2",
"Metadata": "XMLPath2"
}
]
在上面的 JSON 数组中,我需要添加另一个元素的最后位置 (array-Index:2)。
您可以使用 JSON 变量替换。此功能替换 JSON 配置文件中的值。它使用与发布管道和阶段变量名称匹配的值覆盖指定的 JSON 配置文件(例如 appsettings.json)中的值。
在 "Deploy Azure App Service" 发布任务中,您应该看到“文件转换和变量替换”部分。在这里,您将提供要交换变量值的 json 文件的路径。
[![在此处输入图片描述][1]][1]
然后您只需在发布管道或阶段变量中定义所需的替换值。从这里您可以添加要修改的 json 属性 作为变量。
[![在此处输入图片描述][2]][2]
最后在改造后,JSON会包含新的。 Azure DevOps 将在部署时为你换出这些值。
更多细节你可以在这里参考我们的官方教程:[文件转换和变量替换参考][3]
更新:
它只能调整 appsettings.json 文件中的现有条目,似乎无法添加任何新条目。您还可以查看 JSON variable substitution notes
Variable substitution is applied for only the JSON keys predefined in the object hierarchy. It does not create new keys.
作为解决方法,您可以选择使用 File Creator 扩展:https://marketplace.visualstudio.com/items?itemName=eliostruyf.build-task 将整个新 appsettings.json 文件推送到管道中。
更新2
OP 最终使用他编写的 PS 脚本在 Appsettings.json
的数组中添加新元素[CmdletBinding()]
param(
[string] $AdName,
[bool] $AdIsDefault,
[string] $AdIdPEntityId,
[string] $AdMetadata,
[string] $AppSettingFilePath
)
clear-Host
Write-Host 'Updating appsettings.json...' -ForegroundColor Yellow
function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
$indent = 0;
($json -Split '\n' |
% {
if ($_ -match '[\}\]]') {
# This line contains ] or }, decrement the indentation level
$indent--
}
$line = (' ' * $indent * 2) + $_.TrimStart().Replace(': ', ': ')
if ($_ -match '[\{\[]') {
# This line contains [ or {, increment the indentation level
$indent++
}
$line
}) -Join "`n"
}
$JsonDataAdd=@"
{
"Name":"$AdName",
"IsDefault": "$AdIsDefault",
"IdPEntityId":"$AdIdPEntityId",
"Metadata": "$AdMetadata"
}
"@
Write-Host ' Active directory details :' -ForegroundColor Yellow
Write-Host `n $JsonDataAdd -ForegroundColor Green
$jsonData = Get-Content "$AppSettingFilePath" | Out-String | ConvertFrom-Json -ErrorAction Stop
$jsonData.IdentitySettings.ExternalProviders.Saml2Providers += (ConvertFrom-Json $JsonDataAdd)
$jsonData | ConvertTo-Json -Depth 10 | Format-Json | Set-Content "$AppSettingFilePath" -Encoding UTF8
Write-Host 'Successfully Updated -appSettings.json !' -ForegroundColor Yellow