Variable using C# scripting

I’m trying to set the value of a variable to a C# script, which looks possible with the variable editor, but every time I return to the variable editor after setting the value, the type is set back to “Plain text”. This is the value I am setting it to after selecting C#:

#{Octopus.Release.Number}.Split(’-’).Last();

I’ve also tried:

Octopus.Parameters[“Octopus.Release.Number”].Split(’-’).Last();

Am I doing something wrong?

Hi Christopher,

Thanks for getting in touch! The variable editor’s language selection is simply a convenience for when editing. The selected value isn’t persisted, so it won’t affect how it works in your scripts. Regarding the variable value you pasted above, I think all you’d need to do is enclose it in quotes for it to work in your C# script. i.e. "#{Octopus.Release.Number}.Split(’-’).Last();"

That said, I have noticed a bug when a specific language is selected in the editor and any change to the value field resets the type to Plain text. I’ve raised this issue at the following link.

I hope this helps! Let me know how you go or if you have any questions or concerns going forward. :slight_smile:

Best regards,

Kenny

Thanks, Kenny. I tried as you suggested with the format using quotes and it is still not working. I’m using the variable in a deploy package step, with the Configuration Variables feature.

Hi Christopher,

Thanks for keeping in touch! Sorry to hear that didn’t get this C# script up and running. I’d like to give this a test with your specific C# code snippet. Would you be willing to send that through, and the error message you’re hitting when trying to run it?

I look forward to hearing back!

Best regards,

Kenny

This is the exact snippet "#{Octopus.Release.Number}.Split(’-’).Last();"

The value of the variable ends up as: "1.2-234.Split('-').Last();" So, the variable substitution happens, but the script portion does not evaluate. I also tried an alternative:

new String("#{Octopus.Release.Number}").Split(’-’).Last();

which became:

new String("1.2-234").Split('-').Last();

Is scripting not supported in variables that are substituted into files?

Hi Christopher,

Thanks for following up and elaborating on this one. You’re correct, and I think the way to approach this would be to execute this logic in a C# script on a step during the deployment, store the result in an output variable (via Octopus.SetVariable("TestResult", "Passed"); where TestResult is the output variable’s name, and Passed is the value) and then use that to substitute into the file (via #{Octopus.Action[StepA].Output.TestResult} where StepA is the name of the step that created the output variable).

Let me know if that helps get this working as needed, or if you have any further questions going forward. :slight_smile:

Best regards,

Kenny