TFS 2012 Build Definition/Template - How to set Build Quality Automatically? - tfs

I thought this would be pretty straight forward, but it looks like the template has to be modified. Anyone have any idea what to do to add this piece to the default template?
I know how to edit the templates, I just don't know what I need to do to make this work.

This case already provided a solution for your requirement:
Copy the DefaultTemplate.xaml to DefaultTemplateCustom.xaml and open it
Click the Collapse All link in the top right corner
Open the toolbox and locate the Assign activity. It is located in the Primitives section
Drag the Assign activity to the end of the workflow, after the "Run MSBuild" activity
Go to the properties window. Set the "To" parameter to BuildDetail.Quality. Set "Value" to "xxx (Build Quality)"
Locate the InvokeMethod in the toolbox and drag it and drop it after the Assign activity
Set the "Target Object" to BuildDetail. Set "MethodName" to Save
Save the build definition xaml file and check it in, then build with the customized definition.

You can also consider to create a powershell script to set the build quality via TFS API and then invoke this powershell script in your build template. Refer to this link for details: PowerShell and TFS: The Basics and Beyond.
Here is a simple script to set the build quality:
$builduri = "vstfs:///Build/Build/1";
$collectionurl = "http://xxxx:8080/tfs/xxx/";
$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($collectionurl);
$buildservice = $tfs.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer]);
$build = $buildservice.GetBuild($builduri);
$build.Quality = "Released";
$build.Save()

Related

How can I execute tests on specific browser through Jenkins using choice parameters?

I want to perform testing on specific browser through Jenkins i.e. if I select firefox or chrome browser from dropdown and click on build to process further. How can I make it working. We are using in built automation framework.What if I maintain different testng.xml files as respective to browser. But again how can achieve it using same approach.
Let me know end to end best solution for it.
Thanks.
You can always use build with parameters and select the browser you want from a select element.
Then, you set the browser you want it to be executed on to be a parameter inside your pipeline file instead of changing all the parameters like you are doing right now(I guess?) or using multiple builds.
If you could expand on what you currently have the way you are using it right now, I'll be glad to help.
You can set a parameter in jenkins where you need to develop your code to read that parameter.
This parameter is called Choice parameter under "MAVEN INFO PLUGIN CONFIGURATION" in general settings of project in jenkins.
Check on "This project is parameterized"
add name as "Browser" and choices as your browser names you want to specify like
IE
EDGE
CHROME
Above process will let you trigger tests using choice parameters but one more change needs to be done is at "Goals and options" under BUILD section.
Here please use (test -DBrowser="$Browser") which lets you pass choice parameter value selected with Browser as key.
Try below code in your utilities to read browser type and so use driver type accordingly.
String DriverType = System.getProperty("Browser");
if(DriverType.contains("IE")) {
DriverType = "IE";
} else {
DriverType ="Edge";
} return DriverType;

TFS Build Template Customization ( MSBuildArguments)

I have to customize TFS DefaultTemplate.xaml with MSBuildArguments Parameter.
Once we try to create new XAML build definition & select default template, once it gets loaded then I want to set code MSBuildArguments in Advance setting & parameter should be /p:DebugType=pdbOnly;Configuration=Release by default.
As of now this Arguments we have to put manually whenever we create a new build definition. I want to make it customize this template.
screen shot:
Specify MSBuild Arguments when create a build definition or queue a build is more convenient.
However if you want to bind the arguments with the xaml build template, then you need to custom the template.
In your scenario, you can simply replace the value of CommandLineArguments with below strings: (Note that the arguments you added here will be hidden, that means you can not see them in build definition, they are set to be the default arguments)
String.Format("/p:SkipInvalidConfigurations=true {0}", "/p:DebugType=pdbOnly;Configuration=Release").
But same time the MSBuildArguments you specified in build definition will not be available any more.
You can reference below articles to custom the build template to replace the MSBuildArguments:
Pass Relative Path Arguments to MSBuild in TFS2010 Team Build
Properly incorporate MsBuild arguments into your build process
template

Include branch name in post build event on Team Build

