Search code examples
octopus-deploy

Variable Substitution in OctopusDeploy


I'm trying to substitute a #{port} variable during by deployment using OctopusDeploy. I am able to do it if I directly add it in my App.Config as follows:

<services>
  <service name="SampleService" behaviorConfiguration="ServiceBehaviour">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:#{port}/SampleService/" />
      </baseAddresses>
    </host>
    ..
  </service>

Is there a way I can add a default port here (say 25555, so that developers can use it without replacing #{port} always), but still specify to OctopusDeploy that this particular port has to be modified? (I'm using multi-tenant deployments)


Solution

  • You can use the XML transform feature of config files in conjunction with Octopus deploy.

    Octopus itself can run these transforms. When you compile your code, the app.config is renamed [applicationName].exe.config If you create a file called [applicationName].exe.release.config and add this to your package then you can use the same style of transform, that you would normally use in web.config transforms, in that file. If Octopus sees a file called *.release.config it will run any transforms during the deployment process.

    Alternatively This blog contains instructions of extending your project so you can use the transform ability of web.config files in app.config files.

    So you could do this several ways. Have the hard coded value in you "base" app.config then have the Octopus Variable as a transform in the app.release.config. When you build and package your code, so long as you use the "release" configuration the transform will fire and the hard coded value will be replaced by your build and added to the package.

    Or you could do it the other way round, have the hard coded value in the app.debug.config and the Octopus Variable in the app.config. When a developer runs a debug build the Variable would be replaced by the value in the debug.config.