How can I require an approval when a version I am deploying is lower than the currently deployed version in the Environment?

I have a project in Octopus that has multiple channels:

  • Default Channel
  • Feature-Test

We use the default versioning (starting at 0.0.1, 0.0.2 and so on) that Octopus gives us when we cut a new release.

I want to be able to prevent a developer from accidentally deploying a lower version release to an environment where it already has a higher versioned release deployed.

Instead, I’d like a manual intervention step to run when this scenario occurs so the developer or tester can make an informed choice

This can be achieved in a similar method to another one of our KB articles - https://help.octopus.com/t/how-can-i-require-an-approval-when-a-major-or-minor-version-is-deployed-to-an-environment-the-first-time.

In it, it discusses having an approval when a major or minor version is deployed for the first time. That solution uses:

Octopus supports conditional syntax operators such as == and !=.

It doesn’t, however, support:

  • less than <
  • greater than >

Therefore we can’t add a run condition using that kind of syntax.

Instead, we can simply add a script step and a manual intervention step at the start of our deployment process which looks like this:

image

The Calculate Newer Release script step contains the following source:

$ReleaseNumberBeingDeployed = $OctopusParameters['Octopus.Release.Number']
$ReleaseNumberAlreadyDeployedInEnvironment = $OctopusParameters['Octopus.Release.CurrentForEnvironment.Number']

$idx = $ReleaseNumberBeingDeployed.IndexOf("-")

if($idx -ge 0) 
{
    $ReleaseNumberBeingDeployed = $ReleaseNumberBeingDeployed.Substring(0, $idx)
}

$currentReleaseVersion = New-Object System.Version($ReleaseNumberBeingDeployed)

$idx = $ReleaseNumberAlreadyDeployedInEnvironment.IndexOf("-")

if($idx -ge 0) 
{
    $ReleaseNumberAlreadyDeployedInEnvironment = $ReleaseNumberAlreadyDeployedInEnvironment.Substring(0, $idx)
}

$envReleaseVersion = New-Object System.Version($ReleaseNumberAlreadyDeployedInEnvironment)

$newerRelease = $false

if($currentReleaseVersion -lt $envReleaseVersion)
{
	$newerRelease = $true
}

Set-OctopusVariable -name "Result" -value $newerRelease

The script step above is written in PowerShell but can be converted to a language of your choice. It simply compares two versions with one another:

  • Octopus.Release.Number - this is the version of the project release version being deployed
  • Octopus.Release.CurrentForEnvironment.Number - this is the project release version of the project that is already deployed to the Environment.

By comparing these two Octopus system variables, we can make a decision as to whether the version about to be deployed is lower than the version currently deployed to that same environment

We then set the result of this comparison in an Output variable called Result.

Next, in our new Version number Check step which runs directly after the Script step above, we can add a run condition which checks the Output variable from the previous step:

.

Tip: You could simplify the run condition variable:

#{if Octopus.Action[Calculate Newer Release].Output.Result}true#{/if}

into a Project Variable if you wanted.

After this has been added, we can see this in action:


You can see from the above screenshot, Development has v0.0.14 deployed to it from the Default channel.

If we then go ahead and deploy v0.0.13 from the Alpha channel to Development, the Manual intervention step will run to ask for confirmation that this is expected: