How to skip delete on folder during publish? - asp.net-mvc

I cannot make it, so that the Publish from visual studio doesnt delete the App_Data folder on the server website. But i would also like it to keep deleting all files (except that folder) to keep the dir "clean".
I have tried this in csproj, .pubxml. And alterations of it (theres one not OnBeforePackageUsingManifest, but iis something)
<PropertyGroup>
<OnBeforePackageUsingManifest>AddCustomSkipRules</OnBeforePackageUsingManifest>
</PropertyGroup>
<Target Name="AddCustomSkipRules">
<ItemGroup>
<MsDeploySkipRules Include="SkipDeleteAppData">
<SkipAction>Delete</SkipAction>
<ObjectName>filePath</ObjectName>
<AbsolutePath>$(_Escaped_PackageTempDir)\\App_Data\\.*</AbsolutePath>
<XPath>
</XPath>
</MsDeploySkipRules>
<MsDeploySkipRules Include="SkipDeleteAppData">
<SkipAction>Delete</SkipAction>
<ObjectName>dirPath</ObjectName>
<AbsolutePath>$(_Escaped_PackageTempDir)\\App_Data\\.*</AbsolutePath>
<XPath>
</XPath>
</MsDeploySkipRules>
</ItemGroup>
</Target>
I even get if i use "SkipAction=Delete" thats it is unable to do so, as Delete is not recognized.
Are there any way to do this? preferably from .pubxml, but csproj will do aswell. Not that much for having to deal with msdeploy command line.
Using visual studio 2015.

Came here looking for a way to keep the "certify the web" .wellknown\acme-challenge folders web.config during a Visual Studio 2019 publish. Thought I'd share it.
Adding the following to pubxml file will cause deploy NOT to delete the web.config during publish.
<ItemGroup>
<MsDeploySkipRules Include="CustomSkipFile">
<ObjectName>filePath</ObjectName>
<AbsolutePath>.well-known\\acme-challenge\\web.config</AbsolutePath>
</MsDeploySkipRules>
</ItemGroup>
Hope this helps somebody!

This (quite recent) SO answer mentions that MsDeploySkipRules settings are effective only when publishing through command line.
When Web Deploy-ing from VS IDE, it suggests checking the following options:
Remove additional files at destination
Exclude files from the App_Data folder

If "Remove additional files at destination" and "Exclude files from the App_Data folder" are both selected, EVERYTHING will be still deleted first and App_Data folder will be ignored (It wont be published).
The only recommendation I can give is to make the folder hidden, this way even "Remove..." is checked it wont be deleted.

Related

Always remove Angular dist folder contents with WebDeploy on Destination

We've got a single page application on Angular 5 with an ASP.NET backend, and when we compile it, the release contents for Angular are output to a folder "Project\dist".
This works great on local dev machines, but all of the dist files are randomized with different names such as:
polyfills.dc7175a7225af84b3c9b.bundle.js
styles.dc7175a7225af84b3c9b.bundle.js
inline.dc7175a7225af84b3c9b.bundle.js
When we use Web Publishing to deploy to staging or production, everything transfers great and our custom folder in the publish profiles is included and published.
However, on the destination server (staging or production) these old, randomly named files and old (no longer used) folders persist. This results in hundreds and hundreds of old files (from old web deploys) that have accumulated on the staging and production servers. I need a method to automatically delete these every time we push updates with webdeploy.
Ideally, the workflow is:
Select publish profile, click Publish
Enter my credentials
Application builds successfully
If app built successfully, we go delete "Project\dist" folder on the destination server. "Project" could be in c:\inetpub\www\project or d:\websites\Project, for example.
Updated files are copied
Web deploy executes and copies the custom files in dist folder (already working).
Here's a redacted version of our current publish profile:
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>MSDeploy</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<MSDeployServiceURL>staging.example.com</MSDeployServiceURL>
<DeployIisAppPath>Project</DeployIisAppPath>
<RemoteSitePhysicalPath />
<SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
<MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
<EnableMSDeployBackup>False</EnableMSDeployBackup>
<UserName>WebDeployUser</UserName>
<PublishDatabaseSettings>
<Objects xmlns="">
</Objects>
</PublishDatabaseSettings>
<ADUsesOwinOrOpenIdConnect>False</ADUsesOwinOrOpenIdConnect>
</PropertyGroup>
<Target Name="CustomCollectFiles">
<ItemGroup>
<_CustomFiles Include="..\Project\dist\**\*" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>dist\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
<CopyAllFilesToSingleFolderForMsdeployDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForMsdeployDependsOn);
</CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
</Project>
I've tried a few accepted answer solutions already and can't get this to work:
https://stackoverflow.com/a/45538847/559988 (I tried this in the csproj and in the publish profile pubxml file.)
https://stackoverflow.com/a/5080942/559988
Any ideas? I have essentially zero knowledge of web deploy aside from setting it up in IIS.
Best,
Chris
EDIT I've also tried this: (Based on this: https://stackoverflow.com/a/15113445/559988)
<Target Name="CleanFolder">
<PropertyGroup>
<TargetFolder>$(_MSDeployDirPath_FullPath)\dist</TargetFolder>
</PropertyGroup>
<ItemGroup>
<FilesToClean Include="$(TargetFolder)\**\*"/>
<Directories Include="$([System.IO.Directory]::GetDirectories('$(TargetFolder)', '*', System.IO.SearchOption.AllDirectories))"
Exclude="$(TargetFolder)"/>
</ItemGroup>
<Delete Files="#(FilesToClean)" ContinueOnError="true"/>
<RemoveDir Directories="#(Directories)" />
</Target>
Update
This is specifically what we're doing: https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/deploying-extra-files
The first comment from there is the same problem we're experiencing:
This comes very handy in deploying Angular distribution files along
with ASP.Net backend, whenever both SPA and the backend share the same
single virtual application. Unfortunately, due to browser cache
busting techniques, the bundle files for Angular deployment will
always ship with unique names and, therefore, an msbuild
command/attribute or other possibility to wipe the folder clean on the
IIS side before sending the updated files would be very welcomed. If
anyone has found a way to do that, please share.
"Sync" functionality described here for msdeploy is exactly what we need to be doing but I don't know how to hook into this:
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/dd569034(v=ws.10)#sync
In a sync operation, if the source file or folder does not exist on
the destination, the provider creates the folder and any subfolders
that have the corresponding files and attributes. If the destination
folder already exists, the provider updates only those objects that do
not match the source. This means that in some cases only one file or
folder will be updated. Files on the destination that do not exist on
the source will be deleted. The source and destination folders for
contentPath do not have to have the same name. If the name of the
destination folder differs from that of the source, the name of the
destination folder will remain the same, but the contents of the
folder will be updated to those of the source.
If I understood you correctly, then resolve this problem is help DeleteExistingFiles property in publish profile.
<DeleteExistingFiles>True</DeleteExistingFiles>
If set to True the output directory (publishUrl) will be purged
before output is written to it, it's good to start out with a clean
slate.
How can I apply this to a specific folder, ex. only delete the
"Project\dist" folder?
As I know result of this property is removing all files. For specify directories to remove you can try verb delete from MSDeploy, that can be wrap in <Exec> task of custom script:
<Exec Command="$(MSDeploy) -verb:delete -dest:"ContentPath=D:\TestDir\Test.txt&quot"/>
/*
* $(MSDeploy) is path to MSDeploy binary that you passed to script.
*/
This example show removing file on local machine. You should customize own call.
Exec Task | How to create a Web Deploy package when publishing a ClickOnce project (Some snippets for using targets)
Try please setting this in publish profile
<SkipExtraFilesOnServer>False</SkipExtraFilesOnServer>
in angular with nodejs, we will handle this problem with 'ng build --output-hashing=false'. Maybe you can search in this scope.

Including files outside of the project in MSBuild

