public static void BuildDefinition(string collection, string teamProject, string buildController, string buildName, string buildDescription, string user, string password) { string teamProjectPath = "$/" + teamProject; // Get a reference to our Team Foundation Server. TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(collection), new System.Net.NetworkCredential(user, password)); tpc.EnsureAuthenticated(); VersionControlServer versionControl = tpc.GetService <VersionControlServer>(); IBuildServer buildServer = tpc.GetService <IBuildServer>(); //Create build definition and give it a name and desription IBuildDefinition buildDef = buildServer.CreateBuildDefinition(teamProject); buildDef.Name = buildName; buildDef.Description = buildDescription; buildDef.ContinuousIntegrationType = ContinuousIntegrationType.Individual; //CI //Controller and default build process template buildDef.BuildController = buildServer.GetBuildController(buildController); var defaultTemplate = buildServer.QueryProcessTemplates(teamProject).First(p => p.TemplateType == ProcessTemplateType.Default); buildDef.Process = defaultTemplate; buildDef.Workspace.AddMapping(teamProjectPath, "$(SourceDir)", WorkspaceMappingType.Map); buildDef.Workspace.AddMapping(teamProjectPath + "/Drops", "$(SourceDir)", WorkspaceMappingType.Cloak); //What to build string pattern = "$/*.sln"; var lists = versionControl.GetItems(pattern, VersionSpec.Latest, RecursionType.Full, DeletedState.NonDeleted, ItemType.File); var processParams = new Dictionary <string, string[]>(); if (lists.Items.Any()) { var list = lists.Items .Select(i => i.ServerItem) .ToList(); processParams.Add("ProjectsToBuild", list.ToArray()); } processParams.Add("ConfigurationsToBuild", new[] { "Any CPU|Debug" }); buildDef.ProcessParameters = SerializeParams(processParams); buildDef.RetentionPolicyList.Clear(); buildDef.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.Succeeded, 10, DeleteOptions.All); buildDef.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.Failed, 10, DeleteOptions.All); buildDef.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.Stopped, 1, DeleteOptions.All); buildDef.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.PartiallySucceeded, 10, DeleteOptions.All); //Lets save it buildDef.Save(); }
static void CreateBuildDefinition(IBuildServer buildServer) { IBuildController defaultBuildController = buildServer.QueryBuildServiceHosts("*") .Where(bsh => bsh.Controller != null).Select(bsh => bsh.Controller).First(); IBuildDefinition buildDefinition = buildServer.CreateBuildDefinition("Contoso"); buildDefinition.Name = "HelloWorld"; buildDefinition.ContinuousIntegrationType = ContinuousIntegrationType.Individual; buildDefinition.BuildController = defaultBuildController; buildDefinition.DefaultDropLocation = @"\\CONTOSO\Projects\HelloWorld\drops"; IRetentionPolicy retentionPolicy = buildDefinition.RetentionPolicyList.Where( rp => rp.BuildReason == BuildReason.Triggered || rp.BuildStatus == BuildStatus.Succeeded) .First(); retentionPolicy.NumberToKeep = 2; retentionPolicy.DeleteOptions = DeleteOptions.All; buildDefinition.Save(); }
public void BuildCopy() { if (string.IsNullOrEmpty(_configuration.Source) || string.IsNullOrEmpty(_configuration.ProjectSource) || string.IsNullOrEmpty(_configuration.Destination) || string.IsNullOrEmpty(_configuration.ProjectDestination)) { WriteHelp(); return; } IBuildServer buildServerSource = GetTfsBuildServer(_configuration.Source); IBuildServer buildServerDestination = GetTfsBuildServer(_configuration.Destination); IBuildDefinition[] sourceBuildDefinitions = buildServerSource.QueryBuildDefinitions(_configuration.ProjectSource); foreach (var sourceBuildDef in sourceBuildDefinitions) { IBuildDefinition targetBuildDef = buildServerDestination.CreateBuildDefinition(_configuration.ProjectDestination); Copy(sourceBuildDef, targetBuildDef); targetBuildDef.Save(); } }
public void BranchBuildDefinition(BuildDefnDetails buildDetail) { IBuildDefinition defn = buildServer.GetBuildDefinition(buildDetail.TeamProjectName, buildDetail.Name); if (defn.GetDefaultSourceProvider().Name != "TFVC") { MessageBox.Show("Branch build definition is only supported for Team Foundation Version Controlled builds", "Microsoft Visual Studio", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } List <WorkspaceBranchMapping> originalMappings = (from mapping in defn.Workspace.Mappings select new WorkspaceBranchMapping(mapping.ServerItem, mapping.ServerItem)).ToList(); VersionControlServer versionControlServer = (VersionControlServer)tfsConnection.ProjectCollection.GetService(typeof(VersionControlServer)); WorkSpaceMappingDialog dlg = new WorkSpaceMappingDialog(originalMappings, versionControlServer); dlg.Text = string.Format("Branch Build - {0}", defn.Name); if (dlg.ShowDialog() == DialogResult.OK) { IBuildDefinition branchedDefn = buildServer.CreateBuildDefinition(buildDetail.TeamProjectName); branchedDefn.CopyFrom(defn); branchedDefn.Name = string.Format("Branch of {0}", buildDetail.Name); // Update new build workspace foreach (IWorkspaceMapping mapping in branchedDefn.Workspace.Mappings) { foreach (WorkspaceBranchMapping branchedMapping in dlg.BranchedMappings) { string pattern = branchedMapping.SourceServerPath.Replace("$", @"\$"); mapping.ServerItem = Regex.Replace(mapping.ServerItem, pattern, branchedMapping.TargetServerPath, RegexOptions.IgnoreCase); } } // Update process parameters foreach (WorkspaceBranchMapping branchedMapping in dlg.BranchedMappings) { string pattern = branchedMapping.SourceServerPath.Replace("$", @"\$"); branchedDefn.ProcessParameters = Utilities.ReplaceTFSServerPaths(branchedDefn.ProcessParameters, branchedMapping.SourceServerPath, branchedMapping.TargetServerPath); //Regex.Replace(branchedDefn.ProcessParameters, pattern, branchedMapping.TargetServerPath, RegexOptions.IgnoreCase); //branchedDefn.Process.ServerPath = Regex.Replace(branchedDefn.ProcessParameters, pattern, branchedMapping.TargetServerPath, RegexOptions.IgnoreCase); } try { branchedDefn.Save(); } catch (BuildDefinitionAlreadyExistsException ex) { MessageBox.Show(string.Format("Team Foundation Error\n\r\n\r{0}", ex.Message), "Microsoft Visual Studio", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (vsTfsBuild != null) { vsTfsBuild.DefinitionManager.OpenDefinition(branchedDefn.Uri); } teamExplorer.CurrentPage.Refresh(); } }