octopus-deploy

Octopus variable within string with no space following it?


I am writing a powershell script and would like to do a Octopus variable substitution for the variable $MyVar. However, it needs to be in a string with no spaces such as:

$query = @"SELECT * FROM [$MyVarDB].[SomeSchema].[SomeTable]"@

The issue arises when octopus thinks the variable's name is $MyVarDB when the variable is really $MyVar, followed by the string "DB". Is there a way to escape or enclose $MyVar so that it can be directly followed by a string?


Solution

  • Octopus recognizes the #{my var} format in inline scripts. If MyVar='Hello' for:

    $query = @"SELECT * FROM [$#{MyVar}DB].[SomeSchema].[SomeTable]"@
    

    Octopus will parse the script prior to running it and substitute value for the #{MyVar} token so the value becomes $HelloDB.

    Note: This only works for scripts using the inline option. If your Octopus script pulls a script from another source (i.e. the deployed package, GitHub) at runtime, it won't have a chance to parse the script prior to running it and substitution won't occur.