static async Task <String> CreateBuildAsync(HttpClient client, BuildDefinitionCreate data, String apiUrl) { var responseBody = String.Empty; var temp = JsonConvert.SerializeObject(data); var content = new StringContent( JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); try { using (HttpResponseMessage response = client.PostAsync(apiUrl, content).Result) { response.EnsureSuccessStatusCode(); responseBody = await response.Content.ReadAsStringAsync(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(responseBody); }
static async void CreateBuildDefinition() { Console.WriteLine("\n******************************\nCreate Build Definition\n******************************\n"); using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(user, pass) }) { using (var client = new HttpClient(handler)) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); uriSources = String.Format(uriSources, teamProject); string completeAddress = uriSources + "build/definitions" + apiVersion; BuildDefinitionCreate newBuild = new BuildDefinitionCreate(); newBuild.name = "Teste - " + Guid.NewGuid().ToString(); newBuild.type = "build"; newBuild.quality = "definition"; newBuild.queue = new Queue { id = 2 }; Variables variaveis = new Variables(); variaveis.BuildConfiguration = new BuildConfiguration { value = "Debug", allowOverride = true }; variaveis.BuildPlatform = new BuildPlatform { value = "Any CPU", allowOverride = true }; newBuild.variables = variaveis; List <Build> lstBuild = new List <Build>(); lstBuild.Add(new Build { enabled = true, continueOnError = false, alwaysRun = false, displayName = "NuGet restore", task = new CreateBuildDefinitionAPI.Create.Task { id = "333b11bd-d341-40d9-afcf-b32d5ce6f23b", versionSpec = "*" }, inputs = new Inputs { solution = "**\\*.sln" } }); lstBuild.Add(new Build { enabled = true, continueOnError = false, alwaysRun = false, displayName = "Build solution", task = new CreateBuildDefinitionAPI.Create.Task { id = "71a9a2d3-a98a-4caa-96ab-affca411ecda", versionSpec = "*" }, inputs = new Inputs { solution = "**\\*.sln", platform = "$(BuildPlatform)", configuration = "$(BuildConfiguration)", vsVersion = "14.0", msbuildArchitecture = "x86", logProjectEvents = "true" } }); lstBuild.Add(new Build { enabled = true, continueOnError = false, alwaysRun = false, displayName = "Test Assemblies", task = new CreateBuildDefinitionAPI.Create.Task { id = "ef087383-ee5e-42c7-9a53-ab56c98420f9", versionSpec = "*" }, inputs = new Inputs { testAssembly = "**\\$(BuildConfiguration)\\*test*.dll", platform = "$(BuildPlatform)", configuration = "$(BuildConfiguration)", codeCoverageEnabled = "true", vsTestVersion = "14.0", publishRunAttachments = "true" } }); lstBuild.Add(new Build { enabled = true, continueOnError = false, alwaysRun = true, displayName = "Copy Files", task = new CreateBuildDefinitionAPI.Create.Task { id = "5bfb729a-a7c8-4a78-a7c3-8d717bb7c13c", versionSpec = "*" }, inputs = new Inputs { SourceFolder = "$(build.sourcesdirectory)", Contents = "**\\bin\\$(BuildConfiguration)\\**", TargetFolder = "$(build.artifactstagingdirectory)", CleanTargetFolder = "false", OverWrite = "false" } }); lstBuild.Add(new Build { enabled = true, continueOnError = false, alwaysRun = true, displayName = "Publish Artifact", task = new CreateBuildDefinitionAPI.Create.Task { id = "2ff763a7-ce83-4e1f-bc89-0ae63477cebe", versionSpec = "*" }, inputs = new Inputs { PathtoPublish = "$(build.artifactstagingdirectory)", ArtifactName = "drop", ArtifactType = "Container" } }); newBuild.build = lstBuild; newBuild.buildNumberFormat = "$(date:yyyyMMdd)$(rev:.r)"; newBuild.jobAuthorizationScope = "projectCollection"; newBuild.jobTimeoutInMinutes = 60; Repository rep = new Repository(); rep.id = "$/"; rep.type = "TfsVersionControl"; rep.name = teamProject; rep.defaultBranch = "$/" + teamProject; rep.rootFolder = "$/" + teamProject; rep.clean = "undefined"; newBuild.repository = rep; var responseBody = await CreateBuildAsync(client, newBuild, completeAddress); Console.WriteLine("Build criado"); } } }