Monday, May 20, 2013

Sitecore Continuos Deployment: From Zero to Hero (Part 2: Deploy)

This is a second blog post in a Build-Deploy-Test series. Here you'll see how to deploy the package created at the previous step, with some hints specific to CI usage.

This blog post is a part of the series:

Deployment script is based on the sample from PowerCore. However, the following changes were added:

1. Use relative path as a deployment package location

$buildFolder = Resolve-Path ..
$sourcePath = "$buildFolder\Output\LaunchSitecore.Build.12345.zip"
view raw gistfile1.ps1 hosted with ❤ by GitHub

2. Cleanup databases before deploying new website, otherwise - database files might be locked

# Cleanup Databases from previuous installation (if needed)
foreach ($db in $databases)
{
Delete-Database $server "$siteName.$db"
}
view raw gistfile1.ps1 hosted with ❤ by GitHub

Here's the complete script: 

Clear-Host
# Framework initialization
$scriptRoot = Split-Path (Resolve-Path $myInvocation.MyCommand.Path)
$env:PSModulePath = $env:PSModulePath + ";$scriptRoot\Tools\PowerCore\Framework"
Import-Module WebUtils
Import-Module ConfigUtils
Import-Module DBUtils
Import-Module IISUtils
Import-Module FileUtils
# Main variables
$buildFolder = Resolve-Path ..
$siteName = "LaunchSitecore"
$licensePath = "C:\license.xml"
$sourcePath = "$buildFolder\Output\LaunchSitecore.Build.12345.zip"
$targetFolder = "E:\inetpub\wwwroot\LaunchSitecore"
$sqlServerName = "$env:COMPUTERNAME\SQLEXPRESS"
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $sqlServerName
$databases = "core", "master", "web"
# Cleanup Databases from previuous installation (if needed)
foreach ($db in $databases)
{
Delete-Database $server "$siteName.$db"
}
New-Item $targetFolder -type directory -Force -Verbose
# Additional variables
$packageFileName = [System.IO.Path]::GetFileNameWithoutExtension($sourcePath)
$dataFolder = "$targetFolder\Data"
$websiteFolder = "$targetFolder\Website"
# Main Script
Unzip-Archive $sourcePath $targetFolder
# Attach Databases
foreach ($db in $databases)
{
Attach-Database $server "$siteName.$db" "$targetFolder\Databases\Sitecore.$db.mdf" "$targetFolder\Databases\Sitecore.$db.ldf"
Set-ConnectionString "$websiteFolder\App_Config\ConnectionStrings.config" "$db" "Trusted_Connection=Yes;Data Source=$sqlServerName;Database=$siteName.$db"
}
Set-ConfigAttribute "$websiteFolder\web.config" "sitecore/sc.variable[@name='dataFolder']" "value" $dataFolder
Copy-Item $licensePath $dataFolder
Create-AppPool $siteName "v4.0"
Create-Site $siteName "$siteName.local" "$targetFolder"
Add-HostFileContent "127.0.0.1" "$siteName.local"
Get-WebPage "$siteName.local/InstallPackages.aspx"
Get-WebPage "$siteName.local/Publish.aspx"
view raw gistfile1.ps1 hosted with ❤ by GitHub

What's interesting, you can now run both build and deployment scripts from your favorite CI server.
My preferred one is TeamCity. It is free for the small teams, and it has such a great amount of useful features. In the area of build artifacts storage and dependencies management TFS is nowhere near. 

I'll add two build configurations: "Build" and "Deploy". Each of them will have just one build step - run PowerShell script. "Deploy" will also depend on "Build" and use its last successful output.

Build:

Deploy:

And here is what we get at the end:


Yes, with TeamCity you can browse contents of zip files online - one of those small usability improvements.
Now how to make "Deploy" configuration use build output from "Build"?

Just add a dependency. Here's how simple it is to configure it:


That's basically all you need to get up and running with Continuous Deployment for Sitecore. All source code and scripts used in this blog post can be found here:

If you still have questions - just leave a comment, or contact me in Twitter / LinkedIn and even %my github username%@gmail.com.




No comments:

Post a Comment