TeamCity 2017.2.1配置:ASP.NET MVC发布前替换指定Bin文件
Absolutely you can set this up in TeamCity 2017.2.1—let’s walk through exactly how to replace those third-party library files in your Bin directory after build success but before deployment:
1. Add a Post-Build File Replacement Step
First, you’ll need to insert a new build step after your main project build step but before your deployment step (drag and drop to reorder steps if needed). You have two solid options here:
Option A: Command Line Step
- Set the step type to
Command Line. - Use the
copycommand with the/Yflag to overwrite existing files without prompting. Example command:copy /Y "\\your-network-share\deploy-specific-libs\ThirdPartyLibrary.dll" "%system.teamcity.build.workingDir%\YourMvcProject\Bin\ThirdPartyLibrary.dll"%system.teamcity.build.workingDir%is a built-in TeamCity variable that points to your project’s build working directory—no need to hardcode paths!- For multiple files, use a wildcard:
copy /Y "\\your-network-share\deploy-specific-libs\*.dll" "%system.teamcity.build.workingDir%\YourMvcProject\Bin\"
Option B: PowerShell Step
- Set the step type to
PowerShell. - Use
Copy-Itemwith the-Forceparameter to ensure overwrites. Example script:
PowerShell is great if you need more logic (like checking if the file exists before replacement).Copy-Item -Path "\\your-network-share\deploy-libs\ThirdPartyLibrary.dll" -Destination "$env:SYSTEM_TEAMCITY_BUILD_WORKINGDIR\YourMvcProject\Bin\" -Force
2. (Optional but Recommended) Use TeamCity Artifacts for Deployment Libraries
Instead of relying on a network share, you can store your deployment-specific libraries as artifacts in a dedicated TeamCity build configuration. Then:
- Go to your MVC project’s build configuration → Dependencies → Add an Artifact Dependency.
- Select the build configuration that hosts your deployment libraries, and specify the artifact path (e.g.,
deploy-libs/* => deploy-libs/). This pulls the files into your build’s working directory under adeploy-libsfolder. - Update your replacement command to use this local path (e.g.,
copy /Y "%system.teamcity.build.workingDir%\deploy-libs\*.dll" "%system.teamcity.build.workingDir%\YourMvcProject\Bin\"). This makes your pipeline more self-contained and less dependent on external resources.
3. Verify Deployment Step Uses Modified Files
Double-check that your deployment step (whether it’s Web Deploy, MSDeploy, or a custom script) targets the same Bin directory you just modified. TeamCity’s default deployment steps typically use the build’s output directory by default, so as long as your replacement step runs first, the updated files will be included in the deployment.
4. Test the Workflow
Run a test build and check the TeamCity logs to confirm the replacement step executed successfully. Then, after deployment, verify the Bin directory on your target server has the deployment-specific library versions.
内容的提问来源于stack exchange,提问作者Andrii Khomiak

