Variable syntax using if/else

Hi Guys - Looking at the documentation for variables, it says:
Complex syntax
Additional conditional statements are supported in Octopus 3.5 and onwards, including == and !=.

Using complex syntax you can have expressions like #{if Octopus.Environment.Name == “Production”}…#{/if} and #{if Octopus.Environment.Name != “Production”}…#{/if}

We have used this syntax for a few of our variables, but we would like to be able to use this to set some of our default (ie unscoped) variable values basing them on other variable values, and would like to know if ‘else’ is supported in this complex syntax? For example:

#{if Octopus.Channel.Name == “Release Branch”}4#{else}5#{/if}

At the moment for simple ones we have to do this:

#{if Octopus.Channel.Name == “Release Branch”}4#{/if}#{if Octopus.Channel.Name != “Release Branch”}5#{/if}

We currently have a requirement to set this variable to one value for two of our channels (which already requires using nested ‘if’ branches), and another value for every other channel, so I’m trying to figure out the best way to do this. If you have any other suggestions I am open to them, but I think that adding ‘else’ to the ‘if’ syntax would be a great addition if it’s not already there.

What happens if you have multiple default (unscoped) entries, like this:
variablename = 4
variablename = #{if Octopus.Channel.Name == “Release Branch”}5#{/if}
variablename = #{if Octopus.Channel.Name == “Another Branch”}5#{/if}

How would these be evaluated at deployment time? Do they all get tested (if so in what order?), or would it randomly pick up one of them? From the testing I’ve done, it always seems to just get the value of ‘4’, regardless of the Octopus.Channel.Name value.

Cheers, Simon.

Hi Simon,

We don’t support else but you’re right, we should, I’ve created an issue to get it added which you can track here.

With the multiple variables question, we add them all into the variable dictionary and then pull out the one that matches your scope (if you have them scoped). If you have multiple values with the same scope we take the first one. In your specific example the right way to do it would be to scope the values to the channel rather than using #{if}.


Thanks for the response Mark, I’ll keep an eye on that issue. As for our current situation, I was hoping to scope it to the channel, but these variables are in a variable library, so are not able to be scoped by channel. I might have to make an exception in this case and pull them out of that library and put them into the project variables so I can scope it this way, that would certainly be the simplest solution.

Cheers, Simon.

Awesome - I see that ‘else’ has been added for the next release, that will allow us to simplify several of our conditional variables, and make them much easier to both construct and interpret. Thanks for the quick action!
Cheers, Simon.

Verified this is working in 2018.3.0, thanks guys!

1 Like

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