I have found other posts here on StackOverflow that deal with my issue I am experiencing, for example:
MSBuild: Deploying files that are not included in the project as well as Include Files in MSBuild that are not a part of the project
I wanted to share the code that I was able to create after reading these posts and ask for some help as to why it might not be working?
To elaborate on what exactly is not wrong and what I intend to do. I am using Visual Studio 2012, and TFS 2012.
I have a batch file called CreateMyFiles.bat, and what I would like to do is execute this and then take the files it outputs (it outputs them to my Includes/Javascript/Bundled folder) and include them in part of the build in MSBuild (so that they are deployed to the target IIS server).
When I edited my local .csproj in my local Visual Studio and added the code below to the bottom of the file and reloaded, I was able to right click on my web projbect, select 'publish', and then select my local file-based publishing profile which did indeed deploy my files to the correct location. It worked!
I then checked in my code to TFS, and went to 'builds' on TFS, and queued a new build. Sure enough, I was able to see the files output to the same directory on the build server. Now, i'm not 100% sure about MSBuild but I noticed that just like when I hit publish locally, it created a _publishedWebsite folder on the build server as well (a directory above the source). The thing is, within this publishedwebsite folder, my manually created files were not present. Furthermore, going to the target web server after the build was done unfortunately did not have the files I wanted.
So it seems like if I were to manually select publish, the code below works, but if I were to queue a build with TFS, it does not work. Does MSBuild use publish? Could that be the reason it does not work below?
Here is the code I've placed in my .csproj file:
<Target Name="CustomCollectFiles">
<Exec Command="CreateMyFiles.bat" /> <!-- Generate Files -->
<ItemGroup>
<!-- Create an identity called _CustomFiles, and associate it to the files I created -->
<_CustomFiles Include="Includes\JavaScript\Bundled\*" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>Includes\JavaScript\Bundled\*%(Filename)%(Extension) </DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
<!-- Hook into the pipeline responsible for gathering files and tell it to add my files -->
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
<CopyAllFilesToSingleFolderForMsdeployDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
I'm really stuck on this and wanted to ask for some help as to why the files might not be going. I suspect MSBuild doesn't use publish and that's why it works locally (because i'm selecting publish)?
Thanks so much for your help
UPDATE
Tried this as per comments below, but this time the files didn't even appear (so it seemed to not even run the tasks now). Any idea why? Did I type this right?
<Target Name="CustomCollectFiles">
<Exec Command="CreateMyFiles.bat" />
<!-- Generate Files -->
<ItemGroup>
<!-- Create an identity called _CustomFiles, and associate it to the files I created -->
<_CustomFiles Include="Includes\JavaScript\Bundled\*" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>Includes\JavaScript\Bundled\*%(Filename)%(Extension) </DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
<!-- Hook into the pipeline responsible for gathering files and tell it to add my files -->
<PropertyGroup>
<PipelineCollectFilesPhaseDependsOn>
CustomCollectFiles;
$(PipelineCollectFilesPhaseDependsOn);
</PipelineCollectFilesPhaseDependsOn>
</PropertyGroup>
UPDATE 2
When I take the above code, and place it into my pubxml file and then execute an actual publish, it works, but as far as I know our process is to just queue a build from TFS. Is it possible to hook into the above code block when simply queuing a build? Or do I need to publish?
to do a publish from TFS build you need to add the following arguments
/p:DeployOnBuild=true;PublishProfile=Release
obviously using your own PublishProfile name
In VS2012, the target was renamed from:
CopyAllFilesToSingleFolderForPackageDependsOn
to:
CopyAllFilesToSingleFolderForMsdeployDependsOn
Update: looks like the above Targets are not getting called from within VS2012 targets, can you replace it with a call to the Target "PipelineCollectFilesPhaseDependsOn"? That should fix it.
<PropertyGroup>
<PipelineCollectFilesPhaseDependsOn>
CustomCollectFiles;
$(PipelineCollectFilesPhaseDependsOn);
</PipelineCollectFilesPhaseDependsOn>
</PropertyGroup>

How to include custom folders when publishing a MVC application?

I have an "Uploads" folder with logos within in. I would like the VS2012 one-click publish to include this folder. Currently it is not.
How can I achieve this?
I did this for a web api project (not dot net core) which had Angular 6 as a front end. My visual studio version was 2017.
I had created a wwwroot folder where I was compiling angular files via custom build action & this folder was not included in my project.
I edited the project file & added these lines.
<PropertyGroup>
<PipelineCollectFilesPhaseDependsOn>
CustomCollectFiles;
$(PipelineCollectFilesPhaseDependsOn);
</PipelineCollectFilesPhaseDependsOn>
</PropertyGroup>
<Target Name="CustomCollectFiles">
<Message Text="Inside of CustomCollectFiles" Importance="high" />
<ItemGroup>
<_CustomFiles Include="wwwroot\**\*" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>wwwroot\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
I believe you need to set the folder's "Build Action" to "Content":
What are the various "Build action" settings in Visual Studio project properties and what do they do?
I tried all solutions above, but none of them worked. I'm using VS2017 and wasn't able to folder publish some help files. I edited the project file (.csproj) and added the following lines somewhere in de file.
<ItemGroup>
<Content Include="HelpFiles\**\*" />
</ItemGroup>
When I push the publish button all my help files are copied to the publish directory.
Go to Project Properties > Package / Publish Web
Then select the configuration combo that you want to setup up.
Below you have the Items to deploy. I just tested here with "All files in this project folder" and everything was published.
The only downside is that everything is getting deployed, I don't know if this is what you want.
There is an attribute named CopyToPublishDirectory to publish in vs profiles. You can specify this in .csproj file of the project.
<ItemGroup>
<Content Update="Foo\**\*" CopyToPublishDirectory="Always" />
</ItemGroup>
<ItemGroup>
<Content Include="Foo\**\*" />
</ItemGroup>
VS Publish Profiles
in my case i Created a folder in the bin folder and needed to include that folder in the publish.
and that code is worked for me.
<Target Name="CustomCollectFiles">
<ItemGroup>
<_CustomFiles Include=".\bin\Dlls\**\*" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>bin\Dlls\%(RecursiveDir)%(Filename)%(Extension)
</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>CustomCollectFiles;
;</CopyAllFilesToSingleFolderForPackageDependsOn>
<CopyAllFilesToSingleFolderForMsdeployDependsOn>CustomCollectFiles;
;</CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
hope that helps someone.
In visual studio 2019 community, you have to right click on the folder and then click on publish. You can see it is published to the destination i.e. existing publish folder
In visual studio 2022 you can publish a content folder separately by right clicking it and selecting "publish".
While this means an extra step when publishing the whole project, it means that you can just push out new content without having to publish the server again

