Using output variables for "Substitute Variables in Files" feature

I have a json config file in my WebApp and I want to update some config parameters in this file using output variables акь previous steps.

  1. So i have an Azure Powershell step that invokes required value and sets an output variable:


    Set-OctopusVariable -name “ServerURL” -value $connString.ConnectionString

  2. Also I have consequent step with enabled feature “Substitute variables in files” and I want to update some parameter in config.json with value of #{ServerURL}

So my question is : how should I use this output variable in my Json? Should it be something like this:
{
“ServerAddress”: " #{ServerURL}"
}

or I need to introduce some intermediate variable and use it in json. e.g.:
$serverUrl = OctopusParameters[“Octopus.Action[GetVariablesFromAppSettings].Output.ServerURL”]

and then in JSON:
{
“ServerAddress”: " #{serverURL}"
}

Hi!

Thanks for getting in touch. You should just be able to put the #{VariablePath} straight into your JSON file and target it with the Substitute variables in files feature.

In this case your file should look something like:

{
 "ServerAddress": "#{Octopus.Action[GetVariablesFromAppSettings].Output.ServerURL}"
}

The only thing to note here is you may need to add a Machine scope to this. Take a good read through Having fun with Output Variables for more details.

There’s also a really good reference on the Variable Substitution syntax.

Check the Verbose log for more details to make sure you get the Output Variable path you expect.

Hope that helps!
Mike

Hi Michael, thanks for your reply.
It is still not clear for me which machine name should be used? I’m setting output variable in the “Azure powershell step” and I do not have any specific machine or role in this step. Then I use this variable in step “Deploy an Azure Web App” which is also not executed on any specific machine.

Hi!

Thanks for giving me that context. Azure PowerShell Steps run on the Octopus Server so you should be OK to leave out the Machine scope from your expression.

I’ve built a working example for you on our demo server: https://demo.octopusdeploy.com/app#/projects/output-variables

Here you can see I’ve got:

  1. An Azure PowerShell Step called Publish Output Variable that publishes an Output Variable ServerUrl
  2. Another PowerShell Step that simply logs the value of that Output Variable using both methods:
ServerUrl = $OctopusParameters["Octopus.Action[Publish Output Variable].Output.ServerUrl"]
Write-Host "ServerUrl: $ServerUrl"

Write-Host "ServerUrl by substitution: #{Octopus.Action[Publish Output Variable].Output.ServerUrl}"

I usually find this is the best way to test a particular Octopus feature, by creating a temporary project, playing with the specific feature where it has no consequences, and then delete the project when you’re done.

Hope that helps!
Mike

Michael, thanks for detailed explanation!!
I managed to make it working.

P.S. I’m again excited with Octopus!

Hi!

Thanks for keeping me in the loop. I’ve been on vacation, hence my lack of response earlier.

I’m glad to hear you got it working and that Octopus is making you happy again!

Happy Deployments!
Mike