Dynamically creating variables to be used in JSON Configuration Variables feature

jonas.cannehag's Avatar

jonas.cannehag

10 Jan, 2018 10:42 AM

Hi, we are using Octopus for many of our deployments. We are going to centralize our configuration in a custom build service.
What I would like to do is to have a PS-script getting the configuration as a JSON block and then in some way have those values inserted using the "JSON Configuration Variables" feature.

What is causing issues is the PS-script. I can get the values without a problem, but the JSON feature seems to only respect Octopus "predefined" variables, which I can't modify... The only thing I can do in my PS-script is to use the Set-OctopusVariable cmdlet, which in turn just creates the output variable on that step.
Is there any way to add variables in a script that is respected and used in the JSON config feature ?

  1. Support Staff 1 Posted by Kenneth Bates on 11 Jan, 2018 01:29 AM

    Kenneth Bates's Avatar

    Hi Jonas,

    Thanks for getting in touch! Using the syntax #{Variable} will only match against project variables (or in a library). To substitute output variables, you would need to target the step that creates the variable using the syntax #{Octopus.Action[StepName].Output.VariableName}. (This is assuming the step that creates this variable runs prior to the step that substitutes the value).

    JSON substitution targets JSON objects by using a special syntax, like foo:bar, which is outlined in the following doc page.
    https://octopus.com/docs/deploying-applications/deploying-asp.net-c...

    I was able to get this working by creating an output variable in a script step, targeting the file in the json substitution feature, and create a project variable with name foo:bar, where the value of this project variable uses the output variable syntax. I've included a few screenshots showing my set up.

    I hope this helps! Let me know how you go, and if you have any further questions, don't hesitate to reach out. :)

    Best regards,

    Kenny

  2. 2 Posted by jonas.cannehag on 11 Jan, 2018 06:00 AM

    jonas.cannehag's Avatar

    Hi and thank you for responding.
    I think you are missing the word dynamically in the title. What you are suggesting is to hardcode every configuration value which is what we do today.

    What we are trying to accomplish is to have the config elsewhere but make use of the JSON config feature.

    As an example, I can retrieve a JSON object from our external config service looking like this:
    {
       "foo": "bar",
       "also:working:with:depth": "baz"
    }

    I want make use of this as input to the JSON Config feature somehow. Do you have any other idea if that is possible without hardcoding the names, because that should be up to the config service, not Octopus

  3. Support Staff 3 Posted by Kenneth Bates on 15 Jan, 2018 04:43 AM

    Kenneth Bates's Avatar

    Hi Jonas,

    Thanks for following up! I used a basic example of how to store an output variable value which you can then use to substitute into your JSON. You could expand upon it to have a script that runs some custom code, and store the resulting value in an output variable in the same way. As a simple example of this, say you have a script step that runs:

    $variable = "#{Octopus.Environment.Name}"
    
    Set-OctopusVariable -name "NewVar" -value "$variable"
    

    Then when calling it in the subsequent step with:

    write-host "#{Octopus.Action[output script].Output.NewVar}"
    

    It writes out the relevant environment I'm deploying to.

    We have some additional information on what can be done with output variables in the following blog post.
    https://octopus.com/blog/fun-with-output-variables

    Does this help? Don't hesitate to reach out if you have any further questions. :)

    Best regards,

    Kenny

  4. 4 Posted by jonas.cannehag on 15 Jan, 2018 06:13 AM

    jonas.cannehag's Avatar

    Hi, thank you again! I think I need to explain a bit more :)
    The problem is that I don't now anything about the variables, not the name, not the value and not how many they are. I will just get a list of keyvalue pairs from our configuration service and would like to append those as "normal" variables so they will be used by the feature.
    A script to generate output variables is not a problem. The problem is to get them to be used by the JSON configuration feature...

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac