tasks/common.tasks.ps1
# Synopsis: Allows build properties to be overriden by environment variables task ApplyEnvironmentVariableOverrides { $buildEnvVars = Get-ChildItem env:BUILDVAR_* foreach ($buildEnvVar in $buildEnvVars) { # strip the 'BUILDVAR_' prefix to leave the variable name to be overridden $varName = $buildEnvVar.Name -replace "^BUILDVAR_","" if ( !(Test-Path variable:/$varName) ) { Write-Warning "Unable to override the variable '$varName' as it is not currently defined - skipping" continue } # get the type of the existing value of the variable $varType = (Get-Variable -Name $varName).Value.GetType() # Update the value of the variable, ensuring that the existing type is preserved and # the string value from the environment variable is cast/coerced/converted accordingly. switch ($varType.Name) { "Boolean" { # booleans require special handling, due to '-as' not always behaving as required # e.g. "false" -as [boolean] returns 'True' $varValue = [System.Convert]::ToBoolean($buildEnvVar.Value) } default { $varValue = $buildEnvVar.Value -as $varType } } Write-Verbose "Overriding '$varName' from environment variable [Type=$($varType.Name)]" Set-Variable -Scope script -Name $varName -Value $varValue } } |