Azure Website - Load App Settings From File (Geta)

Octopus.AzurePowerShell exported 2019-01-22 by sarbis belongs to ‘Azure’ category.

Loads app settings from a json file (e.g. local.settings.json) which is also json-transformed to inject environment-specific values.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Resource group name

Parameters.ResourceGroup.Name =

Name of the target resource group.

Webapp / function name

Parameters.WebApp.Name =

Name of the target webapp.

Settings file path

Parameters.SettingsFile.Path =

Path of the JSON settings file relative to script directory. Or in most of the cases easiest is to use Octopus Action variable for the extract path: #{Octopus.Action.Package[LoadAppSettingsFromFile.Package].ExtractedPath}

Deploy slot name

Parameters.DeploySlot.Name =

Name of the deploy slot. Production slot will be updated if left blank.

Package Id

Parameters.PackageId =

Id of the referenced package which when extracted might contain the settings file.

Script body

Steps based on this template will execute the following PowerShell script.

Function Get-Parameter($Name, $Required, $Default, [switch]$FailOnValidate) {
    $result = $null
    $errMessage = [string]::Empty

    If ($null -ne $OctopusParameters) {
        $result = $OctopusParameters[$Name]
        Write-Host ("Octopus parameter value for " + $Name + ": " + $result)
    }

    If ($null -eq $result) {
        $variable = Get-Variable $Name -EA SilentlyContinue
        if ($null -ne $variable) {
            $result = $variable.Value
        }
    }

    If ($null -eq $result) {
        If ($Required) {
            $errMessage = "Mandatory parameter '$Name' not specified"
        }
        Else {
            $result = $Default
        }
    } 

    If (-Not [string]::IsNullOrEmpty($errMessage)) {
        If ($FailOnValidate) {
            Throw $errMessage
        }
        Else {
            Write-Warning $errMessage
        }
    }

    return $result
}

Function Main(
    [Parameter(Mandatory = $true)][string] $azureResourceGroupName,
    [Parameter(Mandatory = $true)][string] $azureWebAppName,
    [Parameter(Mandatory = $true)][string] $azureSettingsFilePath,
    [Parameter(Mandatory = $false)][string] $azureDeploySlotName = $null
) {
    Write-Host "Start AzureLoadAppSettingsFromFile"

    If ((Test-Path $azureSettingsFilePath) -ne $true) {
        Write-Warning "Settings file '$azureSettingsFilePath' not found!"
        Exit 0
    }

    $settingsJson = Get-Content -Raw -Path $azureSettingsFilePath | ConvertFrom-Json

    If (($settingsJson -eq $null) -or ($settingsJson.Values -eq $null)) {
        Write-Warning "Settings file '$azureSettingsFilePath' doesn't contain Values object. Unable to load app settings!"
        Exit 0
    }

    # Parse app settings into a hashtable object

    $settingsValues = $settingsJson.Values

    $appSettings = @{}
    $settingsValues.psobject.properties | Foreach { $appSettings[$_.Name] = $_.Value }

    # Set app settings for either slot or a webapp

    If ([string]::IsNullOrEmpty($azureDeploySlotName)) {
        Set-AzureRmWebApp -Name $azureWebAppName -ResourceGroupName $azureResourceGroupName -AppSettings $appSettings
    } Else {
        Set-AzureRmWebAppSlot -Name $azureWebAppName -ResourceGroupName $azureResourceGroupName -AppSettings $appSettings -Slot $azureDeploySlotName
    }

    Write-Host "End AzureLoadAppSettingsFromFile"
}

