Variable substitution index replacement - not evaluated

Hey Guys,

I’m trying to acheive the variable substitution index as per the doco here - https://octopus.com/docs/reference/variable-substitution-syntax.

However, the variables aren’t evaluating, which I found happens if you have a typo or something in your variables in your files.

Basically, we have 2 types of variables - primary and secondary. And we have 2 environments, of 2 variables - production and preproduction. Let me explain:

Variables:

(name - value)

  • ApiKey[production]Primary - abc123
  • ApiKey[production]Secondary - 890uio
  • ApiKey[preproduction]Primary - jkl789
  • ApiKey[preproduction]Secondary - 567rtu
  • environment - production
  • environment - preproduction

So in theory, if we put the ‘environment’ variable inside of the ‘apikey’ variable, in the square brackets, that should then evaluate the correct primary and secondary variables for the respective environment

In our JSON template, here is the octo. variables I had configured

  • #{ApiKey[#{environment}]Primary}
  • #{ApiKey[#{environment}]Secondary}

However they were not evaluating.

Have I used that functionality correctly? Is this the correct way to concat variables? Cheers.

Hi,

Thanks for getting in touch! It looks like you have it set up correctly, except for one important thing. Index replacement doesn’t support variable substitution when the index is in the middle of the variable name. I’ve set up an example using your values and slightly different variable names (i.e. PrimaryApiKey[production] instead of ApiKey[production]Primary), which I was able to get working. I’ve attached the following 4 screenshots to outline my process.

  1. My variables, where each environment variable is scoped to its respective environment.
  2. My variables as located in the file that they will be replaced in.
  3. The substituted variables after deploying to my Preproduction environment.
  4. The substituted variables after deploying to my Production environment.

Let me know what you think and how you go. Don’t hesitate to reach out if you have any further questions as well. :slight_smile:

Best regards,

Kenny

03_preproduction_evaluated_variables.png

04_production_evaluated_variables.png

Beautiful! thanks for the clarification.

I’m a bit stuck today until about Tuesday with some ‘need it yesterday’ work, once that’s complete I will test what you did and report back with findings. Thanks again for that.

Hi,

That’s not a problem at all! I’d like to hear how you go, or if you have any other questions. :slight_smile:

Kind regards,

Kenny

Hey Kenneth,

That’s worked great in our JSON files, thanks for that…what about inside of a powershell script run from the octopus server? Looks like it just evaluates to null. I’ve tried a bunch of things:

  • $apikey = $OctopusParameters[“PrimaryApiKey[production]”] - null
  • $apikey = $OctopusParameters[‘PrimaryApiKey[production]’] - null
  • $apikey = $OctopusParamters["{PrimaryApiKey[production]}"] - null
  • $apikey = ${PrimaryApiKey[production]} - nul
  • set-variable -name “apikey” -value ${PrimaryApiKey[production]} - null
  • $apikey = $primaryapikeyproduction - null
    etc.

Cheers.

edit: oh, looks like I’ve inadvertently done some spooky formatting, if you view the source of this post (?) that’s the values I used, ha :).

Note: I don’t require the variables to be nested within []'s, I just used them as that’s what doco suggested for nested variables… I’m testing other methods now.

I tried with:

  • #{PrimaryApiKey.#{production}} - that worked fine in powershell, but doesn’t evaluate in JSON (in fact, that makes all variables not evaluate)

Hi Wez,

Thanks for following up! Your example using the syntax $apikey = $OctopusParameters["PrimaryApiKey[production]"] worked just fine in my setup. Could you provide a verbose deployment log? This will give us a detailed look to help us figure out what’s going on. The process of producing and exporting this log is outlined in our documentation.
https://octopus.com/docs/how-to/how-to-turn-on-variable-logging-and-export-the-task-log

I look forward to hearing back!

Best regards,

Kenny

Hey Kenneth,

Bloody ripper mate, worked perfectly.

Something to note: when ‘script code: source code’ is selected, it doesn’t evaluate there…but running that same code inside a script, on the octopus server, runs fine.

There’s actually a few ways this works:

$env = "preprod"
$keyname = "primary"
# option 1
$variablename = "EdgeApiKey$Keyname[$env]"
# option 2
$keyvalue = $OctopusParameters["EdgeApiKey$keyname[$env]"]
# static option for testing
$keyvalue1 = $OctopusParameters["EdgeApiKeySecondary[preprod]"]

# string evaluation option 3
$variablename = "EdgeApiKey$Keyname[$env]"
$variablename = $ExecutionContext.InvokeCommand.ExpandString($variablename)
$variablename = $OctopusParameters["$variablename"]

All 3 different options above worked fine.

Hi Wez,

That’s great to hear you got it up and running! I’ve given this a quick test in the same setup, and options 2 and 3 both worked fine for me when selecting source code. If you’re still having troubles with getting those to evaluate as source code, we’ll be happy to troubleshoot that further. Feel free to attach your verbose deployment logs with debugging variables enabled for a deployment that fails. You can find the process to producing and exporting this log in the following doc link.
https://octopus.com/docs/how-to/how-to-turn-on-variable-logging-and-export-the-task-log

Kind regards,

Kenny

Hey Ken,

All good mate, perhaps we might need to upgrade or something. Thanks heaps for that! Have a good one.

Wez