Specifying folders not to sync in Web Deploy

Mike Cole picture Mike Cole · Aug 15, 2013 · Viewed 8.9k times · Source

I use the following script to deploy my ASP.NET MVC app to our web server:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe MySolution.sln^
/p:Configuration=TeamCity-Test^
/p:OutputPath=bin^
/p:DeployOnBuild=True^
/p:DeployTarget=MSDeployPublish^
/p:MsDeployServiceUrl=https://mywebserver.com:8172/msdeploy.axd^
/p:username=MyDomain\MyUser^
/p:password=MyPassword^
/p:AllowUntrustedCertificate=True^
/p:DeployIisAppPath=mywebsitename.com^
/p:MSDeployPublishMethod=WMSVC

Now I need to specify to not sync the /uploads folder. Can I specify that in this script? Thanks!

Clarification: I have the Uploads folder in my project. I'd like for Web Deploy to create the folder. I do not want it to delete the folder/subfolders/files from my web server because it contains user-uploaded content.

Clarification #2: I just found the SkipExtraFilesOnServer=True option. However, I don't want this to be global. I'd like to set it to a single folder.

Answer

Isantipov picture Isantipov · Aug 16, 2013

UPDATE: Apparently, what you really want is prevent web deploy from removing existing directory on the destination server, but still have the folder created in case it's not there. You can accomplish this as follows:

  • create YourWebProjectName.wpp.targets file next to you the project file with the following content:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <ItemGroup>
          <MsDeploySkipRules Include="SkipELMAHFolderFiles">
          <SkipAction></SkipAction>
          <ObjectName>filePath</ObjectName>
          <AbsolutePath>$(_DestinationContentPath)\\NameOfYourFolder\\.*</AbsolutePath>
          <Apply>Destination</Apply>
          <XPath></XPath>
        </MsDeploySkipRules>
    
        <MsDeploySkipRules Include="SkipELMAHFolderChildFolders">
          <SkipAction></SkipAction>
          <ObjectName>dirPath</ObjectName>
          <AbsolutePath>$(_DestinationContentPath)\\NameOfYourFolder\\.*\\*</AbsolutePath>
          <Apply>Destination</Apply>
          <XPath></XPath>
        </MsDeploySkipRules>
      </ItemGroup>
    </Project>
    

Change NameOfYourFolder and YourWebProjectName accordingly. This assumes, you have it in the root, I believe, you can use relative path if it's not the case.

  • The first MsDeploySkipRules entry tells webdeploy not to remove any files in Name_OfYourFolder.
  • The second MsDeploySkipRules tells webdeploy not to remove any child folders in Name_OfYourFolder.

Also, to have the folder created if it's not present on the destination server, you have to do the following:

  • include the folder into the project
  • add a dummy DeployemntPlaceholder.txt file into it and include it into the project as well

DeployemntPlaceholder.txt is required to tell MSBUild to add the folder into the package: empty folders are ignored.

I've tested this approach and it works fine when you run publish in the manner you've shown. I've used this answer to get the msbuild items syntaxt right. I believe, this is a MSBuild way to customize flags, passed to webdeploy by MSBuild Deployment Pipeline.

If you ran MSDeploy directly, you could use skip arguments in the following manner:

-skip:objectname='filePath',absolutepath='logs\\.*\\someNameToExclude\.txt'

UPDATE 2

You might also want to have ACL write permissions set on your \Uploads folder - there's a complete guide to do this: Setting Folder Permissions On Web Publish

Conserning the original question "Specifying folders not to sync in Web Deploy", the easiest way to do this is as follows:

You can create a publish profile and add the following lines:

<PropertyGroup>
  <ExcludeFilesFromDeployment>
    File1.aspx;File2.aspx
  </ExcludeFilesFromDeployment>
  <ExcludeFoldersFromDeployment>
    Folder1;Folder2
  </ExcludeFoldersFromDeployment>
</PropertyGroup> 

I've tested this approach for excluding files using publish profiles. An easy guide is here (scroll to Edit the .pubxml file to exclude robots.txt section).

You can also do this in .wpp.targets file or edit you csproj. See more information at Web Deployment FAQ for Visual Studio and ASP.NET