Skip to content

Instantly share code, notes, and snippets.

@daniiiol
Last active December 7, 2016 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniiiol/5fdbba8e991c9a7fda8a307701a0c750 to your computer and use it in GitHub Desktop.
Save daniiiol/5fdbba8e991c9a7fda8a307701a0c750 to your computer and use it in GitHub Desktop.
Replace Octopus Variables in Sitecore Configs to environment specific values
{
"Id": "ActionTemplates-64",
"Name": "Replace Octopus Vars in Sitecore Configs",
"Description": "This Script searchs through all *.config files and replaces sc.variables and Sitecore settings with the value in Octopus of the same key.",
"ActionType": "Octopus.Script",
"Version": 16,
"Properties": {
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.RunOnServer": "false",
"Octopus.Action.Script.ScriptBody": "echo (\"Replace Octopus Variables in Sitecore Configs to environment specific values...\")\r\n\r\nif ($OctopusParameters['ActivateReplacing'] -eq \"True\") {\r\n Set-Location -path $OctopusParameters['Octopus.Action[Deploy NuGet Package].Output.Package.InstallationDirectoryPath']\r\n Set-Location .\\App_Config\r\n $include = $OctopusParameters['IncludeStatement']\r\n $includeArray = @()\r\n \r\n if($include -ne $null) {\r\n $includeArray = $include.Replace(\" \", \"\").Split(',')\r\n }\r\n \r\n $exclude = $OctopusParameters['ExcludeStatement']\r\n $excludeArray = @()\r\n \r\n if($exclude -ne $null) {\r\n $excludeArray = $exclude.Replace(\" \", \"\").Split(',')\r\n }\r\n\r\n Get-ChildItem -Recurse -Include $includeArray -Exclude $excludeArray | \r\n Foreach-Object {\r\n $configPath = $_.FullName\r\n $content = Get-Content $configPath\r\n \r\n Write-Host \"Scan Sitecore config file:\" $configPath\r\n \r\n $configXml = [xml](Get-Content $configPath)\r\n $sitecoreNode = $configXml.sitecore\r\n \r\n # Look for sitecore node for versions prior to 8.1\r\n if ($sitecoreNode -eq $null) {\r\n $sitecoreNode = $configXml.configuration.sitecore\r\n }\r\n \r\n # Ensure that we have a sitecore node to work from\r\n if ($sitecoreNode -eq $null -or $sitecoreNode.settings -eq $null) {\r\n Write-Host \"The sitecore settings node was not found in\" $configPath \". Skipping this file...\"\r\n return\r\n }\r\n \r\n foreach ($key in $OctopusParameters.Keys) {\r\n # Replace Sitecore settings\r\n $setting = $sitecoreNode.settings.setting | where { $_.name -ceq $key }\r\n if ($setting -ne $null) {\r\n if ($setting.value -eq $null -and $setting.HasChildNodes -and ($setting.FirstChild.name -ne $null) -and ($setting.FirstChild.name -eq \"value\")) {\r\n # Replace a patch:setting\r\n Write-Host \"Transformation: \" $setting.name \"patch:setting will be updated from\" $setting.FirstChild.InnerText \"to\" $OctopusParameters[$key] \"in\" $configPath\r\n $setting.FirstChild.InnerText = $OctopusParameters[$key]\r\n }\r\n else {\r\n # Replace a normal setting\r\n Write-Host \"Transformation: \" $setting.name \"setting will be updated from\" $setting.value \"to\" $OctopusParameters[$key] \"in\" $configPath\r\n $setting.value = $OctopusParameters[$key]\r\n }\r\n \r\n }\r\n \r\n # Replace Sitecore variables\r\n $variable = $sitecoreNode.'sc.variable' | where { $_.name -ceq $key }\r\n if ($variable -ne $null) {\r\n Write-Host \"Transformation: \" $variable.name \"Sitecore variable will be updated from\" $settingsNode.value \"to\" $OctopusParameters[$key] \"in\" $configPath\r\n $variable.value = $OctopusParameters[$key]\r\n }\r\n \r\n }\r\n \r\n $configXml.Save($configPath)\r\n }\r\n}",
"Octopus.Action.Script.ScriptFileName": null,
"Octopus.Action.Package.FeedId": null,
"Octopus.Action.Package.PackageId": null
},
"Parameters": [
{
"Id": "b2d872c4-d3f6-4d27-9940-b153820b4b15",
"Name": "ActivateReplacing",
"Label": "Enable the replacing of Sitecore Settings with Octopus Vars",
"HelpText": "This Script searchs through all *.config files and replaces sc.variables and Sitecore settings with the value in Octopus of the same key.",
"DefaultValue": "True",
"DisplaySettings": {
"Octopus.ControlType": "Checkbox"
}
},
{
"Id": "875443ff-3923-4d4b-ab3b-cb2082eab363",
"Name": "IncludeStatement",
"Label": "Include Statement",
"HelpText": "Specify Include-Statement like `*.config` for filtering which files should be transformed.\n\nMulti-Include-Statements are possible like `Sitecore.*.config, Custom.*.config,*.example`\n\nSee [MSDN](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/get-childitem) for more.",
"DefaultValue": "*.config",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "125ec394-6ed7-4e04-9367-eb3fd71f3f2a",
"Name": "ExcludeStatement",
"Label": "Exclude Statement",
"HelpText": "Specify Exclude-Statement like `Debug.*.config` for filtering which files should **not** be transformed.\n\nMulti-Exclude-Statements are possible like `Sitecore.*.config, *.example`\n\nSee [MSDN](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/get-childitem) for more.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
}
],
"$Meta": {
"ExportedAt": "2016-12-07T19:29:07.286Z",
"OctopusVersion": "3.4.12",
"Type": "ActionTemplate"
}
}
# Script based on idea of Deren Hunziker, https://github.com/dthunziker
echo ("Replace Octopus Variables in Sitecore Configs to environment specific values...")
if ($OctopusParameters['ActivateReplacing'] -eq "True") {
Set-Location -path $OctopusParameters['Octopus.Action[Deploy NuGet Package].Output.Package.InstallationDirectoryPath']
Set-Location .\App_Config
$include = $OctopusParameters['IncludeStatement']
$includeArray = @()
if($include -ne $null) {
$includeArray = $include.Replace(" ", "").Split(',')
}
$exclude = $OctopusParameters['ExcludeStatement']
$excludeArray = @()
if($exclude -ne $null) {
$excludeArray = $exclude.Replace(" ", "").Split(',')
}
Get-ChildItem -Recurse -Include $includeArray -Exclude $excludeArray |
Foreach-Object {
$configPath = $_.FullName
$content = Get-Content $configPath
Write-Host "Scan Sitecore config file:" $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) {
if ($setting.value -eq $null -and $setting.HasChildNodes -and ($setting.FirstChild.name -ne $null) -and ($setting.FirstChild.name -eq "value")) {
# Replace a patch:setting
Write-Host "Transformation: " $setting.name "patch:setting will be updated from" $setting.FirstChild.InnerText "to" $OctopusParameters[$key] "in" $configPath
$setting.FirstChild.InnerText = $OctopusParameters[$key]
}
else {
# Replace a normal setting
Write-Host "Transformation: " $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 "Transformation: " $variable.name "Sitecore variable will be updated from" $settingsNode.value "to" $OctopusParameters[$key] "in" $configPath
$variable.value = $OctopusParameters[$key]
}
}
$configXml.Save($configPath)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment