Update 21 Aug 2015 - This post contains all the basic information, but there is an improved PowerShell script discussed in Using Release Management vNext templates when you don’t want to use DSC scripts – A better script
Many web sites are basically forms over data, so you need to deploy some DB schema and something like a MVC website. Even for this ’bread and butter’ work it is important to have an automated process to avoid human error. Hence the rise in use of release tools to run your DACPAC and MSDeploy packages.
In the Microsoft space this might lead to the question of how Desired State Configuration (DSC) can help? I, and others, have posted in the past about how DSC can be used to achieve this type of deployment, but this can be complex and you have to ask is DSC the best way to manage DACPAC and MSDeploy packages? Or is DSC better suited to only the configuration of your infrastructure/OS features?
You might ask why would you not want to use DSC, well the most common reason I see is that you need to provide deployment script to end clients who don’t use DSC, or you have just decided want basic PowerShell. Only you will be able to judge which is the best for your systems, but I thought it worth outlining an alternative way to do deployment of these package using Release Management vNext pipelines that does not make use of DSC.
Background
Let us assume we have a system with a SQL server and a IIS web server that have been added to the Release Management vNext environment. These already have SQL and IIS enabled, maybe you used DSC for that?
The vNext release template allows you to run either DSC or PowerShell on the machines, we will ignore DSC, so what can you do if you want to use simple PowerShell scripts?
Where do I put my Scripts?
We will place the PowerShell scripts (and maybe any tools they call) under source control such that they end up in the build drops location, thus making it easy for Release Management to find them, and allowing the scripts (and tools) to be versioned.
Deploying a DACPAC
The script I have been using to deploy DACPACs is as follows
\# find the script folder
$folder = Split-Path -parent $MyInvocation.MyCommand.Definition
Write-Verbose "Deploying DACPAC $SOURCEFILE using script in '$folder'"
& $foldersqlpackage.exe /Action:Publish /SourceFile:$folder..$SOURCEFILE /TargetServerName:$TARGETSERVERNAME /TargetDatabaseName:$TARGETDATABASENAME | Write-Verbose -Verbose
Note that:
- First it finds the folder it is running in, this is the easiest way to find other resource I need
- The only way any logging will end up in the Release Management logs is if is logged at the verbose level i.e. write-verbose “your message” –verbose
- I have used a simple & my.exe to execute my command, but pass the output via the write-verbose cmdlet to make sure we see the results. The alternative would be to use invoke-process
- SQLPACKAGE.EXE (and its associated DLLs) are located in the same SCRIPTS folder as the PowerShell script and are under source control. Of course you could make sure any tools you need are already installed on the target machine.
I pass the three parameters need for the strips as custom configuration
Remember that you don’t have to be the SQL server to run SQLPACKAGE.EXE, it can be run remotely (that is why in the screen shot above the ServerName is ISS IIS8 not SQL as you might expect)
Deploying a MSDeploy Package
The script I use to deploy the WebDeploy package this is as follows
function Update-ParametersFile
{
param
(
$paramFilePath,
$paramsToReplace
)
write-verbose "Updating parameters file '$paramFilePath'" -verbose
$content = get-content $paramFilePath
$paramsToReplace.GetEnumerator() | % {
Write-Verbose "Replacing value for key '$($\_.Key)'" -Verbose
$content = $content.Replace($\_.Key, $\_.Value)
}
set-content -Path $paramFilePath -Value $content
}
\# the script folder
$folder = Split-Path -parent $MyInvocation.MyCommand.Definition
write-verbose "Deploying Website '$package' using script in '$folder'" -verbose
Update-ParametersFile -paramFilePath "$folder..\_PublishedWebsites$($package)\_Package$package.SetParameters.xml" -paramsToReplace @{
"\_\_DataContext\_\_" = $datacontext
"\_\_SiteName\_\_" = $siteName
"\_\_Domain\_\_" = $Domain
"\_\_AdminGroups\_\_" = $AdminGroups
}
write-verbose "Calling '$package.deploy.cmd'" -verbose
& "$folder..\_PublishedWebsites$($package)\_Package$package.deploy.cmd" /Y | Write-Verbose -verbose
Note that:
- First I declare a function that I use to replace the contents of the package.setparameters.xml file, a key step in using binary promotion and WebDeploy
- Again I finds the folder the script is running in so I can locate other resources
- I then declare the parameters I need to replace and call the replacement function
- Finally I call the package.deploy.cmd command, and pass the output via the write-verbose to pass the output to the Release Management logs
This is called as follows
Summary
So I think these reusable scripts give a fairly easy way to make use of vNext Release Management pipelines. They can also easily be given to clients who just want to manually run something.