I would like to perform the following steps in the TFS build process:
do post build event that will copy some files from my compiled projects to another predefined directory, I'd like that directory path to include the branch name.
I'd like to be able to refer to the branch name inside my xaml workflow template as well.
The first one is rather simple. When you're using the new TFS 2013 build server and process template, you can simply add a post-build powershell script in the Build Definition Configuration, check in the script and run it during the build.
The second one is dependent on whether you're using TFVC or Git, in the first case, use the VersionControlServer class to query the BranchObjects, then check which one is the root for your working folder. Be aware though, that in TFVC multiple branches can be referenced in one workspace, so there may be multiple answers to this query, depending on which file you use the find the branchroot. A custom CodeActivity would do the trick, similar to this check in a custom checkin policy.
The code will be similar to:
IBuildDetail buildDetail = context.GetExtension<IBuildDetail>();
var workspace = buildDetail.BuildDefinition.Workspace;
var versionControlServer = buildDetail.BuildServer.TeamProjectCollection.GetService<VersionControlServer>();
var branches = versionControlServer.QueryRootBranchObjects(RecursionType.Full);
var referencedBranches = listOfFilePaths.GroupBy(
file =>
branches.SingleOrDefault(
branch => file.ServerItem.StartsWith(branch.Properties.RootItem.Item)
)
).Where(group => group.Key != null);
To get a list of all items in yo workspace, you can use Workspace.GetItems.
In case you're using Git, you have a few options as well. The simplest is to invoke the command line:
git symbolic-ref --short HEAD
or dive into LibGit2Sharp and use it to find the branch name based on the current working folder from a custom activity.
If you want to include this in an MsBuild task, this may well be possible as well. It goes a bit far for this answer to completely outline the steps required, but it's not that hard once you know what to do.
Create a custom MsBuild task that invokes the same snippet of code above, though instead of getting access to the workspace through BuildDetail.BuildDefinition.Workspace, but through the WorkStation class:
Workstation workstation = Workstation.Current;
WorkspaceInfo info = workstation.GetLocalWorkspaceInfo(path);
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(info.ServerUri);
Workspace workspace = info.GetWorkspace(collection);
VersionControlServer versionControlServer = collection.GetService<VersionControlServer>();
Once the task has been created, you can create a custom .targets file that hooks into the MsBuild process by overriding certain variables or copying data when the build is finished. You can hook into multiple Targets and define whether you need to do something before or after them.
You can either <import> these into each of your projects, or you can place it in the ImportAfter or ImportBefore folder of your MsBuild version to make it load globally. You can find the folder here:
C:\Program Files (x86)\MSBuild\{MsBuild Version}\Microsoft.Common.Targets\ImportAfter

Issues with multiple build template (xaml files) in project

I'm having trouble with my TFS Build project. I'm following the example from here as far as setting up the project in VS.
I have a project just for the custom activities and a separate project for just the templates. The issue I have is that the Templates project can only contain one xaml file. If I copy and and paste it in the same project it wont build. I get the following error:
Error 102 The item "obj\Debug\TfsBuild_Process_BeforeInitializeComponentHelper.txt" was specified more than once in the "Resources" parameter. Duplicate items are not supported by the "Resources" parameter.
But if I remove it, it'll build just fine. Everything works, meaning it'll build if I queue a build.
I have also tried the solution suggested on here, but it doesn't seem to resolve my issue.
Solution:
Renaming the form name in the solution explorer.
Change the class name in the .cs and Designer.cs file.
Change the constructor and destructor name (which is same as class name)
Change the name space name (if needed).
Here is the link to download the project.
I can't seem to figure out why. Any help is greatly appreciated!
I don't know if you still have this problem but I also had the problem today. I found a resolution that (if you still facing the problem) can help you or others.
When you copy a Build Process template it automaticly gets the x:Class property set to TfsBuild.Process. When you have multiple process templates with the same value then you will get this error when building the project.
<Activity mc:Ignorable="sads sap sap2010" x:Class="TfsBuild.Process" this:Process.BuildNumberFormat="$(BuildDefinitionName)_$(Date:yyyyMMdd)$(Rev:.r)" this:Process.AgentSettings="[New Microsoft.TeamFoundation.Build.Workflow.Activities.AgentSettings() With {.MaxWaitTime = New System.TimeSpan(4, 0, 0), .MaxExecutionTime = New System.TimeSpan(0, 0, 0), .TagComparison = Microsoft.TeamFoundation.Build.Workflow.Activities.TagComparison.MatchExactly}]" this:Process.Verbosity="[Microsoft.TeamFoundation.Build.Workflow.BuildVerbosity.Normal]" this:Process.OutputFullHistory="True" this:Process.OutputBranchOrigin="True" this:Process.OnlyOutputChangedFiles="True" this:Process.StartChangeset="0"......
I solved the problem by replacing TfsBuild.Process with another name for every template (ex. TfsBuild.ProcessLabDefault). Please note you should also change all the instances of this:Process because the two are linked together (ex. this:ProcessLabDefault).
<Activity mc:Ignorable="sads sap sap2010" x:Class="TfsBuild.MyProcess" this:MyProcess.BuildNumberFormat="$(BuildDefinitionName)_$(Date:yyyyMMdd)$(Rev:.r)" this:MyProcess.AgentSettings="[New Microsoft.TeamFoundation.Build.Workflow.Activities.AgentSettings() With {.MaxWaitTime = New System.TimeSpan(4, 0, 0), .MaxExecutionTime = New System.TimeSpan(0, 0, 0), .TagComparison = Microsoft.TeamFoundation.Build.Workflow.Activities.TagComparison.MatchExactly}]" this:MyProcess.Verbosity="[Microsoft.TeamFoundation.Build.Workflow.BuildVerbosity.Normal]" this:MyProcess.OutputFullHistory="True" this:MyProcess.OutputBranchOrigin="True" this:MyProcess.OnlyOutputChangedFiles="True" this:MyProcess.StartChangeset="0"......
I didn't find a way to change this by using the interface, View Code & Find and replace works. Hope it helps.
I used the following approach and it worked very well. All that needs replaced is TfsBuild in two locations on the first line of the raw xaml.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/d3fb6b89-b711-4a02-a10a-a88a0cb4f714/unable-to-add-more-than-one-templatexaml-into-activity-library-workflow-project?forum=tfsprocess
From the link:
1 Right-click your Copy of DefaultTemplate.xaml in Source Control and select View With… to open it in Notepad (you can also right-click the template in VS and select 'View Code').
2 Then in the first line change:
**x:Class="TfsBuild.Process"** to **x:Class="YourProcessTemplateName.Process"**
**xmlns:this="clr-namespace:TfsBuild"** to **xmlns:this="clr-namespace:YourProcessTemplateName"**
I ran in to this problem and wanted to add the solution I found to it. If you check the properties of the XAML files, when you link them to your project they're assigned a BuildAction of XamlAppDef. Changing this to 'Content' will resolve the error.
To find the properties screen you need to edit just highlight the XAML file in Solution Explorer and either hit F4 or Right-Click on the file and choose 'Properties' from the context menu.
Hope this helps.

TFS Build Template Nested Arguments

Is it possible to nest TFS build template arguments in one another?
Example (Set via build definition ui):
$(ToolsRoot) = E:\BuildTools
$(MSPECTools) = $(ToolsRoot)\MSpec\
Alternatively, is it possible to use environmental variables.
I have tried both, and neither seemed to work.
I need to find a way of setting the build root dynamically, as it differs on our various build servers.
I suppose you have implemented a topology like the this:
So, you need to control the root for each Agent.If you open the TFS Admin Console > Build configuration in Build Machine #1 you 'll see the Build Controller & Agents A.1 & A.2.If you open TFS Admin Console > Build configuration in Build Machine #2 you 'll see Agents A.3, A.4, A.5 & A.6.
For any given build Agent, if you click on "Properties" you 'll see the "Working Directory" entry, which typically is set to something like $(SystemDrive)\Builds\$(BuildAgentId)\. On runtime this is transformed into something like C:\Builds\55.
For any given build, in the build definition area "Workspaces" this "Working Directory" equals the entry $(SourceDir).
Suppose you have set in Agent A.1 a working directory "C:\A.1\Build" & in Agent A.2 "C:\A.2\Build". In order to get what you need, you have to set in the build definition a mapping to $(SourceDir)\Template

Resources