This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.TeamFoundation.Client; | |
using Microsoft.TeamFoundation.Build.Client; | |
using Microsoft.TeamFoundation.Build.Workflow; | |
using Microsoft.TeamFoundation.Build.Workflow.Activities; | |
public void CreateBuildDefinition(string solutionFile) | |
{ | |
//todo pull from config | |
string dropLocation = @"\\tfsserver\builddrop"; | |
string teamProject = solutionFile.Substring(2, solutionFile.Replace("$/", "").IndexOf('/')); | |
string solutionDirectory = solutionFile.Substring(0, solutionFile.LastIndexOf('/')); | |
string buildName = GetBuildNameForSolution(solutionFile); //Insert algorithm here to convert solution name (and folder) to a build name | |
//First we create a IBuildDefinition object for the team project and set a name and description for it: | |
var buildDefinition = _buildServer.CreateBuildDefinition(teamProject); | |
buildDefinition.Name = buildName; | |
buildDefinition.Description = "Auto Genned Build for " + solutionFile; | |
//Trigger - Next up, we set the trigger type. For this one, we set it to individual which corresponds to the Continuous Integration - Build each check-in trigger option | |
buildDefinition.ContinuousIntegrationType = ContinuousIntegrationType.Individual; | |
//Workspace - For the workspace mappings, we create two mappings here, where one is a cloak. Note the user of $(SourceDir) variable, which is expanded by Team Build into the sources directory when running the build. | |
buildDefinition.Workspace.AddMapping(solutionDirectory, "$(SourceDir)", WorkspaceMappingType.Map); | |
//Build Defaults - In the build defaults, we set the build controller and the drop location. To get a build controller, we can (for example) use the GetBuildController method to get an existing build controller by name: | |
buildDefinition.BuildController = _buildServer.QueryBuildControllers().First(); | |
buildDefinition.DefaultDropLocation = dropLocation; | |
//Get default template | |
var defaultTemplate = _buildServer.QueryProcessTemplates(teamProject.Replace("/", "").Replace("$", "")).Where(p => p.TemplateType == ProcessTemplateType.Default).First(); | |
buildDefinition.Process = defaultTemplate; | |
//Set process parameters | |
var process = WorkflowHelpers.DeserializeProcessParameters(buildDefinition.ProcessParameters); | |
//Set BuildSettings properties | |
BuildSettings settings = new BuildSettings(); | |
settings.ProjectsToBuild = new StringList(solutionFile); | |
settings.PlatformConfigurations = new PlatformConfigurationList(); | |
settings.PlatformConfigurations.Add(new PlatformConfiguration("Any CPU", "Release")); | |
process.Add(ProcessParameterMetadata.StandardParameterNames.BuildSettings, settings); | |
process[ProcessParameterMetadata.StandardParameterNames.CreateWorkItem] = false; | |
process[ProcessParameterMetadata.StandardParameterNames.AssociateChangesetsAndWorkItems] = true; | |
process[ProcessParameterMetadata.StandardParameterNames.BuildNumberFormat] = "$(BuildDefinitionName)_$(Date:yyyyMMdd)$(Rev:.r)"; | |
TestAssemblySpec testSpec = new TestAssemblySpec(); | |
testSpec.AssemblyFileSpec = "**\\*test*.dll"; | |
process.Add(ProcessParameterMetadata.StandardParameterNames.TestSpecs, new TestSpecList(testSpec)); | |
buildDefinition.ProcessParameters = WorkflowHelpers.SerializeProcessParameters(process); | |
//The other build process parameters of a build definition can be set using the same approach | |
//Retention Policy - This one is easy, we just clear the default settings and set our own: | |
buildDefinition.RetentionPolicyList.Clear(); | |
buildDefinition.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.Succeeded, 10, DeleteOptions.All); | |
buildDefinition.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.Failed, 10, DeleteOptions.All); | |
buildDefinition.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.Stopped, 1, DeleteOptions.All); | |
buildDefinition.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.PartiallySucceeded, 10, DeleteOptions.All); | |
//Save It! | |
buildDefinition.Save(); | |
_buildServer.QueueBuild(buildDefinition); | |
} | |