asp.net-mvc - 由 TeamCity 触发的构建步骤始终会构建 - 即使没有任何更改

标签 asp.net-mvc powershell msbuild teamcity psake

问题:我正在将 TeamCity 设置为 ASP.NET MVC 项目的构建服务器。我使用 Powershell 和 psake 对我们的 .csproj 文件运行 msbuild 并创建可部署的包。从构建服务器,我可以打开 powershell,运行脚本,并且由于没有源代码更改,msbuild 不会重新生成项目 DLL 文件。但是,当我从 TeamCity Web 界面调用完全相同的脚本时,即使没有任何更改,msbuild 始终会重建并重新生成 DLL 文件。 AFAIK 不应该这样做。

我已将这个问题缩小到一个步骤。为了简单起见,我设置了 TeamCity 配置,因此它不使用任何源代码控制,而是运行一个调用我的 powershell 脚本的“powershell”构建步骤。

powershell 脚本运行单个命令:

exec { &$msbuild $ProjectFile /t:Package "/p:PackageLocation=$PackageFile;OutDir=$TempPath;Configuration=$Config;SolutionDir=$BaseDir\Source\" /v:m }

当我从 powershell 命令行手动调用脚本时,我看到:

CoreCompile:
Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files.

当我通过 TeamCity 调用完全相同的脚本时,我看到:

[11:11:26]: CoreCompile:
[11:11:26]:   c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig ...
<SNIP>
[11:11:32]: CopyFilesToOutputDirectory:
[11:11:32]:   Copying file from "obj\Demo\Website.Web.dll" to "d:\deploy\Build\package\Demo\temp\Website.Web.dll".
[11:11:32]:   Website.Web -> d:\deploy\Build\package\Demo\temp\Website.Web.dll
[11:11:32]:   Copying file from "obj\Demo\Website.Web.pdb" to "d:\deploy\Build\package\Demo\temp\Website.Web.pdb".
[11:11:32]: _CopyWebApplicationLegacy:
[11:11:32]:   Copying Web Application Project Files for Website.Web
[11:11:32]:   Copying file from "obj\Demo\Website.Web.dll" to "d:\deploy\Build\package\Demo\temp\_PublishedWebsites\Website.Web\bin\Website.Web.dll".
[11:11:32]:   Copying file from "obj\Demo\Website.Web.pdb" to "d:\deploy\Build\package\Demo\temp\_PublishedWebsites\Website.Web\bin\Website.Web.pdb".
[11:11:32]:   Copying file from "d:\deploy\Build\package\Demo\temp\Website.Data.dll" to "d:\deploy\Build\package\Demo\temp\_PublishedWebsites\Website.Web\bin\Website.Data.dll".
[11:11:32]:   Copying file from "d:\deploy\Build\package\Demo\temp\Website.Data.pdb" to "d:\deploy\Build\package\Demo\temp\_PublishedWebsites\Website.Web\bin\Website.Data.pdb".

有什么想法为什么从 TeamCity 运行此脚本会导致 msbuild 检测更改并重建,但手动运行完全相同的脚本却不会?

更新: 认为这可能是由 TeamCity Powershell 运行程序的一些怪癖引起的,我只是尝试制作一个批处理文件,将脚本传递到 Powershell.exe 并使用命令行运行程序调用它:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -File D:\deploy\Build\run-build.ps1 && exit /b %ERRORLEVEL%

我得到了完全相同的行为。如果我从命令行调用这个批处理文件,msbuild 将跳过编译。如果我从 TeamCity 调用它,DLL 将被重新编译。

更新#2: Eureka !我在msbuild中打开了诊断调试,找到了强制重新编译的原因。它是由 GenerateTargetFrameworkMonikerAttribute 目标引起的。以下是日志输出中的关键位:

[15:23:28]: Target "GenerateTargetFrameworkMonikerAttribute" in file "c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets" from project "d:\deploy\source\Website.Data\Website.Data.csproj" (target "BeforeCompile" depends on it):
[15:23:28]: Building target "GenerateTargetFrameworkMonikerAttribute" completely.
[15:23:28]: Output file "C:\TeamCity\buildAgent\temp\buildTmp\.NETFramework,Version=v4.0.AssemblyAttributes.cs" does not exist.
[15:23:28]: Using "WriteLinesToFile" task from assembly "Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
[15:23:28]: Task "WriteLinesToFile"
[15:23:28]: Done executing task "WriteLinesToFile".
[15:23:28]: Done building target "GenerateTargetFrameworkMonikerAttribute" in project "SMM.Data.csproj".

看起来此目标在 TEMP 环境变量中指定的 TEMP 目录中创建/更新 AssemblyAttributes 文件。显然,TeamCity 覆盖了 TEMP 环境变量并将其设置为:C:\TeamCity\buildAgent\temp\buildTmp,并且该目录在每次构建之前都会被清理。

如果我从 powershell 调用 Get-ChildItem Env: ,我可以看到这一点:

TEMP                           C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
TMP                            C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp

但是如果我从 TeamCity 调用的 powershell 脚本调用它:

TEMP                           C:\TeamCity\buildAgent\temp\buildTmp            
TMP                            C:\TeamCity\buildAgent\temp\buildTmp   

关键是这个文件重新生成后:

[15:23:28]: Building target "CoreCompile" completely.
[15:23:28]: Input file "C:\TeamCity\buildAgent\temp\buildTmp\.NETFramework,Version=v4.0.AssemblyAttributes.cs" is newer than output file "obj\Demo\SMM.Data.pdb".

这就是整个项目重新编译的原因。

当我从 Powershell 运行脚本时,临时目录不会更改或清理,并且构建会按预期运行。

那么,有人知道如何更改创建此 AssemblyAttributes 文件的目录,或者告诉 TeamCity 使用不同的 TEMP 目录吗?我必须相信这是其他人也遇到过的问题。

谢谢!

最佳答案

所以,正如我在上面的“更新#2”中提到的,问题似乎是由两件事引起的: - TeamCity 将 TEMP 和 TMP 环境变量设置为其自己的临时目录 - TeamCity 在每次构建之前“清理”此临时目录 - msbuild 进程的一部分运行一个GenerateTargetFrameworkMonikerAttribute 目标,该目标更新TEMP 环境变量指定的目录中的特定文件 - 导致编译器需要重新编译整个项目

一旦我弄清楚了这一点,我就在这个不相关的问题中找到了适用的答案: In Visual Studio 2010 why is the .NETFramework,Version=v4.0.AssemblyAttributes.cpp file created, and can I disable this?

所以我添加了:

<Target Name="GenerateTargetFrameworkMonikerAttribute" />

我的解决方案中的两个项目都编译为 DLL,并且它有效。

关于asp.net-mvc - 由 TeamCity 触发的构建步骤始终会构建 - 即使没有任何更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7932698/

相关文章:

数据注释中的 Asp.Net Mvc 隐藏字段

powershell - Powershell:在html报告的顶部一次显示表头

powershell - 去掉使用convert-tohtml时出现的powershell Mysql查询结果中不需要的列

powershell - 随着 Powershell 的迅速普及,是否还有理由学习 VBScript?

c# - .csproj 文件中自动生成的 IntermediateOutputPath

visual-studio - MSB3644 找不到框架的引用程序集

asp.net-mvc - 在生产服务器上未调用OnException

asp.net-mvc - aspnet-api-versioning 有什么好处?

visual-studio-2010 - MS Build 是否可以在不单独指定的情况下包含所有库项目代码文件,并与 Visual Studio 良好配合?

c# - 如何在 Asp.net MVC 中使用文件类型将文件内容保存在数据库表中