How can I test for the existence of a variable in Octopus Deploy?

logic-in-deployments
(Mark Harrison) #1

I have a PowerShell script step in my Octopus Deploy deployment process. I want to be able to control which parts of the script run depending on whether or not a variable exists in the deployment.

Is this possible in Octopus?

(Mark Harrison) #2

There are a couple of ways this can be achieved, depending on whether or not the variable has a value that can be interpreted by Octopus as True or False.

Solution 1: Variable Substitution

Octopus has a feature known as Variable Substitution. (https://octopus.com/docs/projects/variables/variable-substitutions) which supports syntax of if, if-else, and unless keywords.

Let’s go ahead and use this feature and see how it works.

Using Variable Substitution, you could test for the value of a variable’s existence in your script step by using the following:

#{if ProjectVariable}
    Write-Highlight "ProjectVariable exists"
#{else}
    Write-Highlight "ProjectVariable does not exist"
#{/if}

If the variable ProjectVariable didn’t exist in the project as is the case for my example, then you would see the following output:

image

But what happens if we add a variable with the name ProjectVariable but assign it no value?

We add our variable:

Then, we create a new release and run the same script step as before:

image

Octopus is still telling us the variable doesn’t exist, but we created one, so what’s going on here?

Truthy and Falsy values

It turns out that Octopus is behaving as expected. When evaluating if, if-else or unless conditions, Octopus will consider values to be:

  • falsy if it is undefined, empty, False or 0 .
  • All other values are considered to be truthy .

In my example above, since we have provided no value, the variable ProjectVariable has been evaluated as false and that explains why we see the highlighted message:

ProjectVariable does not exist

If we want Octopus to evaluate the variable as true, we need to give the ProjectVariable a value which will evaluate as true:

Then, we create a new release and run the same script step as before:

image

Now the message that’s printed is that the variable exists.

Testing for Specific values

If you wanted to test for a specific value in the variable, you could alter the script to something like this:

#{if ProjectVariable == "ValueToTestFor"}
    Write-Highlight "ProjectVariable exists"
#{else}
    Write-Highlight "ProjectVariable does not exist"
#{/if}

For a full reference on Variable Substitution and how it treats True and False, see our documentation on https://octopus.com/docs/projects/variables/variable-substitutions#VariableSubstitutionSyntax-TruthyandFalsyvalues


Solution 2: using $OctopusParameters

Another solution is to use the built-in $OctopusParameters dictionary which contains all of the variable values defined for this deployment.

You could use this in your script like this:

$result = $OctopusParameters.ContainsKey("ProjectVariable")
if($result) 
{
	Write-Highlight "ProjectVariable exists"
}
else 
{
	Write-Highlight "ProjectVariable does not exist"
}

AppSettings with Octopus Variables

If you utilize an AppSettings file in your applications, and want to verify they have a matching Octopus variable present, it’s worth reading the article my colleague wrote on our blog on https://octopus.com/blog/verify-appsettings-json#ensuring-app-settings-have-octopus-parameters


Summary

It’s possible to test for the existence of a variable in your Script step, using either solution above. Which one you choose really depends on your preference. Variable Substitution is a powerful feature that can be used in more than just a Script step and is, therefore my preferred option.

(Mark Harrison) listed #3