We recently switched from using SVN to Git and GitHub. There are many advantages but there’s one in particular I want to talk about.

Using TeamCity you can automatically build all pull requests. This is incredibly useful as it ensures only code that compiles successfully and passes all unit tests gets back into master. If the build fails you are notified in GitHub.

But, there’s a problem. View errors are not reported because MvcBuildViews is off. One solution is to enable MvcBuildViews for all web projects but in our case this is best avoided as it roughly quadruples compile time. The ideal solution is to enable MvcBuildViews only if a view has been changed in the current build. Unfortunately I struggled to work out how to do this. So I asked a question on Stack Overflow and the first answer I received pointed me the right direction.

I’ve managed to improve on my Stack Overflow answer. It simply enables MvcBuildViews for all .csproj files if a view has changed, but this is still quite slow. It’s much faster to only enable the setting for projects in which views have changed. This may be overkill for many solutions but for ours it’s not.

A few things to bear in mind:

  • I’m no PowerShell expert
  • You’ll probably what to change the Get-ChildItem command in step one to point directly at your web project .csproj files rather than all .csproj files
  • The Write-Host commands must be formatted in this way so they appear in the TeamCity build log
  • By default TeamCity does not do a clean checkout of the source code (although this can be enabled). Instead changes are applied incrementally meaning that alterations to .csproj files are not undone on each build. So MvcBuildViews should be reset on every build
  • %system.teamcity.build.changedFiles.file% – This file lists all the changes in the current build in the format [fileName]:[status]:[commitHash]. E.g.Home/Index.cshtml:CHANGED:1400ea14c3196abbfd8de3ab6fe0a902be2ccad8
  • %teamcity.build.workingDir% – This is the directory that contains all the files to be built