Search code examples
c#tfsbuildtfs-2015

TFS Build 2015 - How to copy a file's content to another one during the build?


I'm working in a project that has these two files

Web.env1.config
Web.env2.config

Those files represent the two different enviroments on which my application is published.

We're using TFS Build 2015 to publish our applications, and whenever I have to publish the application, I have to manually copy the content of the one of those files and paste it into Web.config file.

So, I wanted to know if it's possible to do this process automatically without having to do it manually.


Solution

  • Here's what I do.

    If all of your settings are located in config sections supported by it, you could create transforms and apply them during a web deploy task in the release pipeline with this setting:

    enter image description here

    If I have environment-specific settings in sections not support by transforms, I maintain a Web.config.template file side-by-side with web.config. The environment-specific values are tokens that will be replaced in the Release Pipeline. For instance, instead of a connection string value it might be "~connstr~". Web.config is just used during debugging.

    In the release pipeline I have a replace tokens task that pulls pipeline variables and replaces the ~tokens~ with values. The environment-specific values are scoped pipeline variables:

    enter image description here

    After the web.config.template file has had it's tokens replaced with environment-specific values, I have a command line task that renames it to web.config. From there the pipeline deploys the files.

    Another option is to create a Secure File for each environment and bring it into the build or release pipeline.

    Pipelines are very powerful and you can get creative with solutions.