2017-11-09 13 views
0

ビルド定義IDのみを指定すると、以下のコードが動作しています。パラメータを指定すると、このエラーが発生します。お知らせ下さい。 トリガーvNextビルド定義614が失敗しました! - System.Net.WebException:リモートサーバーがエラーを返しました:(400)不正な要求。 \ TestConsoleApp \のProgram.cs:CでTestConsoleApp.Program.TriggerVNextBuild(文字列WorkItemId、列BuildDefinitionId)でSystem.Net.HttpWebRequest.GetResponse() におけるライン53変数変数を持つvNextビルドを起動できません

 static private void TriggerVNextBuild(string WorkItemId, string BuildDefinitionId) 
    { 
     try 
     { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://testtfs2017:8080/tfs/mycollection/myteamproject/_apis/build/builds?api-version=2.0"); 
      request.Credentials = CredentialCache.DefaultNetworkCredentials; 
      request.Method = "Post"; 
      request.ContentType = "application/json"; 
      Stream stream = request.GetRequestStream(); 
      //string json = "{\"definition\":{\"id\":" + BuildDefinitionId + "}}"; WORKING 

      //NOT WORKING 
      string json = "{\"definition\":{\" id\": " + BuildDefinitionId + "}" + "," + 
           "\"parameters\": {\" ReleaseWorkItemID\": " + WorkItemId + "}}"; 

      byte[] buffer = Encoding.UTF8.GetBytes(json); 
      stream.Write(buffer, 0, buffer.Length); 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

      Console.Write(response.StatusCode); 
      using (var streamReader = new StreamReader(response.GetResponseStream())) 
      { 
       var result = streamReader.ReadToEnd(); 
      } 
     } 
     catch (Exception e) 
     { 
      System.IO.File.WriteAllText(@"C:\TFSLogs\vNextAutomation.txt", "Trigger vNext Build Definition " + BuildDefinitionId + " is failed! -- " + e.ToString()); 
     } 
    } 

よろしく、

+0

は、ビルド定義のためのavaillableパラメータはありますか?定義に使用できない、または定義されていないパラメータは指定できません。 –

答えて

0

定義に使用できない、または定義されていないパラメータは指定できません。

私はこの問題をテストして再現できますが、これはC#のjson構文の問題です。

ただ、この方法を試してください。

string json = "{\"definition\":{\"id\":" + BuildDefinitionId + "},\"parameters\":\"{\\\"system.debug\\\":\\\"true\\\"}\"}"; 

以外にも、あなたもvNextは、指定されたパラメータでビルド構築トリガするためにPowerShellを使用することができます。あなたの参照用のサンプル以下

Param(
    [string]$collectionurl = "http://server:8080/tfs/DefaultCollection/", 
    [string]$projectName = "ProjectName", 
    [string]$keepForever = "true", 
    [string]$BuildDefinitionId = "34", 
    [string]$user = "username", 
    [string]$token = "token" 
) 

# Base64-encodes the Personal Access Token (PAT) appropriately 
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token))) 

function CreateJsonBody 
{ 

    $value = @" 
    { 
    "definition": { 
    "id": $BuildDefinitionId 
    }, 

    "parameters": "{\"system.debug\":\"true\",\"BuildConfiguration\":\"debug\",\"BuildPlatform\":\"x64\"}" 
} 
"@ 

return $value 
} 

$json = CreateJsonBody 

$uri = "$($collectionurl)/$($projectName)/_apis/build/builds?api-version=2.0" 
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} 
関連する問題