How to specify Octopus built-in variable names in

2020-07-13 09:45发布

问题:

We are testing Octopus Deploy, so we made a sample web.config file that we installed and transform in different environments. web.config transformation and use of custom variable work fine, but I can't get build-in Octopus variables to be substituted. Here's an example of of XML section in web.config:

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <add key="environmentTransform" value="" />
    <add key="EnvironmentVariable1" value="$OctopusEnvironmentName" />
    <add key="EnvironmentVariable2" value="$(OctopusEnvironmentName)" />
    <add key="EnvironmentVariable3" value="$(Octopus.Environment.Name)" />
    <add key="EnvironmentVariable4" value="$Octopus.Environment.Name)" />
    <add key="EnvironmentVariable5" value="$OctopusParameters[&quot;Octopus.Environment.Name&quot;]" />
    <add key="MachineVariable1" value="$Octopus.Machine.Name" />
    <add key="MachineVariable2" value="#(Octopus.Machine.Name)" />
    <add key="MachineVariable3" value="#OctopusMachineName" />
    <add key="CustomVariable" value="CustomVariable" />
  </appSettings>

And here's web.Dev.config, named after the environment:

  <appSettings>
    <add key="environmentTransform" value="Dev" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
  </appSettings>

And here's the result:

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <add key="environmentTransform" value="Dev" />
    <add key="EnvironmentVariable1" value="$OctopusEnvironmentName" />
    <add key="EnvironmentVariable2" value="$(OctopusEnvironmentName)" />
    <add key="EnvironmentVariable3" value="$(Octopus.Environment.Name)" />
    <add key="EnvironmentVariable4" value="$Octopus.Environment.Name)" />
    <add key="EnvironmentVariable5" value="$OctopusParameters[&quot;Octopus.Environment.Name&quot;]" />
    <add key="MachineVariable1" value="$Octopus.Machine.Name" />
    <add key="MachineVariable2" value="#(Octopus.Machine.Name)" />
    <add key="MachineVariable3" value="#OctopusMachineName" />
    <add key="CustomVariable" value="Value for maeaint01" />
  </appSettings>

As you can see, Octopus variables are not replaced. Any idea why?

回答1:

Octopus's config file variable substitution works only on the setting key, not the value. If you create a setting like:

<add key="Octopus.Environment.Name" value="..." />

then the value will be set at deployment time.

What you're attempting with the other approaches is reasonable of course, but not how Octopus currently works with config files.

We've enhanced our variable substitution processing in 2.0 with a new parser and new capabilities (#{if ..., #{each and so-on) - your question inspired me to draft up this proposal which might address this usage better.



回答2:

I found that my .config files would not pick up built in variables or custom variables.

What you could do is set variable in the octopus dashboard called EnvironmentVariable1 and set it's value to be

#{OctopusEnvironmentName}

This will then transform the EnvironmentVariable1 node in your config to be the environment name that you are currently deploying to.