能否通过VSTS的AWS CodeDeploy任务排除文件部署?.NET应用配置文件留存咨询
Absolutely, your requirement is completely feasible! This is a standard practice for .NET web application deployments, as .config files (like web.config or app.config) typically store environment-specific configurations—think database connection strings, API keys, or local settings—that shouldn’t be overwritten with every deployment. AWS CodeDeploy was designed to handle exactly this kind of file retention scenario, and you can absolutely implement it using the AWS CodeDeploy task in VSTS (now Azure DevOps).
There are a couple of straightforward ways to pull this off, depending on whether you want to handle exclusion at the build stage or directly within CodeDeploy’s configuration:
1. Use AWS CodeDeploy’s appspec.yml Configuration File
The appspec.yml is the core configuration file for AWS CodeDeploy, and it lets you explicitly define which files to include or exclude during deployment. Here’s how to set it up:
- Create or update your
appspec.ymlfile (make sure it’s in the root of your deployment package) with anexcludelist targeting your.configfiles. For a Windows-based .NET app, the config might look like this:version: 0.0 os: windows files: - source: / destination: C:\inetpub\wwwroot\YourWebApp exclude: - web.config - app.config - **/appsettings.config - When you run the VSTS AWS CodeDeploy task, it will read this
appspec.ymland skip deploying any files listed in theexcludesection. This ensures your server’s existing.configfiles remain untouched.
2. Exclude .config Files During the VSTS Build Stage
If you prefer to keep the .config files out of the deployment package entirely, you can exclude them during your build process:
- DotNet Publish Task: Add the
--exclude-filesparameter to your publish command. For example:dotnet publish --configuration Release --exclude-files web.config,app.config - Archive Files Task: If you’re archiving your build output for deployment, use the "Exclude" field to specify
.configfiles. You can use glob patterns like**/*.configto exclude all config files, or target specific ones likeweb.config.
3. Optional: Use CodeDeploy Hook Scripts for Advanced Retention
If you need more control (like backing up config files before deployment, or conditionally retaining them), you can use CodeDeploy’s hook scripts. For example, add a BeforeInstall hook in appspec.yml to back up your existing .config files to a temporary location, then restore them after deployment completes. This is overkill for basic exclusion, but useful for complex workflows.
Just make sure your VSTS AWS CodeDeploy task is pointed at the correct deployment package (either the one with the configured appspec.yml or the build output where .config files were excluded), and you’re good to go.
内容的提问来源于stack exchange,提问作者Yasmin AbdelGhaffar

