Detecting first deployment to an environment


Is there a way to detect when Octopus is pushing a project up to an environment for the first time? I’d like a step to occur only on the first push up to say our QA environment, but I’m unsure how to detect that via a variable of some kind to ensure that the step only occurs then.

Can you advise on the best way (and if it’s possible) to do this?


Hi Simon,

Thanks for reaching out to us with this question. If I may ask, what are you looking to do on first deployment?

If you only care about the whole Environment scope then the Octopus.Release.CurrentForEnvironment.Number built in variable could work as it will be empty on first deployment. Something like this script below would setup an output variable that you can bind to in subsequent steps.

$number = $Octopusparameters['Octopus.Release.CurrentForEnvironment.Number'];
echo "CurrentForEnvironment.Number: $number";
$isFirstDeployment = $false;
if($number -eq "" -or $number -eq $null) {
  $isFirstDeployment = $true;

echo "isFirstDeployment: $isFirstDeployment";
Set-OctopusVariable -name 'IsFirstDeployment' -value $isFirstDeployment

However, this is value is for the environment and not the individual machines in this environment. So if you add a machine into this environment later, it will show the same result as the existing machines. So for something like machine provisioning this will not work.

If you wanted to do something similar at the machine level you could check for some indicators like a file or a registry key and follow the same approach. You could also setup a separate project for environment provisioning instead.

Another option that comes to mind here is if you are up for some manual handling, then the Script Console under Tasks can allow you to run ad-hoc scripts against machines on an as needed basis.

Hope this help!

Kind regards,


Thanks Jim, that’s exactly what I’m after! Much appreciated :slight_smile:

No problem Simon!

Happy Deployments :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.