How to Work With TFS - TFS Build Agent, Build Definition, TFS Build Custom Task

In this example, I want to execute my own custom task which will write some text into the event viewer as "My Custom Task is executed" & this custom task has to be executed after the TFS build succeeds To proceed we have to handle the below step.

  1. Create a class library to handle "Custom Task"
  2. Create "TFS Build Agent"
  3. Create "New Build definition"
  4. Run TFS build

Step 1. Custom task

Writes text into event viewer after TFS build succeeds. This is to be implemented in the Class Library.

  1. First, create a Class Library & name it "MySample".
  2. Add a class called MyTask and add a flowing reference to the library.
    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;
    using System.Diagnostics;
    
  3. 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 the execute method & implementing our own implementation.

Now compile it and as usual, it will generate MySample.dll

Step 2. Create a TFS Build Agent

Before working on a TFS build definition we need to have a TFS build agent. Let's follow the below steps to create a build agent.

  1. Connect to the TFS Server from Team Explorer.
  2. Right-click on the Build folder & select "Manage Build Agent".
    Manage build
  3. Now click on the "New" button in the build agent window & provide the below necessary information.
    Click on  new button
  4. Display name: Name of your choice.
  5. Computer name: TFS Server name.
  6. Communications port: TFS server port.
  7. Working directory: TFS build will create build files at this location. To provide the path of the TFS server's physical path
  8. Agent status: Enabled
    Now click the "OK" button.

Step 3. Create TFS build definition

  1. Connect to the TFS Server from Team Explorer.
  2. Right-click on the Build folder & select "New Build Definition".
    New build definition
  3. A new window will open up as shown below.
    General
  4. General build definition name: Enter the build definition name.
  5. Select workspace.
    Select workspace
  6. Status: Active
  7. Source control folder: TFS server path project.
  8. Select "Project file", and click the Create button.
    Project
  9. MS Build Project file creation wizard will open up. In this select the item as shown below.
    Selection
  10. Click next and leave other options as is & click the FINISH button.
  11. Leave the retention policy section as it is.
    Retention policy
  12. Now select Build Defaults.
  13. Build agent: Select the build agent name that we have created in Step 2 Create TFS Build Agent.
  14. Builds will be staged: should provide a shared path to which the TFS server has access.
  15. The trigger section allows configuring TFS to build to run at those intervals.
  16. Finally, click the ok button.

Step 4. Run TFS build

  1. Connect to the TFS Server from Team Explorer.
  2. Expand the Build folder & write the build definition name that you want to run. In our case right-click "MyTestBuildDefinition" & select Queue new build definition.
    Queue new build
  3. Select as shown below
    • Build Definition name
    • Build Agent name
    • Drop folder
    • Priority
      Queue

Now by this time, the build definition would be stated up & know the processing right click on the build definition name & select open.

Open

Finally, you should see seeing build succeeded if everything goes fine & now go to the path (entered while creating the build definition & build agent) to see the build files. Happy coding, Hope this helps!