【问题标题】:Build step triggered by TeamCity always builds - even when there are no changesTeamCity 触发的构建步骤始终构建 - 即使没有更改
【发布时间】:2011-10-28 17:28:35
【问题描述】:

问题:我将 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: 尤里卡!我在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 目录吗?我必须相信这是其他人遇到的问题。

谢谢!

【问题讨论】:

    标签: asp.net-mvc powershell msbuild teamcity psake


    【解决方案1】:

    所以,正如我在上面的“更新#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 并且它工作正常。

    【讨论】:

    • 我能够重现该问题,并且得出了类似的结论。无论如何,很高兴你找到了答案。尽可能接受你的答案。
    • 似乎 TeamCity 做了它必须做的事情,因为依赖于临时文件夹或任何其他特定文件夹在 CI 中不是一个好主意。
    【解决方案2】:

    作为 obliojoe 答案的变体,如果您不想或无法更改单个项目文件,您可以将这些文件备份到/从 TEMP 文件夹中恢复:

    1. 第一次尝试从备份中恢复文件:

      copy temp\*.* %%temp%% /y
      echo AssemblyAttributes restore attempted
      
    2. 然后使用 TeamCity 构建运行程序执行构建步骤

    3. 备份文件:

      mkdir temp 2> nil
      copy %%temp%%\*AssemblyAttributes.cs temp /y
      echo AssemblyAttributes files saved
      

    两个批处理文件需要从同一个目录运行。

    请注意这些批处理文件中的最终 ECHO,它可以保证成功退出(错误代码 0)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 2012-12-28
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多