Options

Get rid of web.*.config files after deployment

rborgesrborges Posts: 4
In addition to getting rid of web.*.config files we also keep our actual appSettings and connectionStrings values in a Config directory

Environments:
  • Dev
  • QA
  • QC
  • We do not currently have a Prod environment due to firewall requirements, but do maintain Prod configs
Config files example
  • Confg\Dev\appSettings.config
  • Config\QA\connectionStrings.config
We only want to keep the config files for the current environment, delete the rest, and delete the web.*.config files

Special requirement: when deploying to QC, we want to keep our Prod configs to help makes our production deployments a little easier.

<#
	This is the post deployment script that will run after
	Red Gate Deployment Manager completes a deployment 
	to one of the servers

	Expected Red Gate Variables:
		$RedGateEnvironmentName - should be either null, Dev, QA, QC

	This script should be placed at the root of every web site or web application that 
	will be deployed using Deployment Manager

	This script will remove the web.*.config files and
	will delete the Configuration/* directories that don't apply to the current environment

	Example config files, if the current environment is QA:
		Web.config -> this file gets transformed
		Web.Debug.config -> will be deleted
		Web.Dev.config -> will be deleted
		Web.QA.config -> Deployment Manager will run this transform after the release config, this file will be deleted
		Web.QC.config -> will be deleted
		Web.Prod.config -> will be deleted
		Web.Release.config -> Deployment Manager always runs this file's transformations first

	Example config directories, if the current environment is QA:
		Configurations/Debug -> will be deleted
		Configurations/Dev -> will be deleted
		Configurations/QA -> will NOT be deleted
		Configurations/QC -> will be deleted
		Configurations/Prod -> will be deleted. If the current environment is QC, then this will not be deleted in order to aid with the deployment process
#>

# log file
$stream = [System.IO.StreamWriter] "PostDeployLog.log"

# name of, coming from Deployment Manager 
$environmentName = $RedGateEnvironmentName

# path that the script is running in
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

if(!$environmentName)
{
	$stream.WriteLine("Environment not found. Will do nothing")
}
else
{
	$stream.WriteLine("Environment is " + $environmentName)
	
	# delete config transform files
	$webConfigs = Get-ChildItem web.*.config
	foreach($file in $webConfigs)
	{
		$stream.WriteLine("Removing file: " + $file)
		Remove-Item $file.Name
		$stream.WriteLine("File removed: " + $file)
		$stream.WriteLine("")
	}
	# end delete config transform files

	# Delete config directories
	[System.IO.DirectoryInfo]$directInf = New-Object IO.DirectoryInfo($scriptPath + "\Configurations")
	$folders = $directInf.GetDirectories()
	
	$stream.WriteLine("")
	$stream.WriteLine("Deleting unnecessary folders in the Configurations directory")	

	foreach($directory in $folders)
	{
		$stream.WriteLine("Current directory is: " + $directory.Name)

		<#
			This is a mild attempt to reduce cyclomatic complexity
			if the current environment is QC and current directory is QC
				dont delete
			if the current environment is QC and the current directory is Prod
				dont delete
			if the current environment is QC and is any other directory
				delete
			if the current environment is not QC and the folder is the same as the environment
				dont delete
			if the current environment is not QC and the folder is not the same as the environment
				delete
		#>

		$isQcEnvironment = [string]::Compare($environmentName, "QC", $True)
		$currentDirectoryIsQc = [string]::Compare($directory.Name, "QC", $True)
		$currentDirectoryIsProd = [string]::Compare($directory.Name, "Prod", $True)
		$isFolderSameAsEnvironment = [string]::Compare($directory.Name, $environmentName, $True)

		if ($isQcEnvironment -eq 0 -and $currentDirectoryIsQc -eq 0)
		{
			$stream.WriteLine("Don't delete QC folder")
		}
		elseif($isQcEnvironment -eq 0 -and $currentDirectoryIsProd -eq 0)
		{
			$stream.WriteLine("Don't delete Prod folder")
		}
		elseif($isQcEnvironment -eq 0)
		{
			$stream.WriteLine("Delete " + $directory.Name + " folder")
			$removalName = $directory.FullName
			$stream.WriteLine($removalName)
			Remove-Item $removalName  -recurse
		}
		elseif($isQcEnvironment -ne 0 -and $isFolderSameAsEnvironment -eq 0)
		{
			$stream.WriteLine("Don't delete " + $directory.Name + " folder")
		}
		elseif($isQcEnvironment -ne 0 -and $isFolderSameAsEnvironment -ne 0)
		{
			$stream.WriteLine("Delete " + $directory.Name + " folder")
			$removalName = $directory.FullName
			$stream.WriteLine($removalName)
			Remove-Item $removalName  -recurse
		}
		$stream.WriteLine("")
	}
	# end Delete config directories
}
	
$stream.close()


Sign In or Register to comment.