private async Task SetBuildConfigurationIdFromName() { this.Logger.LogDebug("Attempting to resolve build configuration ID from project and name..."); using (var client = new TeamCityWebClient(this.ConnectionInfo)) { this.Logger.LogDebug("Downloading build types..."); string result = await client.DownloadStringTaskAsync("app/rest/buildTypes").ConfigureAwait(false); var doc = XDocument.Parse(result); var buildConfigurations = from e in doc.Element("buildTypes").Elements("buildType") let buildType = new BuildType(e) where string.Equals(buildType.BuildConfigurationName, this.BuildConfigurationName, StringComparison.OrdinalIgnoreCase) let match = new { BuildType = buildType, Index = Array.FindIndex(buildType.ProjectNameParts, p => string.Equals(p, this.ProjectName, StringComparison.OrdinalIgnoreCase)) } where match.Index > -1 || string.Equals(match.BuildType.ProjectName, this.ProjectName, StringComparison.OrdinalIgnoreCase) orderby match.Index select match.BuildType.BuildConfigurationId; this.BuildConfigurationId = buildConfigurations.FirstOrDefault(); if (this.BuildConfigurationId == null) { throw new ExecutionFailureException($"Build configuration ID could not be found for project \"{this.ProjectName}\" and build configuration \"{this.BuildConfigurationName}\"."); } this.Logger.LogDebug("Build configuration ID resolved to: " + this.BuildConfigurationId); } }
public async Task QueueBuildAsync(CancellationToken cancellationToken, bool logProgressToExecutionLog) { this.Logger.LogInformation($"Queueing build in TeamCity..."); if (this.BuildConfigurationName != null && this.ProjectName != null && this.BuildConfigurationId == null) { await SetBuildConfigurationIdFromName().ConfigureAwait(false); } using (var client = new TeamCityWebClient(this.ConnectionInfo)) { this.Logger.LogDebug("Triggering build configuration {0}...", this.BuildConfigurationId); if (this.BranchName != null) { this.Logger.LogDebug("Using branch: " + this.BranchName); } var xdoc = new XDocument( new XElement("build", new XAttribute("branchName", this.BranchName ?? ""), new XElement("buildType", new XAttribute("id", this.BuildConfigurationId)) ) ); string response = await client.UploadStringTaskAsync("app/rest/buildQueue", xdoc.ToString(SaveOptions.DisableFormatting)).ConfigureAwait(false); var status = new TeamCityBuildStatus(response); this.Logger.LogInformation("Build of {0} was triggered successfully.", this.BuildConfigurationId); if (!this.WaitForCompletion) { return; } this.Logger.LogInformation("Waiting for build to complete..."); while (!status.Finished) { string getBuildStatusResponse = await client.DownloadStringTaskAsync(status.Href).ConfigureAwait(false); status = new TeamCityBuildStatus(getBuildStatusResponse); this.progressPercent = status.PercentageComplete; this.progressMessage = $"Building {status.ProjectName} Build #{status.Number} ({status.PercentageComplete}% Complete)"; if (logProgressToExecutionLog) { this.Logger.LogInformation(this.progressMessage); } await Task.Delay(2000, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); } if (status.Success) { this.Logger.LogInformation($"{status.ProjectName} build #{status.Number} successful. TeamCity reports: {status.StatusText}"); } else { this.Logger.LogError($"{status.ProjectName} build #{status.Number} failed or encountered an error. TeamCity reports: {status.StatusText}"); } } }