Continuous Integration: Move Unicorn Files Outside of Website

In the previous blog post about Unicorn and Continuous Integration, Unicorn Sync: YML File Used by Another Process, I have mentioned some issues about the Unicorn Sync step and a temporary solution that helped us a lot. However, we had to improve the process because, even though we are talking about a DEV server, it is still better not to kill the website in IIS during the deploy.

The solution is to change the Deployment step in Octopus by replacing the Pre-deployment PowerShell script with a Deployment PowerShell script which moves the Unicorn files to a different folder, outside of the website. By doing this during the deploy, the YML files do not get to be used by the w3wp process, so it is possible to move them to a different location. Before we move the files, we ensure that the folder where we move them is empty, thus avoiding old files still being used. Below is the script we use:

$folder = "<Path>\Website\Unicorn"
$newfolder = "<Path>\Data\Unicorn"

if (Test-Path $newfolder) {
    Get-ChildItem -Path $newfolder -Force -Recurse |
    Sort-Object -Property FullName -Descending |
    Remove-Item -Recurse -Force

if (Test-Path $folder) {
    Get-ChildItem -Path $folder -Recurse | 
    Move-Item -Destination $newfolder -Force

Then, the only thing we need to change is the Unicorn.config file so that the targetDataStore points to the new location.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s