& Main `
    -azureResourceGroupName (Get-Parameter "Parameters.ResourceGroup.Name" $true "" $true) `
    -azureWebAppName (Get-Parameter "Parameters.WebApp.Name" $true "" $true) `
    -azureSettingsFilePath (Get-Parameter "Parameters.SettingsFile.Path" $true "" $true) `
    -azureDeploySlotName (Get-Parameter "Parameters.DeploySlot.Name" $false "" $true)

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "140d22e8-abe9-4a32-aab7-20af667c6255",
  "Name": "Azure Website - Load App Settings From File (Geta)",
  "Description": "Loads app settings from a json file (e.g. local.settings.json) which is also json-transformed to inject environment-specific values.",
  "Version": 7,
  "ExportedAt": "2019-01-22T09:58:08.811Z",
  "ActionType": "Octopus.AzurePowerShell",
  "Author": "sarbis",
  "Packages": [
    {
      "Id": "31f79f978a2e46f9b3add02f695e672b",
      "Name": "LoadAppSettingsFromFile.Package",
      "PackageId": "#{Parameters.PackageId}",
      "FeedId": null,
      "AcquisitionLocation": "Server",
      "Properties": {
        "Extract": "True"
      }
    }
  ],
  "Parameters": [
    {
      "Id": "e21c4265-7e92-4893-a359-3d8c4b0223c2",
      "Name": "Parameters.ResourceGroup.Name",
      "Label": "Resource group name",
      "HelpText": "Name of the target resource group.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "4485ab2e-2003-4fab-814f-90ce9a338cce",
      "Name": "Parameters.WebApp.Name",
      "Label": "Webapp / function name",
      "HelpText": "Name of the target webapp.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "f589143a-3f46-4a2b-b14f-02469ed80c19",
      "Name": "Parameters.SettingsFile.Path",
      "Label": "Settings file path",
      "HelpText": "Path of the JSON settings file relative to script directory. Or in most of the cases easiest is to use Octopus Action variable for the extract path: `#{Octopus.Action.Package[LoadAppSettingsFromFile.Package].ExtractedPath}`",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "fb46e50c-283a-4d7d-b4ec-2a171f76d8c4",
      "Name": "Parameters.DeploySlot.Name",
      "Label": "Deploy slot name",
      "HelpText": "Name of the deploy slot. Production slot will be updated if left blank.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "81b491a9-e1f5-4b22-b9d2-22df2bb60686",
      "Name": "Parameters.PackageId",
      "Label": "Package Id",
      "HelpText": "Id of the referenced package which when extracted might contain the settings file.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Azure.AccountId": "#{Azure.Subscription.Name}",
    "Octopus.Action.Package.JsonConfigurationVariablesEnabled": "True",
    "Octopus.Action.EnabledFeatures": "Octopus.Features.JsonConfigurationVariables",
    "Octopus.Action.Package.JsonConfigurationVariablesTargets": "#{Parameters.SettingsFile.Path}",
    "Octopus.Action.Script.ScriptFileName": null,
    "Octopus.Action.Package.PackageId": "#{Utilities.PackageId}",
    "Octopus.Action.Package.FeedId": "feeds-builtin",
    "Octopus.Action.Package.DownloadOnTentacle": "False",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "Function Get-Parameter($Name, $Required, $Default, [switch]$FailOnValidate) {\n    $result = $null\n    $errMessage = [string]::Empty\n\n    If ($null -ne $OctopusParameters) {\n        $result = $OctopusParameters[$Name]\n        Write-Host (\"Octopus parameter value for \" + $Name + \": \" + $result)\n    }\n\n    If ($null -eq $result) {\n        $variable = Get-Variable $Name -EA SilentlyContinue\n        if ($null -ne $variable) {\n            $result = $variable.Value\n        }\n    }\n\n    If ($null -eq $result) {\n        If ($Required) {\n            $errMessage = \"Mandatory parameter '$Name' not specified\"\n        }\n        Else {\n            $result = $Default\n        }\n    } \n\n    If (-Not [string]::IsNullOrEmpty($errMessage)) {\n        If ($FailOnValidate) {\n            Throw $errMessage\n        }\n        Else {\n            Write-Warning $errMessage\n        }\n    }\n\n    return $result\n}\n\nFunction Main(\n    [Parameter(Mandatory = $true)][string] $azureResourceGroupName,\n    [Parameter(Mandatory = $true)][string] $azureWebAppName,\n    [Parameter(Mandatory = $true)][string] $azureSettingsFilePath,\n    [Parameter(Mandatory = $false)][string] $azureDeploySlotName = $null\n) {\n    Write-Host \"Start AzureLoadAppSettingsFromFile\"\n\n    If ((Test-Path $azureSettingsFilePath) -ne $true) {\n        Write-Warning \"Settings file '$azureSettingsFilePath' not found!\"\n        Exit 0\n    }\n\n    $settingsJson = Get-Content -Raw -Path $azureSettingsFilePath | ConvertFrom-Json\n\n    If (($settingsJson -eq $null) -or ($settingsJson.Values -eq $null)) {\n        Write-Warning \"Settings file '$azureSettingsFilePath' doesn't contain Values object. Unable to load app settings!\"\n        Exit 0\n    }\n\n    # Parse app settings into a hashtable object\n\n    $settingsValues = $settingsJson.Values\n\n    $appSettings = @{}\n    $settingsValues.psobject.properties | Foreach { $appSettings[$_.Name] = $_.Value }\n\n    # Set app settings for either slot or a webapp\n\n    If ([string]::IsNullOrEmpty($azureDeploySlotName)) {\n        Set-AzureRmWebApp -Name $azureWebAppName -ResourceGroupName $azureResourceGroupName -AppSettings $appSettings\n    } Else {\n        Set-AzureRmWebAppSlot -Name $azureWebAppName -ResourceGroupName $azureResourceGroupName -AppSettings $appSettings -Slot $azureDeploySlotName\n    }\n\n    Write-Host \"End AzureLoadAppSettingsFromFile\"\n}\n\n& Main `\n    -azureResourceGroupName (Get-Parameter \"Parameters.ResourceGroup.Name\" $true \"\" $true) `\n    -azureWebAppName (Get-Parameter \"Parameters.WebApp.Name\" $true \"\" $true) `\n    -azureSettingsFilePath (Get-Parameter \"Parameters.SettingsFile.Path\" $true \"\" $true) `\n    -azureDeploySlotName (Get-Parameter \"Parameters.DeploySlot.Name\" $false \"\" $true)"
  },
  "Category": "Azure",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/azure-load-appsettings-from-file.json",
  "Website": "/step-templates/140d22e8-abe9-4a32-aab7-20af667c6255",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////AHjXf7vrv931QJrh7/f8EIDaIIncMJHfYKvmz+b3n8zw3+76j8Ttr9XycLPpUKLkkKvYFAAABGZJREFUeNrsnNmCqjoQRc1MEiD8/9cer7Yt2KBJZQC8ez07sKlKTQlcLgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAzoUSnt8YxXlFuGHSbIaxvj+fip4btkLn1blkWLaF5v03yLhLOYlVuGYfMOMZzNGxCOzhjTJqFkXnjq3Dr1yyvPI3hGl3Ih3zzHHNKudRstRhX5O58vIcShY67Gq6EPIESlzUWvazaGAOGbvU7ArDu/g8M4o8opDZWvbvPzlL/MMBE8jT9T9W7PbAJlHPTBFRf9yVTEcs63msXz2UHLSgf650G/d5t+wjbxxB2UCMqGrk8/LFSD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Nmmqbg18aMVR1L9vwSXHF9TfIWBxpKLs2hj3eQeBC0USvp2HHF3eIkRdhFOd6ER8AAAAAAAAAAAAAAAAAAAAAAAAAAAAA/I/4J8AAo/80BciBec4AAAAASUVORK5CYII=",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Tuesday, January 22, 2019