使 MSDeploy (Visual Studio) 不删除 App_Data 文件夹,而是删除其他所有内容
我正在使用 Visual Studio 的Publish
按钮来部署我的网站,并希望在服务器上有一个不同的 App_Data 文件夹。有一个在目标上保留额外文件(不要删除)
的复选框,它可以防止我的 App_Data 文件夹被删除,但随着网站的更改,它最终会积累大量残留文件。
有什么办法让它在删除所有内容时只排除 App_Data 吗?
I'm using Visual Studio's Publish
button to deploy my website, and want a different App_Data folder on the server. There's a checkbox for Leave extra files on destination (do not delete)
which prevents my App_Data folder from getting deleted, but then it'll eventually accumulate a lot of vestigial files as the website changes.
Is there any way to make it exclude just App_Data when it deletes everything?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当您手动调用 msdeploy 时即可完成 - 只需添加以下参数:
请参阅 Web 部署操作设置。路径是正则表达式,所以相当灵活。
如果使用 VS 生成的 ProjectName.deploy.cmd 脚本进行部署,还可以在 _MsDeployAdditionalFlags 环境变量中传递此参数(运行该脚本时)。
这是我针对我们的需求提出的最好的方案(我们的情况与您类似)。我还没有尝试将它与 VS 的“发布”按钮集成,因为我们是从命令行部署的。
编辑:
自从发布此答案以来,我已经了解了一些有关 MSDeploy 的知识,所以我想现在就更新它。
首先,上述跳过规则会跳过匹配路径(App_Data)上的任何操作。如果需要更精细的控制,可以使用更详细的语法。例如,仅跳过删除(将任何额外文件保留在目标服务器上,但添加任何新文件并更新现有文件):
这会跳过 App_Data 中所有文件和所有子文件夹(及其所有内容)的删除,但不会删除防止添加和更新。
另一个有用的事情是可以在项目文件 (
.csproj
) 中定义跳过规则,以便它们自动包含在与包一起生成的.deploy.cmd
脚本中。这使得无需通过 _MsDeployAdditionalFlags 将它们传递给脚本。如果
csproj
文件中包含以下内容,则会添加上述跳过规则:(名称
AddCustomSkipRules
和SkipDeleteAppData
完全是任意的;$(_Escaped_PackageTempDir)
应该可能需要,但在实践中我总是看到它评估为空字符串)请参阅Web 部署:自定义部署包 和 如何在 .csproj 文件中设置 MSDeploy 设置了解详细信息。
需要注意的是:这只会将这些规则添加到
.deploy.cmd
脚本中,因此如果您想使用图形 IIS 管理器进行包部署,它是无用的,因为它不使用该脚本(从 VS 部署可能也是如此,但我还没有检查过)。It can be done when you invoke msdeploy manually - just add the following parameter:
See Web Deploy Operation Settings. The path is a regular expression, so it is quite flexible.
If you deploy using the VS-generated ProjectName.deploy.cmd script, you can also pass this parameter in the _MsDeployAdditionalFlags environment variable (when running that script).
This is the best I've come up with for our needs (we have a similar situation as you). I haven't tried integrating it with VS's Publish button, since we deploy from command line.
EDIT:
I have learned a few things about MSDeploy since I posted this answer, so I thought I'd update it now.
First of all, the above skip rule skips any operations on the matching path (App_Data). If more granular control is needed, a more verbose syntax is available. For example, to skip only deletes (to keep any extra files on target server, but add any new ones and update existing ones):
This skips deletes of all files and all subfolders (with all their content) in App_Data, but doesn't prevent adds and updates.
Another useful thing is that skip rules can be defined in the project file (
.csproj
) so that they are automatically included in the.deploy.cmd
script generated along with the package. This makes it unnecessary to pass them to the script through _MsDeployAdditionalFlags.The above skip rule will be added if the following is included in
csproj
file:(the names
AddCustomSkipRules
andSkipDeleteAppData
are completely arbitrary;$(_Escaped_PackageTempDir)
is supposed to be possibly needed, but in practice I've always seen it evaluate to an empty string)See Web Deploy: Customizing a deployment package and How to set MSDeploy settings in .csproj file for more info.
One caveat: this only adds those rules to the
.deploy.cmd
script, so it is useless if you want to use the graphical IIS Manager for package deployment, as it doesn't use that script (the same probably goes for deployment from VS, but I haven't checked).根据我的经验,
MsDeploySkipRules
仅在从命令行部署时运行。如果要从 Visual Studio 发布到 Azure(或使用其他 Web 部署方法),则可以在发布时设置以下内容。
当选中“删除目的地的其他文件”时,它将在您正在部署的文件和文件夹与服务器上的文件和文件夹之间进行比较。
请注意,如果您有用户生成的内容(例如上传),您可能会遇到问题。但这可以通过将这些文件夹存储在不同的位置(例如 S3/Azure 存储)来解决。
From my experience,
MsDeploySkipRules
are only run when deploying from the command line.If you are publishing from Visual Studio to Azure (or using another Web Deploy method), you can set the following when Publishing.
When "Remove additional files at destination" is checked, it will make a comparison between the files and folders you are deploying and the ones on the server.
Be warned, you may run into issues if you have User Generated content, e.g. Uploads. But this could be worked around by storing those folders in a different location, e.g. S3 / Azure Storage.
在 Powershell 中,如果您想使用 msdeploy.exe 或 myproj.deploy.cmd (部署Web包),以避免删除App_Data文件夹并避免
错误,您必须将skip指令括在三引号中,例如:
From Powershell, if you want to use msdeploy.exe or the myproj.deploy.cmd (Deploying Web Packages) produced when publishing with Web Deploy Package, in order to to skip deleting the App_Data folder and avoid the
error, you have to enclose the skip directive in triple quotes, e.g. :
您是否查看过项目设置中的 Package/Publish Web,因为您可以告诉它排除 App_Data 文件夹中的文件。
Have you looked at Package/Publish Web in the project settings, because you can tell it to exlude files in the App_Data folder.
对于 asp.net core Web 应用程序,请在 csproj 中使用
MsDeploySkipRules
。将
替换为根文件夹https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-配置文件?view=aspnetcore-2.1#exclude-files
For asp.net core web apps use
MsDeploySkipRules
in csproj.Replace
<dir_name>
with your root folderhttps://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-2.1#exclude-files
这并不理想,因为您可能会复制很多文件(我没有),但这是我备份文件夹的解决方案。适合在发布过程中将文件夹移动到将要发布的位置。将其放入您的 pubxml 文件中:
如果您只想执行任意数量的特定已知文件,请为每个文件执行一个复制块:
This isn't ideal since you may be copying a lot of files doing this (I'm not), but here's my solution for backing up a folder. Adapted to move the folder to the location from which it will be published, during the publish process. Put this in your pubxml file:
And if you just want to do any number of specific known files, do one Copy block per file:
将其放入您的 pubxml 文件中:
Put this in your pubxml file: