CC.NET:单个项目中的多个 MSBuild 任务

CC.NET: Multiple MSBuild-Tasks within a single project

在 CruiseControl.NET 1.8.4 中,我 运行 使用多个 MsBuild-Tasks 构建一个项目。项目配置的简短摘录,由 CCValidator 解析和验证:

<tasks>
  <msbuild>
    <buildArgs>msbuild-base.xml /m /p:BuildInParallel="true";Configuration="Release";Platform="Any CPU";nr="false";ccversionbase=15.1.0;NuGetProjectDir=D:\REPO\Repo-Base /verbosity:minimal /nologo /target:PreBuild</buildArgs>
    <dynamicValues />
    <environment />
    <executable>C:\Program Files (x86)\MSBuild.0\Bin\MSBuild.exe</executable>
    <logger>"C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MSBuild.dll"</logger>
    <loggerParameters />
    <priority>Normal</priority>
    <timeout>120</timeout>
    <workingDirectory>D:\BaseLibraries</workingDirectory>
  </msbuild>
  <msbuild>
    <buildArgs>msbuild-other.xml /m /p:BuildInParallel="true";Configuration="Release";Platform="Any CPU";nr="false";ccversionbase=15.1.0;revfile=D:\REPO\Repo-Base\ci-revision.xml /verbosity:minimal /nologo /target:CIFull</buildArgs>
    <dynamicValues />
    <environment />
    <executable>C:\Program Files (x86)\MSBuild.0\Bin\MSBuild.exe</executable>
    <logger>"C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MSBuild.dll"</logger>
    <loggerParameters />
    <priority>Normal</priority>
    <timeout>120</timeout>
    <workingDirectory>D:\BaseLibraries</workingDirectory>
  </msbuild>
</tasks>

还有很多任务,但这应该足以说明问题了。所有任务都有不同的 msbuild 文件,并且不是同一文件中的目标。

要成功构建 运行 项目,前几个任务必须成功完成。所以当任务失败时 CC.NET 停止构建是完美的。但是,如果后面的任务之一失败,我想让 CC.NET 继续下一个任务,而不是停止整个构建。

如何配置 CC.NET 以忽略失败的任务并继续下一个任务?

如果您将可能总是 运行 的任务放在发布者部分, 他们将是 运行,无论他们是否失败。

如果顺序是这样的,问题就更严重了: MayNotFail,MayFail,MayFail,MayNotFail

对于那种情况我不能立即想到解决方案

CruiseControl.NET 当其中一项任务失败时,让构建停止并失败。我希望在触发所有任务的情况下完成构建,即使一个任务失败。奖励:如果失败,我想要 "failed" 上的构建状态,但不会在第一个失败事件上停止。

所以我将所有非关键 ("optional") 任务放入一个额外的 MsBuild-Script:

<Project DefaultTargets="CIFull" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <PropertyGroup>
        <MsBuildAgent>"C:\Program Files (x86)\MSBuild.0\Bin\MSBuild.exe"</MsBuildAgent>
        <MsBuildArguments>msbuild.xml /m /p:BuildInParallel="true";Configuration="Release";Platform="Any CPU";nr="false";ccversionbase=$(ccversionbase);revfile=$(revfile);buildplans=$(buildplans) /verbosity:minimal /nologo /target:CIFull /nr:false</MsBuildArguments>
    </PropertyGroup>

    <Target Name="Libraries">
        <Exec Command="$(MsBuildAgent) $(MsBuildArguments)" WorkingDirectory="$(buildplans)\MyLibraryOne" IgnoreExitCode="false" ContinueOnError="true">
            <Output TaskParameter="ExitCode" PropertyName="LibExitCode01" />
        </Exec>
        <Exec Command="$(MsBuildAgent) $(MsBuildArguments)" WorkingDirectory="$(buildplans)\MyLibraryTwo" IgnoreExitCode="false" ContinueOnError="true">
            <Output TaskParameter="ExitCode" PropertyName="LibExitCode02" />
        </Exec>
        <Exec Command="$(MsBuildAgent) $(MsBuildArguments)" WorkingDirectory="$(buildplans)\MyLibraryThree" IgnoreExitCode="false" ContinueOnError="true">
            <Output TaskParameter="ExitCode" PropertyName="LibExitCode03" />
        </Exec>
    </Target>

    <Target Name="Applications">
        <Exec Command="$(MsBuildAgent) $(MsBuildArguments)" WorkingDirectory="$(buildplans)\ApplicationOne" IgnoreExitCode="false" ContinueOnError="true">
            <Output TaskParameter="ExitCode" PropertyName="AppExitCode01" />
        </Exec>
        <Exec Command="$(MsBuildAgent) $(MsBuildArguments)" WorkingDirectory="$(buildplans)\ApplicationTwo" IgnoreExitCode="false" ContinueOnError="true">
            <Output TaskParameter="ExitCode" PropertyName="AppExitCode02" />
        </Exec>
        <Exec Command="$(MsBuildAgent) $(MsBuildArguments)" WorkingDirectory="$(buildplans)\ApplicationThree" IgnoreExitCode="false" ContinueOnError="true">
            <Output TaskParameter="ExitCode" PropertyName="AppExitCode03" />
        </Exec>
    </Target>

    <Target Name="ValidateExitCodes">
        <ItemGroup>
            <Failed Condition="$(LibExitCode01) != 0" Include="MyLibraryOne" />
            <Failed Condition="$(LibExitCode02) != 0" Include="MyLibraryTwo" />
            <Failed Condition="$(LibExitCode03) != 0" Include="MyLibraryThree" />
            <Failed Condition="$(AppExitCode01) != 0" Include="ApplicationOne" />
            <Failed Condition="$(AppExitCode02) != 0" Include="ApplicationTwo" />
            <Failed Condition="$(AppExitCode03) != 0" Include="ApplicationThree" />
        </ItemGroup>
        <ItemGroup>
            <Success Condition="$(LibExitCode01) == 0" Include="MyLibraryOne" />
            <Success Condition="$(LibExitCode02) == 0" Include="MyLibraryTwo" />
            <Success Condition="$(LibExitCode03) == 0" Include="MyLibraryThree" />
            <Success Condition="$(AppExitCode01) == 0" Include="ApplicationOne" />
            <Success Condition="$(AppExitCode02) == 0" Include="ApplicationTwo" />
            <Success Condition="$(AppExitCode03) == 0" Include="ApplicationThree" />
        </ItemGroup>
        <Message Importance="High" Text="*** Success: @(Success->Count()) (@(Success, ' '))" />
        <Message Importance="High" Text="*** Failed:  @(Failed->Count()) (@(Failed, ' '))" />
        <Error Text="*** FAILED: @(Failed, ' ') ***" Condition="'@(Failed->Count())' > 0" />
    </Target>

    <Target Name="CIFull" DependsOnTargets="Libraries;Applications;ValidateExitCodes" />

</Project>

不再将每个 "optional" 库和应用程序作为任务添加到 CC.NET 项目,它们现在列在上面的 MsBuild 文件中。 CC.NET配置修改如下:

<tasks>
  <msbuild>
    <!-- When this task fails, the build shall stop and fail -->
    <buildArgs>msbuild-base.xml /m /p:BuildInParallel="true";Configuration="Release";Platform="Any CPU";nr="false";ccversionbase=15.1.0;NuGetProjectDir=D:\REPO\Repo-Base /verbosity:minimal /nologo /target:PreBuild</buildArgs>
    <dynamicValues />
    <environment />
    <executable>C:\Program Files (x86)\MSBuild.0\Bin\MSBuild.exe</executable>
    <logger>"C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MSBuild.dll"</logger>
    <loggerParameters />
    <priority>Normal</priority>
    <timeout>120</timeout>
    <workingDirectory>D:\BaseLibraries</workingDirectory>
  </msbuild>
  <msbuild>
    <!-- This task is the new build file from above. -->
    <buildArgs>msbuild-new.xml /m /p:BuildInParallel="true";Configuration="Release";Platform="Any CPU";nr="false";ccversionbase=15.1.0;revfile=D:\REPO\Repo-Base\ci-revision.xml;buildplans=D:\Meta\BuildPlans /verbosity:minimal /nologo /target:CIFull</buildArgs>
    <dynamicValues />
    <environment />
    <executable>C:\Program Files (x86)\MSBuild.0\Bin\MSBuild.exe</executable>
    <logger>"C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MSBuild.dll"</logger>
    <loggerParameters />
    <priority>Normal</priority>
    <timeout>120</timeout>
    <workingDirectory>D:\Meta</workingDirectory>
  </msbuild>
</tasks>

CC.NET 先做第一个任务。如果失败,则不会执行其他任务并报告错误。还好吗,CC.NET继续第二个任务

现在,所有库和应用程序都已构建 - 忽略(但保存)return 值(目标 LibrariesApplications)。目标 ValidateExitCodes 现在创建成功和失败任务的列表 - 在所有任务 运行 之后。如果失败任务列表包含一个元素,则错误将 return 编辑为 CC.NET。 CC.NET 会将构建状态设置为错误。