Dynamically include Variable Set via script


Is it possible to dynamically include a Variable Set via script/step/script module code ? (preferably based on a variable name)
For example if we have ProjectA with variable "IncludeVariableSet " set to “MainVS” , The script will include/inject a pre-existing variable set called “MainVS” to the deployment steps.

It seems like output variables are related but i couldn’t find the C# syntax to perform such operation.


Hi there @Jack

Thanks for your question about dynamically adding Variable Sets.

Unfortunately, we don’t have a feature that does this. Would you be able to share a little bit more about how you have configured your projects and environments, and what your overall goal is?

One approach could be to explictly scope the variables in the Variable set to Environments that align with the projects you are working against, ensuring you could re-use these variables, but only in the desired environments.

Another approach is a little bit more like your idea: use the extended/complex variable substitution syntax and use that project variable IncludeVariableSet in your complex substitutions.

Finally, output variables may work for you too. You could also add a Run a Script step to the beginning of your projects that would set some common variables up, and turn that into a step template, and then include that in other projects as needed.

In C# your script would need to contain lines like this:

Octopus.SetVariable("PrimaryDnsIpAddress", "");

Assuming this was done in a step named StepA you could then use these in variable subsitutions in later steps:


I hope that helps you out, let me know if there is more I can help with!

All the best,

Hi @Jim.Burger ,

Thank you for the detailed reply.

I was trying to accomplish this with output variables, Similar to your suggestion but i wanted to loop over a pre-defined variable set instead of manually setting the variable in the script itself.

So if i have a variable set called MainVS , the pseudo code would be:

var variablesList = Octopus.VariableSet("MainVS");

foreach (var v in variablesList)

This will allow to create this variables in the set using the UI interface and scope them per environment.

  • Can this be done ?
  • Is there a documentation for the C# commands used in scripts ?

Thanks @Jack

Strictly speaking, it is possible but I’m not sure if it 100% achieves what you want. Could you share why you need to do this? There might be a better construct that we haven’t considered yet.

If you setup the ‘template’ variables in each variable set with a naming convention (e.g. VarSetA, VarSetB) - and then include both variable sets in the project, then it is possible to loop over them. At the time the script step runs, these values have been resolved (scoped) and you can use Linq to pick out the ones you want.

At this point you can loop over the Octopus.Parameters dictionary and do what you want with them.

Something like this might be a starting point?

var desiredVariableSetPrefix = Octopus.Parameters["VariableSetPrefix"]; //VarSetA
foreach(var p in Octopus.Parameters.Where(p => p.Key.StartsWith(desiredVariableSetPrefix))) {
	//setup your output variables

To address your second question, I don’t think we have a dedicated page for C# documentation, but we do have many C# examples in our documentation, for instance, https://octopus.com/docs/deployment-examples/custom-scripts#Customscripts-Variables

Often you’ll see blocks like this one, where you can select your language of choice

Another useful reference is our System variables that may be worth giving a read,

Hope this helps!

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