Introduction
In this article, we will see how to create TFS build Custom task.
In this example, I want to execute my own custom task which will write some text into event viewer as “My Custom Task is executed” & this custom task has to be executed after TFS build succeeds.
To proceed on, we have to handle the below step:
- Create class library to handle “Custom Task”
- Create “TFS Build Agent”
- Create “New Build definition”
- Run TFS build
Step 1 – Custom Task
Writes text into event viewer after TFS build succeeds. This is to be implemented in Class Library.
- First create Class Library & name it as “
MySample
”
- Add a class called
MyTask
and add the following references to the library:
Microsoft.Build.Framework
Microsoft.Build.Utilities
System.Diagnostics
- Now inherit the class “
TASK
” as shown below:
public class MyTask : Task
{
public override bool Execute()
{
EventLog log = new EventLog();
log.Source = “Application”;
log.WriteEntry(”Step 2 Executed”);
return true;
}
}
Note: In the above code, we are overriding execute
method & implementing our own implementation.
- Now compile it and as usual, it will generate MySample.dll.
Step 2 – Create TFS Build Agent
Before working on TFS build definition, we need to have TFS build agent.
Let’s follow the below steps to create a build agent:
- Connect to TFS Server from Team Explorer.
- Right click on Build folder & select “Manage Build Agent”.
- Now click on “New” button in build agent window & provide below necessary information:
- Display Name: of your choice
- Computer Name: TFS Server name
- Communications port: TFS server port
- Working directory: TFS build will create build files at this location. So provide path of TFS server physical path
- Agent Status: Enabled
- Now Click “Ok” button
Step 3 – Create TFS Build Definition
- Connect to TFS Server from Team Explorer
- Right click on Build folder & select “New Build Definition”
- A new window will open up as shown below:
- General – Build definition name: Enter build definition name
- Select workspace
- Status: Active
- Source Control Folder: TFS server path project
- Select “Project file”, click create button:
- MSBuild Project file creation wizard will open up. In this, select the item as shown below:
- Click next and leave other options as is & click FINISH button.
- Leave retention policy section as it is.
- Now select Build Defaults
- Build Agent: Should select build agent name that we have created in Step 2 – Create TFS Build Agent
- Builds will be staged: should provide shared path which TFS server has access
- Trigger section allows to configure TFS build to run at those intervals
- Finally click ok button
Step 4 – Run TFS Build
- Connect to TFS Server from Team Explorer
- Expand Build folder & right build definition name which you want to run. In our case, right click “
MyTestBuildDefinition
” & select Queue new build definition
- Select as shown below:
- Build Definition name
- Build Agent name
- Drop folder
- Priority
-
Now by this time, build definition would be started up & know the processing right click on build definition name & select open.
- Finally you should be seeing build succeeded if everything goes fine & now go to the path (entered while creating build definition & build agent) to see the build files.
Happy coding… hope this helps!
History
- 29th October, 2011: Initial version