Avoid deleting folder on Web Publish

I'm deploying my application to an Azure Website. I've configured the Publishing Profile succesfuly and setup tfspreview.com to publish automatically using continuous integration on each code commit.
I have a folder on the path "/media". This folder has pictures and documents uploaded through the CMS (umbraco). This folder gets deleted on each web deploy.
From this answer, I learned how to add a SkipDelete rule on either the .csproj or on the wpp.targets file, but everytime I publish the site the whole folder gets deleted anyway.
Here is the code I'm currently using inside wpp.targets:
<PropertyGroup>
<AfterAddIisSettingAndFileContentsToSourceManifest>
AddCustomSkipRules
</AfterAddIisSettingAndFileContentsToSourceManifest>
</PropertyGroup>
<Target Name="AddCustomSkipRules">
<Message Text="Adding Custom Skip Rules" />
<ItemGroup>
<MsDeploySkipRules Include="SkipMediaFolder">
<SkipAction>Delete</SkipAction>
<ObjectName>filePath</ObjectName>
<AbsolutePath>media</AbsolutePath>
</MsDeploySkipRules>
</ItemGroup>
</Target>
<PropertyGroup>
<UseMsDeployExe>true</UseMsDeployExe>
</PropertyGroup>
Is this not just an issue of unchecking the box in the publish wizard (settings step) that says "Delete all existing files prior to publish"? I know that option is available when setting up publishing from the Visual Studio side - it seems to me the Azure publishing credentials just give you the connection, and not the settings which you make through the wizard.
Looking over the question you are linking to and the code you have supplied above, it seems that you need to change the line:
<AbsolutePath>ErrorLog</AbsolutePath>
to
<AbsolutePath>media</AbsolutePath>
as this refers to the folder you do not want to delete. ErrorLog was the folder the other question's author did not want to delete.

How do I put files in the TFS Build drop location

I'm new to using TFS build. I've got a build defined that runs as a continuous integration. It creates a drop folder, but there's nothing in it.
What's the best practice for moving stuff in the drop folder? I've seen a Binaries folder, do I need to copy things into there, or do I alter the TFSbuild.proj in some way to copy the files I want to the drop folder?
It sounds like you want to copy miscellaneous files from your workspace (or elsewhere) into the drop location?
The target above gives you an example of how to create a target to copy files, but you're probably wondering how to hook it up in your TFSBuild.proj.
A simple way to do this is using one of the pre-defined skeleton targets for this such as AfterDropBuild. If you had a target like the one mentioned above for copying your files you would put this in TFSBuild.proj:
<CreateItem Include="$(SolutionRoot)\Source\RandomFilesInWorkspaceFolder\**\*.*">
<Output TaskParameter="Include" ItemName="RandomFiles" />
</CreateItem>
<Copy SourceFiles="#(RandomFiles)" DestinationFiles="#(RandomFiles->'$(DropLocation)\RandomDestinationSubFolder\%(RecursiveDir)%(Filename)%(Extension)')" />
I seemed to get it working by adding this near the end of my TFSBuild.proj
<Target Name="PackageBinaries">
<ItemGroup>
<FilesToDrop Include="$(SolutionRoot)\MyProduct\Installer\Bin\**\*.msi"/>
</ItemGroup>
<Message Text="FilesToDrop=#(FilesToDrop)"/>
<Copy SourceFiles="#(FilesToDrop)"
DestinationFiles="#(FilesToDrop ->'$(BinariesRoot)\%(RecursiveDir)%(Filename)%(Extension)')"/>
</Target>
It copies wanted msi files into the Binaries folder which the normal tfs build system then copies to the drop location. I noticed the Binaries folder gets deleted everytime a build is started, so you don’t have to worry about cleaning up.
The PackageBinaries target seems to be the standard target name that you can override for doing this kind of thing.
Update Newer versions of TFS probably have better ways!

Resources