How to do a version based Variable?

If I have a variable “MyVariable”

and before version 3.0.0 I want it set to “Bob”
and after version 3.0.0 I want it set to “Frank”

How do I set that up?

Hi @Keith_Nicholas,

Thanks for reaching out to Octopus Deploy.

If you haven’t already, please take a look at Conditionals with Variables. I believe this might be what you’re looking for and you should be able to build some logic into how the Variable picks its value.

If this is something you can utilize please let me know. If this isn’t what you’re after, then could you please provide a few more details on what you’re trying to accomplish so I can provide a better suggestion?

Regards,
Garrett

No, that seems like how to use variables as conditions.

I want the variables that will get substituted into the appsettings.json file to be conditional.

ideally in the following I’d want a section that had a conditional, that way I could define multiple variables and set the condition for when each one applies

Hi Keith,

Thanks for following up and elaborating on what you’re after. I’ll jump in here for Garrett as he’s currently offline as part of our US-based team.

You write the conditional directly into the variable value, which will be resolved during deployment. My simple example is variable named MyVariable with value #{if Octopus.Release.Number == "3.0.0"}bob#{else}frank#{/if}. A script step simply Write-host #{MyVariable} writes bob when the release version being deployed is v3.0.0, and frank for all other releases.

But the issue I hit trying to achieve this was the range of release versions to accommodate for. You might be able to do some trickery with the | substring filter to get the first digit in Octopus.Release.Number, and place the conditional and that filter into a variable’s value, which controls the value to print out based on the value of that one digit. However, I think probably the easiest, least-hacky solution would be to script the logic in a preceding PowerShell script step (since Octostache doesn’t allow “greater than” or “less than” logic), set an output variable and base the condition off that. For a solution to a very similar example, the following thread illustrates this.

I hope this helps, and please let me know how you go!

Best regards,

Kenny

Hi Keith,

Just jumping in again quickly, as I had a moment and wanted to confirm conclusively that this setup works as I was expecting (and hoping it does so for your specific needs). Here’s the process I followed, using the previously linked Stack Overflow thread as a template.

Created a PowerShell script step (named Check version) as the first step in the deployment process. This retrieves the release version via a system variable, and creates an output variable whose value is either Bob if the release version is greater than 3.0.0, or Frank if the release version is less than 3.0.0.

$IsOver300 = $OctopusParameters["Octopus.Release.Number"] -gt [version]"3.0.0"
Set-OctopusVariable -name "IsOver300" -value (&{If($IsOver300) {"Bob"} Else {"Frank"}})

I then created a project variable, named MyVariable, whose value is the syntax to call the output variable that was generated in the first step, e.g. #{Octopus.Action[Check version].Output.IsOver300}

I lastly created a second deployment process step that simply has Write-host #{MyVariable}. Depending on whether the release is pre or post-v3.0.0, this still will write Bob or Frank correctly.

I hope that helps!

Best regards,

Kenny

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