How to replace a string in web.config file?

I am looking to replace a specific string in web.config file with Octo deploy.

The web.config (partial) file looks like

I need to replace ‘localhost’ string with certain string value varying with environments. Note that ‘localhost’ is NOT the host name/ip of the tentacle system.
I know you can write PS script with XML APIs but that sounds too complicated to me for such a common task, I assume there are better way than that.
What’s the right (or best) way to do it?

Thanks

It seems there is a problem to past .xml file content to the post, here is another trial -

Hi Peter,

I can see your XML code fine when i view it as plain text. Our system uses markdown so another way to post it is to indicate a span of code, wrap it with 3 backtick quotes.
which will look like the following:

<configuration>
  <system.serviceModel>
    <client>
      <endpoint address="net.tcp://localhost:8004/string1" binding="string2" bindingConfiguration="string3" contract="string4" name="string5" />
    </client>
  </system.serviceModel>
</configuration>

Now my markup lesson is done :slight_smile: to your actual question. I am going to point you at a blog post about deploying PHP with Octopus. While any good skeptic would think Im a bit mad to do that, it shows how you can take any file including your configuration files, put an Octopus Variable in them, and have Octopus evaluate it for you. The “substitute variables in files” part. It shows this in steps, just ignore the other stuff about PHP and other silly things.

Hope this helps!
Vanessa

Thanks Vanessa.

I understand and know that way works. However that will require dev side to embed the variable (e.g. #{localhost}) in the initial config file, which is doable but not that preferred by developers.

The way I am looking for is: taking a config file from dev, I can search for the unique string and replace it with specific value (depending on environments) during deployment, best by using Octopus’ in box feature.

Based on the existing Octopus “Variables” design, a 4 column will meet my needs -

  1. File Location: could be destination absolute path (e.g. C:\dest\web\web.config) or relative path in .Nuget package (e.g. ./web/web.config’)
  2. Name: searching path for the UNIQUE string (or variable if you want call it), such as “<System.serviceModel><endpoint address=”
  3. Value: either to replace the whole string (e.g. “net.tcp://thesystemname.com:8004/string1”), or making step 2) to specify ‘localhost’ and set the value to “thesystemname.com”.
  4. Scope: no change

This will give the flexibility that deployment can handle the replacement without Dev’s change (in our case), of course, making the string/variable UNIQUE is the required.

The other day I read an Octopus document about web.config/app.confg (or such) variable replacement, it actually is close to my thought above, but it search name/value pair in the files, not any unique string/variable in the files.

So in short, my impression is that we have to set the variable(s) with certain format (#{xxx}) in the original (config) file first, then Octopus can search and replace it during deployment and can’t do wild search (for ‘localhost’) and replacement. Am I right?

Regards,
Peter

Correction/update to my last post -

I re-visited the document (http://docs.octopusdeploy.com/display/OD/Configuration+files), it is replacing the whole value of the string, and the only thing not working for us is that you have to have a name/value pair inside the line (or xml brackets <>), in my case below it won’t work (how do you know which ones of the 5 items are name/value), right?

Hi Peter,

So there is this library step template https://library.octopusdeploy.com/#!/step-template/actiontemplate-file-system-regular-expression-find-and-replace
That should provide you the functionality you are after.

Thanks!
Vanessa