Created
July 14, 2019 20:52
-
-
Save mitchgollub/ebce195ab188c4f4b9f235229e7866e1 to your computer and use it in GitHub Desktop.
net-core-appsettings-json-variable-replacement-in-appveyor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Write-Host "Environment Variable Substitution" | |
$variables = gci $env:$env:APPLICATION_PREFIX* | Select-Object -Property Key, Value | |
$configPath = "$env:APPLICATION_PATH\$env:CONFIG_FILE" | |
Write-Output "Loading config file from $configPath" | |
$appSettings = Get-Content -Raw $configPath | ConvertFrom-Json | |
foreach($variable in $variables) { | |
$matchString = $variable.Key.replace($env:APPLICATION_PREFIX, "") | |
$matchProperties = $matchString.Split(".") | |
if($matchProperties.Count -gt 1) { | |
$match = $appSettings.($matchProperties[0]).psobject.properties | where { $_.Name -eq $matchProperties[1] } | |
if ($match) { | |
$appSettings.($matchProperties[0]).($matchProperties[1]) = $variable.Value | |
} | |
else { | |
Write-Output "Could not find match for $matchString" | |
} | |
} | |
else { | |
$match = $appSettings.psobject.properties | where { $_.Name -eq $matchString } | |
if ($match) { | |
$appSettings.($matchString) = $variable.Value | |
} | |
else { | |
Write-Output "Could not find match for $matchString" | |
} | |
} | |
} | |
$appSettings | ConvertTo-Json -depth 100 | Out-File $configPath |
@mitchgollub I wasn't able to send direct messages so I had to create it as a page:
https://gist.github.com/eivindivine/83cbfbf2efb97575ec7c1268459a05a0/041585263d9c428a4d8506510626c875948abc58
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @eivindivine,
That's great news! I was testing it out a little yesterday and had noticed my actual final version of the code is hard-coded with the application prefix
$variables = gci $env:APPID_* | Select-Object -Property Key, Value
So thats my mistake. If you'd like to share your changes, I'd be happy to update this file and credit you.
Also your assumptions on the variable replacement configuration are correct, but you probably knew that already.
Thanks!