Azure管道-在多个json文件中转换相同的密钥



我似乎找不到任何关于如何转换多个文件中存在的键的文档或信息。"文件转换"任务似乎只支持唯一密钥的转换。我安装的windows web应用程序是一个OrchardCore CMS,有3个租户,每个租户都有自己的appSettings.json文件,每个文件中都有一个ConnectionString。

我最初认为有一些方法可以将"文件转换"任务连接到特定的变量,在这种情况下,这很容易,但看起来不可能。除此之外,由于某些项目要求,我们不能使用市场上的任何扩展,如MagicChunks。

任何帮助都将不胜感激,这让我抓狂。

您可以尝试安装这个免费的第三方扩展:XDT Transform,然后在管道中获取外部任务:XDT Transform任务。

好的,我找到了一个临时的解决方法,但它很脏,需要修改下面的内容,以便更新json属性,而不是替换字符串值。我也不喜欢这种方法直接修改工件。下面是一个带有内联脚本的PowerShell任务,它使用管道变量。希望这对某人有帮助。

# cd to the agent artifacts directory (where the zip file exist)
cd $env:Agent_ReleaseDirectory
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem");
# Open zip and find the particular file (assumes only one inside the Zip file)
$zipfileName = Get-ChildItem $(System.DefaultWorkingDirectory) -depth 4 -filter '*.zip'
$zip =  [System.IO.Compression.ZipFile]::Open($zipfileName.FullName,"Update")
$defaultAppSettings = $zip.Entries | Where-Object { $_.FullName -eq "App_Data/Sites/Default/appsettings.json" }
$secondaryAppSettings = $zip.Entries | Where-Object { $_.FullName -eq "App_Data/Sites/Secondary/appsettings.json" }
Write-Host "Update Default App Settings"
# Update Default Settings
$defaultAppSettingsFile = [System.IO.StreamReader]($defaultAppSettings).Open()
$defaultAppSettingsText = $defaultAppSettingsFile.ReadToEnd()
$defaultAppSettingsFile.Close()
$defaultAppSettingsFile.Dispose()
$defaultAppSettingsText = $defaultAppSettingsText -replace  "Server=###.###.###.###;Initial Catalog=############;MultipleActiveResultSets=true;User ID=######;Password=#######;ConnectRetryCount=0","$(Default.ConnectionString)"
$defaultAppSettingsText = $defaultAppSettingsText -replace  "#########","$(Default.AppSettings.ApiSetting.ApiKey)"
$defaultAppSettingsText = $defaultAppSettingsText -replace  "#########","$(Default.AppSettings.ApiSetting.ApiBaseUrl)"
#update file with new content
$defaultAppSettingsFile = [System.IO.StreamWriter]($defaultAppSettings).Open()
$defaultAppSettingsFile.BaseStream.SetLength(0)
# Insert the $text to the file and close
$defaultAppSettingsFile.Write($defaultAppSettingsText)
$defaultAppSettingsFile.Flush()
$defaultAppSettingsFile.Close()
Write-Host "Default App Settings Updated"
Write-Host "Update Secondary App Settings"
# Update Scoot Settings
$secondaryAppSettingsFile = [System.IO.StreamReader]($secondaryAppSettings).Open()
$secondaryAppSettingsText = $secondaryAppSettingsFile.ReadToEnd()
$secondaryAppSettingsFile.Close()
$secondaryAppSettingsFile.Dispose()
$secondaryAppSettingsText = $secondaryAppSettingsText -replace  "Server=###.###.###.###;Initial Catalog=############;MultipleActiveResultSets=true;User ID=######;Password=#######;ConnectRetryCount=0","$(Secondary.ConnectionString)"
$secondaryAppSettingsText = $secondaryAppSettingsText -replace  "#########","$(Default.AppSettings.ApiSetting.ApiKey)"
$secondaryAppSettingsText = $secondaryAppSettingsText -replace  "#########","$(Default.AppSettings.ApiSetting.ApiBaseUrl)"
#update file with new content
$secondaryAppSettingsFile = [System.IO.StreamWriter]($secondaryAppSettings).Open()
$secondaryAppSettingsFile.BaseStream.SetLength(0)
# Insert the $text to the file and close
$secondaryAppSettingsFile.Write($secondaryAppSettingsText)
$secondaryAppSettingsFile.Flush()
$secondaryAppSettingsFile.Close()
Write-Host Secondary App Settings Updated"
# Write the changes and close the zip file
$zip.Dispose()

相关内容

  • 没有找到相关文章

最新更新