【问题标题】:Error while cloning build definitions using REST API使用 REST API 克隆构建定义时出错
【发布时间】:2019-10-01 02:07:05
【问题描述】:

我正在尝试使用 powershell REST API 方法克隆 VSTS 中的所有构建定义。但是,我面临以下错误。分享可能有用的代码和错误。

代码:

Clear-Host
$buildToCloneName = $buildWeWant
$newBuildName = $buildWeWant-Clone

$user = "xxxxxxx"
$accessToken="xxxxxxxx"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$accessToken)))
$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = "https://xxxxx.visualstudio.com/"
$env:SYSTEM_TEAMPROJECTID = "xxxxxxx"

"Getting all bulid definitions"
$allSuitesBuildUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions?api-version=2.0"
$allSuitedBuilds = Invoke-RestMethod -Uri $allSuitesBuildUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}


foreach($buildDetails in $allSuitedBuilds.value){

    $buildWeWant = $buildDetails
    $buildId = $buildWeWant."id"


    [int]$buildIdTest = $null
    if(![int]::TryParse($buildId, [ref]$buildIdTest))
        {
            throw [Exception] "ERROR: NO BUILD ID FOUND"
        }

        "Getting the exact definition for the build"
        # You can see this in the browser using xxxxxxxxxx
        $thisBuildDefUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions/" + $buildId + "?api-version=2.0"
        $thisBuildDefUrl
        $thisBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

        ## Create a name for the clone by prefixing "_Clone" to the build name
        "Assigning a new name"
        $thisBuildDef.Name = $buildWeWant."id"."_Clone" 


        "Creating a clone build with name $newBuildName"
        $defAsJson = $thisBuildDef | ConvertTo-Json -Depth 100
        $newBuildDefUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions?api-version=2.0"
        $newBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -Body $defAsJson -ContentType "application/json" -ErrorAction Stop


        $newBuildDefAsJson = $newBuildDef | ConvertTo-Json -Depth 100
        $newBuildDefAsJson

        "New Build Created"
        $newBuildDef.Name

}

错误:

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value 不能为空。\r\n参数名称: 定义.Name","typeName":"System.ArgumentNullException, mscorlib, 版本=14.0.0.0,文化=中性, PublicKeyToken=b77a5c561934e089","typeKey":"ArgumentNullException","errorCode":0,"eventId":0} 在行:42 字符:24 + ... wBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers @{Author ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], 网络异常 + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

【问题讨论】:

    标签: powershell


    【解决方案1】:

    除了内联定义之外,您可以单独准备标题

    Clear-Host
    $buildToCloneName = $buildWeWant
    $newBuildName = $buildWeWant-Clone
    
    $user = "xxxxxxx"
    $accessToken = "xxxxxxxx"
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $accessToken)))
    $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = "https://xxxxx.visualstudio.com/"
    $env:SYSTEM_TEAMPROJECTID = "xxxxxxx"
    
    "Getting all bulid definitions"
    $allSuitesBuildUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions?api-version=2.0"
    $allSuitedBuilds = Invoke-RestMethod -Uri $allSuitesBuildUrl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo) }
    
    
    foreach ($buildDetails in $allSuitedBuilds.value) {
    
        $buildWeWant = $buildDetails
        $buildId = $buildWeWant."id"
    
    
        [int]$buildIdTest = $null
        if (![int]::TryParse($buildId, [ref]$buildIdTest)) {
            throw [Exception] "ERROR: NO BUILD ID FOUND"
        }
    
        "Getting the exact definition for the build"
        # You can see this in the browser using xxxxxxxxxx
        $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
        $headers.Add("Authorization", "Basic $base64AuthInfo")
        $headers.Add("Content-Type", "application/json")
    
        $thisBuildDefUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions/" + $buildId + "?api-version=2.0"
        $thisBuildDefUrl
        $thisBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers $headers
    
        ## Create a name for the clone by prefixing "_Clone" to the build name
        "Assigning a new name"
        $thisBuildDef.Name = $buildWeWant."id"."_Clone"
    
    
        "Creating a clone build with name $newBuildName"
        $defAsJson = $thisBuildDef | ConvertTo-Json -Depth 100
    
        $newBuildDefUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/build/definitions?api-version=2.0"
        $newBuildDef = Invoke-RestMethod -Uri $thisBuildDefUrl -Headers $headers -Method Post -Body $defAsJson -ContentType "application/json" -ErrorAction Stop
    
    
        $newBuildDefAsJson = $newBuildDef | ConvertTo-Json -Depth 100
        $newBuildDefAsJson
    
        "New Build Created"
        $newBuildDef.Name
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-09
      • 2020-02-29
      • 2014-05-20
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多