开发者

what tool to use to automate building & publishing content in VS2010?

I'm using vs 开发者_StackOverflow中文版2010 and have a web application project. I currently use the right-click "publish" option on my project using the "web deploy" option to publish the entire application to a server that is running msdeploy.axd.

I have to publish this project many times (100+), each time just changing the web.config.

I'm looking into what technology I should use to automate this process. I would prefer to use standard MS technology.

Should I be looking at MSBuild? The VS Command Prompt? (are these the same thing?)

What technology should I learn to best automate this scenario. I'm looking for the most standard way to do this...

In my head the script I'm going to write will:

  1. change the web.config file
  2. do the equivalent of right-click my project, click publish, use the web-deploy option, and deploy it to that server using the windows domain\username + password details i've saved in Visual Studio

Any help is appreciated.


I've done this exact thing just with MSBuild. The steps involved in my case were:

  1. TFS performed a build using a custom MSBuild project, not a solution file. Any CI build system would work, nothing special about TFS in this instance.
  2. In the custom build project, in addition to building all the projects, I copied a bunch of web.config "template" files to a special folder under the $(OutDir) folder. These would end up in the build drop from TFS. In my case, the built in configuration file transformations were nowhere close to being sophisticated enough, but if that can work for you it is simpler by far.
  3. The web.config files actually contained references to other config files, one for each configurable feature. This used a custom configuration provider. This technique would also work if you just had a single web.config file though.
  4. I enabled automated deployment (Publish) from the command line, as a new MSBuild target in the same custom MSBuild project that was driving the build.
  5. It was then easy to automate the Publish step in the main build to a VM or QA machine, as well as make it possible to manually deploy to other servers (eventually the staging servers) from the command line.

The web.config templates had stuff like this:

In the connection string:  "Data Source=${SQLINSTANCE};Initial Catalog=${SQLDATABASENAME}..."

It is significant that the delimiters for the replacable tokens is ${ } and not $( ), since MSBuild won't mess with the curly brackets.

In the Publish step, use an MSBuild property function to replace bits in the config files, the following is taken from MSDN's description of MSBuild inline tasks:

<UsingTask
   TaskName="ReplaceToken"
   TaskFactory="CodeTaskFactory"
   AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
   <ParameterGroup>
      <File ParameterType="System.String" Required="true" />
      <Token ParameterType="System.String" Required="true" />
      <Replacement ParameterType="System.String" Required="true" />
   </ParameterGroup>
   <Task>
      <Code Type="Fragment" Language="cs">
      <![CDATA[
         string content = File.ReadAllText(File);
         content = content.Replace(Token, Replacement);
         File.WriteAllText(File, content);
      ]]>
      </Code>
    </Task>
 </UsingTask>

Then in your project, you can use this task with,

<ReplaceToken
  File="PathTo\Web.config"
  Token="${SQLINSTANCE}"
  Replacement=".\SQLEXPRESS"
  />

This is really just a rough guide, all of the parameters to ReplaceToken were also configured in MSBuild item metadata, allowing options for which database, server, security, etc., each of which could be specified individually.

So, for each build/deployment, it would perform the build, copy the config templates, do string replacements on them, then automate the Package/Publish, which is the final bit to go over.

Your best bet is to start with this blog: http://vishaljoshi.blogspot.com/2009/02/web-packaging-creating-web-packages.html which explains a bit, and this answer, which contains likes to a bunch of other related StackOverflow posts MsBuild and MsDeploy with multiple environments, then search online with http://www.bing.com/search?q=msbuild+msdeploy+command+line&go=&form=QBLH&qs=n&sk= to dig deeper. I really hate to just dump you off to a search engine for this part of it, but I've found that there are so many different scenarios it is tough to single one out. About half of the top ten responses have insight on some valuable angle. Reply with more info to help narrow down my response at this point. For my implementation, I called MSDeploy.exe directly from MSBuild using an Exec task. Some things to consider:

  • How to deal with security on the various publish sites. I had to set up a mess of build service accounts, and always required passing the password on the msbuild command line. If you can get by with Windows auth like you suggest it will be a bit easier.
  • For services, I had to use PSExec to run installutil on the remote server
  • There were some additional configuration items that I automated using PSExec calling appcmd on the remote server.
  • It is easy to open up a remote share on the server and use a "net use" command to map and unmap it during the build, you may have some other preference.
  • Performance is tough. For larger sites it can run quite long doing it file by file. RoboCopy isn't any faster. I found using MSDeploy to package remotely (point to local drop as source, and remote share for the package source) was very fast to Rackspace. You may need to first package to a zip file using one MSDeploy call, then push the package remotely using a second.

Hope this gets you started. Please comment or supply more detail in your question if there is something I really missed or glossed over.

Response to comment:

The "Publish" target is something along these lines,

<Target Name="Publish">
   <!-- token replacement in config files, as above -->
   <!-- ...lots of custom setup, selection of various properties used below -->
   <PropertyGroup>
     <_MsDeployExe>$(PROGRAMFILES)\IIS\Microsoft Web Deploy\msdeploy</_MsDeployExe>
     <_MsDeploySourceArg>-source:contentpath="$(_BuildDropFolder)"</_MsDeploySourceArg>
     <_MsDeployDestArg>-dest:contentpath=\\$(_RemoteComputerName)\DropFolder</_MsDeployDestArg>
   </PropertyGroup>
   <Message
     Text="&quot;$(_MsDeployExe)&quot; -verb:sync $(_MsDeploySourceArg) $(_MsDeployDestArg)"
     />
   <Exec
     Condition="'$(DryRun)' != 'true'"
     Command="&quot;$(_MsDeployExe)&quot; -verb:sync $(_MsDeploySourceArg) $(_MsDeployDestArg)"
     ContinueOnError="false"
     WorkingDirectory="$(MSBuildThisFileDirectory)"
     />
  </Target>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