Use Octopus variables in PowerShell cmdlets

Octopus Deploy is a powerful deploy tool, and we use it in our CI-chain alongside TeamCity and GitHub. Amongst things, Octopus enables you do run scripts before, during and after the deployment phases.

In our case we are using PowerShell scripts (or cmdlets if you prefer it that way) to start and stop the IIS processes on target machine before deploying (otherwise the resource is locked for writing because it is used by another process).

In that scenario we are taking advantage of the module WebAdministrationhttps://technet.microsoft.com/en-us/library/ee790599.aspx

Previously we've simply just invoked the Stop-WebSite 'sitename' and Start-WebSite 'sitename', regardless of which environment the tentacle was targeting, because every site was named in a coherent way. But as projects grow larger and more complex we found ourselves in a situation where we had multiple environments and multiple websites to start, so we came up with a simple way to "automate and simplify" this.

In Octopus we added a new variable called SiteNamesForDeploy – a simple string which is distributed with the tentacle in each deploy process, diversifying the values between environments. For example:

Variable name Value Environment
SiteNamesForDeploy TestSite Test
SiteNamesForDeploy StageSite Stage
SiteNamesForDeploy ProdSite, OtherSite.com Production

And then a small scripts that simply takes that variable, splits the value and invokes another cmdlet per value.

Simple as pie?
Enjoy.


Why the Exit 1?

In our case it's vital for our pre deploy scripts to actually stop the process we are trying to overwrite, if that fails, all other deployment steps is redundant.