Improved clean.ps1 for Sitecore Container Development
If you're working with Sitecore containers, chances are you're also using the clean.ps1 script to, well, clean your persisted data layer and start fresh. Read on to learn how we improved it.
If you're working with Sitecore containers, chances are you're also using the clean.ps1 script from the Getting Started repo by Sitecore to remove data in the mounted directories for your containers. We've started with the clean.ps1, too and made some handy additions to it, while working with Sitecore 10 and Docker.
Why Do We Need a Clean-up Script?
When working with containers, most of the data you're working with is built into the containers and thus relatively short lived and can be rebuilt at any time. Throwing away and rebuilding the data layer of your Solution is neither short lived nor rebuilt quickly. Thus, all database files and Solr index files are persisted in your local filesystem and mounted into the containers.
Now, when switching branches, testing module installations, introducing new indexes, modifying existing data structures and probably many more use cases, you want a reliable and fast way to remove the persisted data and start from scratch. That is, when the docker-cleanup.ps1 script comes into play.
Setup and Usage
You can create your own version of the cleanup script and modify it to your liking by copying the code below. The script assumes a filesystem structure, where it is placed in the root of the repository and the persistent data mounts are places under a root folder called docker:
<git root>
|-- docker
|-- data
|-- cm // mounts to the log directory in the container
|-- cd // dito cm
|-- mssql // all database files
|-- solr // all solr indexes
|-- deploy // build output that is watched on the Sitecore instance
With this (or a similar) structure in place, the script takes an alias for the folder to clean up as -Target
argument:
.\docker-cleanup.ps1 -Target deploy
We're currently happy with options for data
to remove all databases and indexes (basically a "start from scratch" scenario), a more specific sql
and solr
target for either one of these and, most importantly, the deploy
alias to remove build output from the deploy folder.
So, here's the script:
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[ValidateSet('data','deploy','sql','solr','YOLO')]
[string[]]
$Target
)
function cleanup ($dir) {
Get-ChildItem -Path (Join-Path $PSScriptRoot $dir) -Directory | ForEach-Object {
$dataPath = $_.FullName
Get-ChildItem -Path $dataPath -Exclude ".gitkeep" -Recurse | Remove-Item -Force -Recurse
}
Write-Host "Cleanup of directory '$dir' done." -ForegroundColor Green
}
switch -Exact ($Target)
{
'data'
{
cleanup('\docker\data');
}
'deploy'
{
cleanup('\docker\deploy');
}
'sql'
{
cleanup('\docker\data\mssql');
}
'solr'
{
cleanup('\docker\data\solr');
}
'YOLO'
{
cleanup('\docker\data');
cleanup('\docker\deploy');
}
}
Bonus
If you would like to execute the script from within your Visual Studio, here's your snippet for the Command Task Runner (a very handy extension to VS):
"2 Clean Deploy Dir": {
"fileName": "powershell.exe",
"workingDirectory": ".",
"arguments": "/noprofile /c ..\\docker-cleanup.ps1 -Target deploy"
}
This allows you to run the clean-up task for the deploy directory with a simple right-click in the Command Task Runner window in Visual Studio. Why this is useful? A VS Solution build publishes the build output to the deploy directory with a mount into the Sitecore container. A file watcher inside the container, watches for changes in said directory and copies files into the container web root. If you remove or rename build output resources (like config files), they will not be removed from the deploy directory and thus create a bit of a mess inside the container.