/// <summary> /// Handles the Deploy button. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void OnDeploy(object sender, EventArgs e) { var commit = new OnCommitArgs() { Hash = this.GetLatestCommitHash(), Message = "Re-deploy" }; NotificationWriter.Clear(); this.Deploy(commit); }
/// <summary> /// Deploys dev websites on a successful commit /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="commit"> /// The commit. /// </param> private void DeploySuccessfulCommit(object sender, OnCommitArgs commit) { if (commit.Success) { commit.Hash = this.GetLatestCommitHash(); NotificationWriter.Clear(); NotificationWriter.Write("Commit " + commit.Hash + " successful"); this.Deploy(commit); } // Unsubscribe from successful commit event BlinkboxSccHooks.OnCommit -= this.DeploySuccessfulCommit; }
/// <summary> /// Builds and deploys. /// </summary> /// <param name="commit"> /// The commit. Supplied if called after a successful commit, otherwise a new instance is created. /// </param> /// <returns> /// true if the deploy was successful. /// </returns> private bool Deploy(OnCommitArgs commit) { NotificationWriter.Write("Begin build and deploy to " + commit.Hash); try { // Look for a deploy project var buildProjectFileName = this.GetSolutionDirectory() + "\\" + BlinkboxSccOptions.Current.PostCommitDeployProjectName; if (!File.Exists(buildProjectFileName)) { MessageBox.Show("build project not found", "Deploy abandoned", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } NotificationWriter.Write("Deploy project found at " + buildProjectFileName); // Initisalise our own project collection which can be cleaned up after the build. This is to prevent caching of the project. using (var projectCollection = new ProjectCollection(Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.ToolsetLocations)) { var commitComment = Regex.Replace(commit.Message, @"\r|\n|\t", string.Empty); commitComment = HttpUtility.UrlEncode(commitComment.Substring(0, commitComment.Length > 80 ? 80 : commitComment.Length)); // Global properties need to be set before the projects are instantiated. var globalProperties = new Dictionary <string, string> { { BlinkboxSccOptions.Current.CommitGuidPropertyName, commit.Hash }, { BlinkboxSccOptions.Current.CommitCommentPropertyName, commitComment } }; var msbuildProject = new ProjectInstance(buildProjectFileName, globalProperties, "4.0", projectCollection); // Build it WriteToStatusBar("Building " + Path.GetFileNameWithoutExtension(msbuildProject.FullPath)); var buildRequest = new BuildRequestData(msbuildProject, new string[] { }); var buildParams = new BuildParameters(projectCollection); buildParams.Loggers = new List <ILogger>() { new BuildNotificationLogger() { Verbosity = LoggerVerbosity.Minimal } }; var result = BuildManager.DefaultBuildManager.Build(buildParams, buildRequest); if (result.OverallResult == BuildResultCode.Failure) { string message = result.Exception == null ? "An error occurred during build; please see the pending changes window for details." : result.Exception.Message; MessageBox.Show(message, "Build failed", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } // Launch urls in browser var launchUrls = msbuildProject.Items.Where(pii => pii.ItemType == BlinkboxSccOptions.Current.UrlToLaunchPropertyName); foreach (var launchItem in launchUrls) { this.LaunchBrowser(launchItem.EvaluatedInclude); } // Clean up project to prevent caching. projectCollection.UnloadAllProjects(); projectCollection.UnregisterAllLoggers(); } return(true); } catch (Exception exc) { MessageBox.Show(exc.Message, "Build failed", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } }