由TeamCity触发的构建步骤始终构建 - 即使没有更改

obl*_*joe 6 msbuild asp.net-mvc powershell 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 }
Run Code Online (Sandbox Code Playgroud)

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

CoreCompile:
Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files.
Run Code Online (Sandbox Code Playgroud)

当我通过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".
Run Code Online (Sandbox Code Playgroud)

从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%
Run Code Online (Sandbox Code Playgroud)

我得到了完全相同的行为.如果我从命令行调用此批处理文件,则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".
Run Code Online (Sandbox Code Playgroud)

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

如果我调用Get-ChildItem Env,我可以看到这个:来自powershell:

TEMP                           C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
TMP                            C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
Run Code Online (Sandbox Code Playgroud)

但是,如果我从来自TeamCity调用的powershell脚本中调用它:

TEMP                           C:\TeamCity\buildAgent\temp\buildTmp            
TMP                            C:\TeamCity\buildAgent\temp\buildTmp   
Run Code Online (Sandbox Code Playgroud)

关键是在此文件被重新审核后:

[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".
Run Code Online (Sandbox Code Playgroud)

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

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

那么,任何人都知道如何更改此AssemblyAttributes文件的目录,或者告诉TeamCity使用不同的TEMP目录?我不得不相信这是其他人遇到的问题.

谢谢!

obl*_*joe 6

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

一旦我弄清楚这一点,我在这个无关的问题中找到了一个适用的答案: 在Visual Studio 2010中,为什么创建了.NETFramework,Version = v4.0.AssemblyAttributes.cpp文件,我可以禁用它吗?

所以我补充说:

<Target Name="GenerateTargetFrameworkMonikerAttribute" />
Run Code Online (Sandbox Code Playgroud)

在我的解决方案中编译为DLL的两个项目,它工作.