0

I can't seem to find any documentation or information on how to transform a key that exists in multiple files. The File Transform task seems to only support the transformation of unique keys. The windows web app i have setup is an OrchardCore CMS with 3 tenants, each tenant has their own appSettings.json file and in each of the files is a ConnectionString.

I initially thought there would be some way to connect a File Transform task to a specific variable in which case this would be easy but it doesnt look like this is possible. In addition to this, due to certain project requirements we can't use any extensions from the Market Place like MagicChunks.

Any help would be immensely appreciated, this has been driving me nuts.

Overide
  • 1
  • 2

2 Answers2

0

You could try to install this free 3rd-party extension: XDT Transform, and then get external task: XDT transform task in pipeline.

Dharman
  • 30,962
  • 25
  • 85
  • 135
wallezzi
  • 334
  • 1
  • 6
  • Thank you for the response Dharman but unfortunately we cannot use any tasks from the market place, it is a requirement we have to follow. We ideally want to acccomplish this with the default tasks available in the azure pipeline. – Overide May 14 '21 at 04:20
  • Currently there are no such available pre-defined tasks, you could consider to [develop one](https://learn.microsoft.com/en-us/azure/devops/extend/develop/add-build-task?view=azure-devops) if you cannot use others' extensions. – wallezzi May 14 '21 at 09:20
  • that would explain why i havent been able to find anything helpful with regards to this, seems odd not to provide this ability by default, i cant imagine this situation being that rare. Well i have about 5 days to get this done so i guess its a working weekend for me, i appreciate the link. – Overide May 14 '21 at 10:22
0

Ok I've found a temporary workaround for this but its dirty, need to modify the below so that I am updating json properties instead of replacing string values. I also don't like that this approach modifies the artifact directly. The below is a Power Shell Task with inline script and it uses pipeline variables. Hope this is helpful to someone.

# 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()
Overide
  • 1
  • 2