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(); }
private IBuildDefinition CreateBuildDefinition(IBuildServer buildServer, string buildController, string project, string dropLocation, string buildDefinition) { var controller = GetBuildController(buildServer, buildController); // Get the Upgrade template to use as the process template var processTemplate = buildServer.QueryProcessTemplates(project, new[] { ProcessTemplateType.Upgrade })[0]; var definition = buildServer.CreateBuildDefinition(project); definition.Name = buildDefinition; definition.ContinuousIntegrationType = ContinuousIntegrationType.None; definition.BuildController = controller; definition.DefaultDropLocation = dropLocation; definition.Description = "Fake build definition used to create fake builds."; definition.QueueStatus = DefinitionQueueStatus.Enabled; definition.Workspace.AddMapping("$/", "c:\\fake", WorkspaceMappingType.Map); definition.Process = processTemplate; definition.Save(); return definition; }
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(); }
private IBuildDefinition CreateBuildDefinition(IBuildServer buildServer, string buildController, string project, string dropLocation, string buildDefinition) { var controller = GetBuildController(buildServer, buildController); // Get the Upgrade template to use as the process template var processTemplate = buildServer.QueryProcessTemplates(project, new[] { ProcessTemplateType.Upgrade })[0]; var definition = buildServer.CreateBuildDefinition(project); definition.Name = buildDefinition; definition.ContinuousIntegrationType = ContinuousIntegrationType.None; definition.BuildController = controller; definition.DefaultDropLocation = dropLocation; definition.Description = "Fake build definition used to create fake builds."; definition.QueueStatus = DefinitionQueueStatus.Enabled; definition.Workspace.AddMapping("$/", "c:\\fake", WorkspaceMappingType.Map); definition.Process = processTemplate; definition.Save(); return(definition); }
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(); } }