Sitecore - Settings & Variable Replacement

Octopus.Script exported 2016-06-13 by dthunziker belongs to ‘Sitecore’ category.

The default Configuration Variables functionality replaces appSettings and connectionStrings entries. This step template extends this functionality to the Sitecore configuration settings and sc.variable nodes within the configuration file(s) that you specify. Variables that are defined for the Octopus project will automatically replace those defined in the target Sitecore configuration file(s).

Parameters

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

Sitecore.ReplaceConfigFiles

Sitecore.ReplaceConfigFiles

Enter the full path to your Sitecore configuration file(s) that contains the sitecore node, one per line. For versions of Sitecore prior to 8.1, this should point to your primary Web.config at the root of your website, and for versions 8.1+, to the Sitecore.config file within your App_Config folder. Alternatively, this value can be left empty and defined within your Octopus project’s variable collection using the variable name Sitecore.ReplaceConfigFiles.

Script body

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

$ErrorActionPreference = "Stop" 
$configFiles = $OctopusParameters["Sitecore.ReplaceConfigFiles"]

if ([string]::IsNullOrEmpty($configFiles)) {
    throw [System.ArgumentNullException] "Sitecore.ReplaceConfigFiles"
}

($configFiles -split '[\r\n]') | ForEach-Object {
    
    $configPath = $_

    if ([string]::IsNullOrEmpty($configPath)) { 
        return
    }
    
    $configPath = $configPath.Trim()

    if (-not (Test-Path -LiteralPath $configPath)) {
        Write-Host "$configPath was not found."
        return
    }

    Write-Host "Searching Sitecore config file for replacement variables:" $configPath
        
    $configXml = [xml](Get-Content $configPath)
    $sitecoreNode = $configXml.sitecore
    
    # Look for sitecore node for versions prior to 8.1
    if ($sitecoreNode -eq $null) {
        $sitecoreNode = $configXml.configuration.sitecore
    }
    
    # Ensure that we have a sitecore node to work from
    if ($sitecoreNode -eq $null -or $sitecoreNode.settings -eq $null) {
        Write-Host "The sitecore settings node was not found in" $configPath ". Skipping this file..."
        return
    }
    
    foreach ($key in $OctopusParameters.Keys) {
    
        # Replace Sitecore settings
        $setting = $sitecoreNode.settings.setting | where { $_.name -ceq $key }
        if ($setting -ne $null) {
            Write-Host $setting.name "setting will be updated from" $setting.value "to" $OctopusParameters[$key] "in" $configPath
            $setting.value = $OctopusParameters[$key]
        }
    
        # Replace Sitecore variables
        $variable = $sitecoreNode.'sc.variable' | where { $_.name -ceq $key }
        if ($variable -ne $null) {
            Write-Host $variable.name "Sitecore variable will be updated from" $settingsNode.value "to" $OctopusParameters[$key] "in" $configPath
            $variable.value = $OctopusParameters[$key]
        }
    
    }
    
    $configXml.Save($configPath)
}

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": "382b9610-64c4-4c64-a31c-34d07f262ed4",
  "Name": "Sitecore - Settings & Variable Replacement",
  "Description": "The default [Configuration Variables](https://octopus.com/docs/deployment-process/configuration-features#Configurationfiles-ConfigurationVariables) functionality replaces **appSettings** and **connectionStrings** entries. This step template extends this functionality to the Sitecore configuration **settings** and **sc.variable** nodes within the configuration file(s) that you specify. Variables that are defined for the Octopus project will automatically replace those defined in the target Sitecore configuration file(s).",
  "Version": 2,
  "ExportedAt": "2016-06-13T17:19:09.003+00:00",
  "ActionType": "Octopus.Script",
  "Author": "dthunziker",
  "Parameters": [
    {
      "Name": "Sitecore.ReplaceConfigFiles",
      "Label": "Sitecore.ReplaceConfigFiles",
      "HelpText": "Enter the full path to your Sitecore configuration file(s) that contains the **sitecore** node, one per line. For versions of Sitecore prior to 8.1, this should point to your primary Web.config at the root of your website, and for versions 8.1+, to the Sitecore.config file within your App_Config folder. Alternatively, this value can be left empty and defined within your Octopus project's variable collection using the variable name **Sitecore.ReplaceConfigFiles**.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "MultiLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.ScriptBody": "$ErrorActionPreference = \"Stop\" \n$configFiles = $OctopusParameters[\"Sitecore.ReplaceConfigFiles\"]\n\nif ([string]::IsNullOrEmpty($configFiles)) {\n    throw [System.ArgumentNullException] \"Sitecore.ReplaceConfigFiles\"\n}\n\n($configFiles -split '[\\r\\n]') | ForEach-Object {\n    \n    $configPath = $_\n\n    if ([string]::IsNullOrEmpty($configPath)) { \n        return\n    }\n    \n    $configPath = $configPath.Trim()\n\n    if (-not (Test-Path -LiteralPath $configPath)) {\n        Write-Host \"$configPath was not found.\"\n        return\n    }\n\n    Write-Host \"Searching Sitecore config file for replacement variables:\" $configPath\n        \n    $configXml = [xml](Get-Content $configPath)\n    $sitecoreNode = $configXml.sitecore\n    \n    # Look for sitecore node for versions prior to 8.1\n    if ($sitecoreNode -eq $null) {\n        $sitecoreNode = $configXml.configuration.sitecore\n    }\n    \n    # Ensure that we have a sitecore node to work from\n    if ($sitecoreNode -eq $null -or $sitecoreNode.settings -eq $null) {\n        Write-Host \"The sitecore settings node was not found in\" $configPath \". Skipping this file...\"\n        return\n    }\n    \n    foreach ($key in $OctopusParameters.Keys) {\n    \n        # Replace Sitecore settings\n        $setting = $sitecoreNode.settings.setting | where { $_.name -ceq $key }\n        if ($setting -ne $null) {\n            Write-Host $setting.name \"setting will be updated from\" $setting.value \"to\" $OctopusParameters[$key] \"in\" $configPath\n            $setting.value = $OctopusParameters[$key]\n        }\n    \n        # Replace Sitecore variables\n        $variable = $sitecoreNode.'sc.variable' | where { $_.name -ceq $key }\n        if ($variable -ne $null) {\n            Write-Host $variable.name \"Sitecore variable will be updated from\" $settingsNode.value \"to\" $OctopusParameters[$key] \"in\" $configPath\n            $variable.value = $OctopusParameters[$key]\n        }\n    \n    }\n    \n    $configXml.Save($configPath)\n}",
    "Octopus.Action.Script.ScriptFileName": null,
    "Octopus.Action.Package.NuGetFeedId": null,
    "Octopus.Action.Package.NuGetPackageId": null
  },
  "Category": "Sitecore",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/sitecore-settings-variable-replacement.json",
  "Website": "/step-templates/382b9610-64c4-4c64-a31c-34d07f262ed4",
  "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