【问题标题】:Determine how a TFS 2015 build was initiated from inside Msbuild确定如何从 Msbuild 内部启动 TFS 2015 构建
【发布时间】:2016-04-20 20:47:26
【问题描述】:

在 TFS2015 上的 MSBuild 中是否有任何属性等(不是 XAML 构建),使我能够确定特定构建是手动启动还是通过 CI 触发器启动?

我试图避免有两个构建定义,它们的区别仅在于当前作为属性传入的真/假值,例如与

/p:Manual=true

然后我在我的项目中使用它

$(Manual)

没有这个,看起来我需要两个定义 - 一个通过 CI 触发并在该属性中传递为 False,另一个手动传递 True。如果我有办法找出它,构建是签入的结果,那么我可以不用两个。

2016 年 4 月 21 日编辑

澄清一下,我知道 Build 定义上没有属性,我正在实际的 MSBuild 过程中(因为它正在运行)中寻找可以让我确定这一点的东西。

此时,即使获取计划构建的用户的登录 ID 也可以 - 如果它是触发构建,我的猜测是运行 TFS 的服务帐户,否则它是人。

【问题讨论】:

    标签: msbuild tfsbuild tfs-2015 azure-pipelines


    【解决方案1】:

    为了扩展 Dhruv 的答案,这里是相关的 Powershell 代码,用于记录 TFS 2015 的构建原因:

    # Retrieve build reason via REST API call and set as TFS 'Build.BuildReason' variable
    $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECT/_apis/build/builds/$env:BUILD_BUILDNUMBER`?api-version=2.0"
    $header = @{"Authorization" = "Bearer $env:SYSTEM_ACCESSTOKEN"}
    $response = Invoke-RestMethod $url -Headers $header
    $reason = $response.reason
    Write-Host "##vso[task.setvariable variable=Build.BuildReason;]$reason"
    

    在需要引用构建原因的任何步骤之前,在 Powershell 步骤中调用它。

    【讨论】:

      【解决方案2】:

      一种实现自动化的方法是在 msbuild 之前运行一个 powershell,您可以在其中使用 rest api 来获取当前执行构建的原因,如记录的here

      将rest api调用的返回值传递给一个变量,并在msbuild中访问该变量。

      还有很多其他方法可以自动执行此操作。这是一种方式。

      【讨论】:

        【解决方案3】:

        在触发构建时,构建代理会设置几个环境变量。您可以使用这些来确定构建是由用户还是系统身份请求的,以及为谁请求构建。来自文档:

         | Build.RequestedFor | BUILD_REQUESTEDFOR | This user the build was requested
         |                    |                    | for. In a CI build this will be
         |                    |                    | the user who performed the check-
         |                    |                    | in that triggered the build.
         ------------------------------------------------------------------------------
         | Build.QueuedBy     | BUILD_QUEUEDBY     | The identity of the user who 
         |                    |                    | queued the build. In the case 
         |                    |                    | of CI and Scheduled this will 
         |                    |                    | simply be the system identity
         |                    |                    | 
         |                    |                    | * For manual this will be the 
         |                    |                    |   identity of the user who queued
         |                    |                    |   the build
         |                    |                    | 
         |                    |                    | * For external triggers it will 
         |                    |                    |   be the identity used to 
         |                    |                    |   authenticate to the service
         ------------------------------------------------------------------------------
         | Build.SourceTfvcShelveset | BUILD_SOURCETFVCSHELVESET  | If you are 
         |                           |                            | running a gated 
         |                           |                            | build or a 
         |                           |                            | shelveset build 
         |                           |                            | Queue a build, 
         |                           |                            | this is set to 
         |                           |                            | the name of the 
         |                           |                            | shelveset you 
         |                           |                            | are building.
         ---------------------------------------------------------------------------
        

        can access these variables inside MsBuild,所以 Build.RequestedFor 在你的 msbuild 文件中变成 $(BUILD_REQUESTEDFOR)

        不确定构建是 CI、Gates、Nightly 还是批处理,但它确实区分了手动排队构建和触发构建,这似乎是您所追求的。

        【讨论】:

        • 有趣。我得试一试。在接下来的几天里,我会告诉你它对我的影响。谢谢!
        【解决方案4】:

        您可以将默认值设置为 False 的构建变量(例如称为 IsManual)添加到您的 vNext 构建中,这样每次构建由 CI 触发时,它都会采用默认值。

        当您手动对构建进行排队时,您必须更改变量值。

        【讨论】:

        • 这有点像我现在得到的,就像一个财产。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-01
        • 2013-03-16
        • 2015-04-10
        相关资源
        最近更新 更多