2016-07-26 2 views
1

를 실행중인 경우 내가 빌드가 완료된 경우 완벽하게 작동하고 간단한 C#을 클라이언트가 keepforever 설정할 수 없습니다 빌드 단계의 일부로서 실행하면 코드는 않습니다 어떤 오류도 발생시키지 않으며, UploadString의 JSON은 keepForever가 변경되었지만 유지되지 않는다고 제안합니다.빌드는 여전히

빌드가 완료된 후에야 안정적으로 작동합니다.

장황한 작업 환경을 작성하기 전에 내가 놓친 것이 분명한 것입니까? 빌드가 실행 중일 때 강제로 업데이트 할 수 있습니까?

+0

:

namespace RetainBuildIndefinitely { class Program { static void Main(string[] args) { var client = new WebApiCalls(); client.RetainBuildIndefinately(args[0]); } } } 

그리고 구현 :

RetainBuildIndefinitely.exe는 콘솔 응용 프로그램입니다 // 유래 .com/questions/38583551/send-a-patch-request-from-ac-sharp-client? –

+0

권한 오류가 발생했기 때문에 powershell 스크립트를 버렸지 만 C# 기능이 동일해야하므로 PS 스크립트가 작동했는지 확인할 수 없으므로 실제로는 그렇지 않습니다. –

답변

1

나는 주위에 작업 실행 :

가 먼저 콘솔 응용 프로그램을 작성을하고,이 빌드 단계에서 호출 될 것입니다 : 그것은에 RetainBuildIndefinitely.exe를 호출

private static void Main(string[] args) 
     { 
     // All this does is dispatch the call to a new process, so that the build can complete independently 
     // before attempting to update the keepForever field 
     // This is because you cannot update this field while the build is running 

     if (args.Length < 1) 
     { 
      throw new Exception("No Build Number Provided"); 
     } 

     var buildId = args[0]; 

     Console.WriteLine("Dispatching task to retain build: " + buildId); 

     var workingDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); 

     Console.WriteLine("Working Directory Set: " + workingDir); 

     if (workingDir == null) 
     { 
      throw new Exception("Working directory is null"); 
     } 

     var p = new Process 
     { 
      StartInfo = 
      { 
       WorkingDirectory = workingDir, 
       FileName = "RetainBuildIndefinitely.exe", 
       Arguments = buildId, 
       RedirectStandardOutput = false, 
       UseShellExecute = true, 
       CreateNoWindow = false 
      } 
     }; 
     p.Start(); 
     } 

지금 당신이 그것을 알 수 있습니다 자신의 프로세스. 이것은이 태스크를 디스패치하고 종료 한 다음 빌드를 완료 할 수 있도록하기위한 것입니다. 당신이 빌드에 http 동안 유사한 PowerShell 스크립트를 사용하는 경우 현상 무엇

namespace RetainBuildIndefinitely 
{ 
    public class WebApiCalls 
    { 
     public void RetainBuildIndefinately(string buildId) 
     { 
     // Max retry attempts 
     var retryCount = 30; 

     // The Tfs Url 
     var url = [YourURL eg: http://server:8080/tfs/TeamCollection/Project/_apis/build/builds/{buildId}?api-version=2.0"; 

     // Poll until the build is finished 
     // Since this call should be made in the last build step, there shouldn't be too much waiting for this to complete 

     using (var client = new WebClient {UseDefaultCredentials = true}) 
     { 
      client.Headers.Add("Content-Type", "application/json"); 
      client.Encoding = Encoding.UTF8; 

      var completed = false; 
      var attempt = 1; 

      while (completed == false) 
      { 
       if (attempt == retryCount) 
       { 
        // Couldn't complete? Clearly something went very wrong 
        // TODO: Sent out a notification email, but to who? 
        return; 
       } 

       var source = client.DownloadString(url); 
       dynamic data = JObject.Parse(source); 

       if (data.status == "completed") 
       { 
        // Completed, let's move on! 
        completed = true; 
       } 

       attempt = attempt + 1; 

       Thread.Sleep(2000); 
      } 
     }   ; 

     // Set the keepForever property 
     using (var client2 = new WebClient {UseDefaultCredentials = true}) 
     { 

      client2.Headers.Add("Content-Type", "application/json"); 
      client2.Encoding = Encoding.UTF8; 
      client2.UploadString(url, "PATCH", "{keepForever : true}"); 
     } 

     } 
    } 
} 
관련 문제