Skip to content

Instantly share code, notes, and snippets.

@JasonElkin
Last active September 6, 2023 16:40
Show Gist options
  • Save JasonElkin/8002451b03f35f414338db0ae938c657 to your computer and use it in GitHub Desktop.
Save JasonElkin/8002451b03f35f414338db0ae938c657 to your computer and use it in GitHub Desktop.
Environment Variable replacement for appsettings.json on AppVeyor deployment
<#
AppVeyor Deployment Script
This script is run after deployment to the web server.
It substitutes environment variables in the appsettings.json file with values from the environment.
All environment variables that start with the prefix are loaded and substituted into the appsettings.json file
e.g. if the prefix is "APP_" then the environment variable "APP_ConnectionStrings.umbracoDbDSN" will set
the value of "ConnectionStrings.umbracoDbDSN" in the appsettings.json file.
#>
Write-Host "Environment Variable Substitution" -ForegroundColor Cyan
# Environment variables to be added to appsettings.json should be prefixed with this string.
# This avoids leaking unwanted variables into the app.
if ($null -eq $env:APPLICATION_PREFIX) {
Write-Warning "APPLICATION_PREFIX environment variable not set."
Return
}
# supplyng the path as an environment variable lets us target different appsettings files
# in different environments. e.g. appsettings.Staging.json, appsettings.Production.json
if ($null -eq $env:APPSETTINGS_PATH) {
Write-Warning "APPSETTINGS_PATH environment variable not set."
Return
}
$prefix = ${env:APPLICATION_PREFIX} + "*"
$appSettingsPath = $env:APPSETTINGS_PATH
# Get all environment variables that start with the prefix as a list of name/value pairs
$props = Get-ChildItem env:$prefix | Select-Object -Property Name, Value
Write-Host "Loading config file from $appSettingsPath"
$appSettings = Get-Content -Raw $appSettingsPath | ConvertFrom-Json
foreach ($variable in $props) {
$matchString = $variable.Name.replace(${env:APPLICATION_PREFIX}, "")
$propertyPathSegments = $matchString.Split(".")
$obj = $appSettings;
for ($i = 0; $i -lt $propertyPathSegments.Length; $i++) {
$propName = $propertyPathSegments[$i]
# if we're not at the last property, check if the property exists and create it if it doesn't
if ($i -lt ($propertyPathSegments.Length - 1)) {
if ($null -eq $obj.$propName) {
Write-Host "Creating '$propName'" -ForegroundColor Gray
$newObj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name $propName -Value $newObj
}
# recurse
$obj = $obj.$propName
Continue
}
# if we're at the last property, check if it exists
if ($null -eq $obj.$propName) {
Write-Host "Creating & setting '$propName'" -ForegroundColor White
# create it if it doesn't (with the value)
$obj | Add-Member -MemberType NoteProperty -Name $propName -Value $variable.Value
}
else {
# just set the value
Write-Host "Setting '$matchString'" -ForegroundColor White
$obj.$propName = $variable.Value
}
}
}
Write-Host "Saving config file to $appSettingsPath" -ForegroundColor Gray
$appSettings | ConvertTo-Json -depth 100 | Out-File $appSettingsPath
Write-Host "Variable substitution complete" -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment