TFS 2015使用REST Api进行队列新构建 - 设置需求

时间:2017-09-06 09:26:56

标签: tfs

我试图使用TFS 2015.3 REST API对新构建进行排队,我已经阅读了很多文章,但无法使其工作。

我在PowerShell中执行此操作,标准队列新建构调用在仅传递定义ID时起作用,但除了id之外传递任何其他内容似乎不起作用。

我的代码:

$buildDef = Invoke-RestMethod -Method Get -UseDefaultCredentials -Uri "$($tfsRoot)/_apis/build/definitions?api-version=2.0&name=$buildDefintionName"        

        $detailedResults = Invoke-RestMethod -Uri $buildDef.Value[0].Url -Method Get -ContentType "application/json" -UseDefaultCredentials

        if ($buildDef.Value[0].Id)
        {
            $agentDemandString = "Agent.Name -equals $agent"
            $demands = $detailedResults.Demands

            $json = "definition: { id:$($buildDef.Value[0].Id) }, demands: $demands" 
            $bodyjson = $json | ConvertTo-Json
            Write-Host "Queuing build $buildDefintionName on agent $agent with parameters $json"
            $build = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri "$($tfsRoot)/_apis/build/builds?api-version=2.0" -Body $bodyjson
        }

我已经尝试了许多不同的传递要求的变体,但看起来它甚至没有达到这一点,因为它抱怨“构建”参数。

  

Invoke-RestMethod:{“$ id”:“1”,“innerException”:null,“message”:“值不能为空。\ r \ nParameter name:build”,“typeName”:“System.ArgumentNullException ,mscorlib,版本= 4.0.0.0,文化=中性

如果我正确,构建参数包含要执行的构建步骤。这让我觉得排队的构建正在丢弃所有现有的配置,并试图只依赖于JsonBody中传递的内容,这不是我想要的。

我应该通过什么以及如何为新的构建排队,但需要更新/额外的需求。

4 个答案:

答案 0 :(得分:1)

我最终得到了一些帮助。请求属性被接受。 由于带有Json转换的powerShell代码,它看起来不起作用。如果我在下面使用并且不将它转换为Json,它就可以了!

Function queuebuild{ 

      $uri="$($tfsRoot)/_apis/build/builds?api-version=2.0"

      $body='{
                 "definition": {
                 "id": 1061
                 },
                    "sourceBranch": "$/Project/Branch",
                    "demands":["Demand1", "Agent.Name -equals Computer2-E-A2"]
                 }';

        $result=Invoke-RestMethod -Uri $uri -Method Post -ContentType "application/json" -UseDefaultCredentials -Body $body

}

答案 1 :(得分:0)

尝试设置深度:

  

$ bodyjson = $ json | ConvertTo-Json -Depth 3

答案 2 :(得分:0)

$json = "definition: { id:$($buildDef.Value[0].Id) }, demands: $demands"不会是有效的JSON - 例如,它不会被包裹在花括号中。

我建议创建一个能够正确转换为有效JSON的关联数组。 documentation中提供的示例JSON是:

{
  "definition": {
    "id": 25
  },
  "sourceBranch": "refs/heads/master",
  "parameters": "{\"system.debug\":\"true\",\"BuildConfiguration\":\"debug\",\"BuildPlatform\":\"x64\"}"
}

所以这会生成一个合适的JSON对象:

$body = @{
    definition = @{ id=25 }
    sourceBranch = 'refs/heads/master'
    parameters = '{\"system.debug\":\"true\",\"BuildConfiguration\":\"debug\",\"BuildPlatform\":\"x64\"}'
} 

$body | convertto-json

或者如果你想要更加花哨并消除内部JSON-as-a-string位:

$body = @{
    definition = @{ id=25 }
    sourceBranch = 'refs/heads/master'
    parameters = (@{'system.debug' = $true; BuildConfiguration='debug'; BuildPlatform='x64'}) | convertto-json -Compress
} 

$body | convertto-json

答案 3 :(得分:0)

根据我的测试,我们无法直接使用Queue build REST Api设置需求。

构建仍将使用在定义中设置的代理,即使我们在构建队列时指定了其他具有“需求”设置的代理。您可以使用REST API查看此屏幕截图,以供参考。

使用REST API获取构建例如:

Dim days
Dim inputFolderList, ObjFolder, Files, objFileAge

If Not WScript.Arguments.Count = 2 Then
    Wscript.Echo "Invalid number of arguments. Arg1: Daily or Weekly. Arg2: Remove all files older then this"
    WScript.Quit(-1)
End If

days = WScript.Arguments.Item(1)

inputFileList = "D:\FileGrep2.txt"
Set Fso = CreateObject("Scripting.FileSystemObject")
Set objTextFile = fso.OpenTextFile(inputFileList, 1)

Do Until objTextFile.AtEndOfStream
    sFolderName = objTextFile.ReadLine
    getfoldernames(sFolderName)
Loop

Function getfoldernames(sFolderName)
    Set ObjFolder = fso.GetFolder(sFolderName)
    Set Files = ObjFolder.Files

    For Each Check In Files
        objFileAge = DateDiff("n", Check.DateLastModified, Now)
        If objFileAge > 90 Then
            WScript.Echo Now & "the following will be deleted " & Check.Path
            Check.Delete
        End If
    Next
End Function

您可以看到,“需求”未包含在响应中。它仅出现在构建定义响应中。

实际上,“需求”是在构建定义中设置的,它仅针对构建定义。使用REST API对构建进行排队时,它只会触发构建定义。因此,如果要使用REST API使用特定代理触发构建,则需要首先更新定义(设置需求),然后触发构建定义。

要更新定义,请使用REST API:请参阅Update a build definition

GET http://SERVER:8080/tfs/CollectionLC/6debd6ea-fa97-4ea2-b0c0-3cbbc4afa802/_apis/build/Builds/1071/

因此,您可以编写脚本来更新构建定义,然后使用构建定义ID触发构建。

enter image description here

相关问题