Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy different version of file for different build configurations?

In particular, I would like to deploy a different robots.txt file for different build configurations. We have a staging environment that is publicly available on the web, but we disallow everything in the robots.txt file for that environment so it doesn't get indexed and compete with our production site.

Currently, we just manually copy the previous version of the robots.txt file in production into the new folder each time we deploy. Is there a way to put both versions into the project and deploy a particular one based on the build configuration? Or is there a more 'correct' way to handle this?

like image 794
BVernon Avatar asked Jun 07 '15 23:06

BVernon


3 Answers

I use a pre-build event to do this. There is a really good article on it here: http://www.aaroncoleman.net/post/2011/10/28/Simple-ASPNET-robotstxt-transform-like-XDT.aspx

In summary use the following script in the pre-build event filed in your project's properties.

copy $(ProjectDir)Content\robots_txt\robots.$(ConfigurationName).txt $(ProjectDir)robots.txt /Y

Then you can store different file versions named according to the build config, and the script will copy the contents to the new file.

like image 129
MikeD Avatar answered Oct 03 '22 00:10

MikeD


I had the same problem and I found this article on www.asp.net: http://www.asp.net/mvc/overview/deployment/visual-studio-web-deployment/deploying-extra-files

As your situation is similar to mine, I had to adjust the technique in the article by creating separate subfolders under the ExtraFiles folder for each build profile. And then update each of your .pubxml files to refer to each build profile in that _CustomFiles node:

<_CustomFiles Include="..\ExtraFiles\BuildProfile1\**\*" />
like image 42
AmoebaMan17 Avatar answered Oct 03 '22 01:10

AmoebaMan17


For those of us, who are used to XML web.config transformations, the solution for simple files, like robots.txt, might be as easy as to put all the relevant info into a web.config app setting and on application startup, just generate the exact file(s) you need.

For example, in our web.config, we might have lines like these:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
      <add key="robots.txt" value="
User-agent: *
Disallow: /
" />
    </appSettings>
</configuration>

In our transformation XML files (web.stage.config, web.prod.config), we would, of course transform the value of that setting. And at last, in our code, we could just generate the file with transformed content:

static void Main(string[] args)
{
    var fileName = "robots.txt";

    var fileContent = ConfigurationManager.AppSettings[fileName];
    File.WriteAllText(fileName, fileContent);
}
like image 39
Mladen B. Avatar answered Oct 03 '22 00:10

Mladen B.