Share via


Custom build activity for TFS 2010 to send email with build details – Part 2

My previous blog post talked about how to develop a custom workflow activity to use it in the build workflow process to generate email with work item and file information. This second installment talks about how to integrate the custom workflow activity in your build process.

First step in this integration is to compile the assembly and register it in Global Assembly Cache (GAC) using gacutil.exe. Open up Visual Studio Command Prompt (2010) in administrator mode by going to Start –> All Programs –> Microsoft Visual Studio 2010 –> Visual Studio Tools. Type gacutil /i <custom library location> to register it in the GAC. This will ensure that the assembly is available for the Visual Studio toolbox and Visual Studio workflow designer and will allow the workflow host to access the custom activity.

image 

Second step involves creating the build workflow process template. Instead of creating a new workflow template, we will make use of the default template that ships with Visual Studio Team Foundation Server. Create a new build definition in your TFS project and click on the process item to bring up the template configuration screen.

 image

Instead of modifying the default template please create a copy of it by clicking on new and selecting "Copy an existing XAML file”.

image

Once we have the new XAML file, open it up from the Source Control or from the local workspace which should load the Visual Studio workflow designer. Right click on the toolbox, select “Choose Items”, select “System.Activities” and click “Browse” to select the activity DLL, ensure that you select the custom activity in the “Team Foundation Build Activities” tab. Once you have it on the toolbox, now its time to drop it in a sequence for it start sending email. When you open the copy of the “Default Template” and collapse the workflow, you should see 4 parts of the main sequence – Get The Build, Update The Drop Location, Run on Agent and Check In Gated Changes for CheckInShelveset Builds – in which we will be making changes to Run on Agent sequence. 

image            image 

Double click on “Run On Agent” to open the sequence and drag and drop the custom activity from toolbox after where we would be adding the custom activity. At the end of “Try Compile, Test, and Associate Changesets and Work Items” sequence, drop a new “Sequence” from “Control Flow” group in toolbox and rename it to “Send Build Mail”. Now double click on the newly created sequence to drop the custom activity and configure its properties.

image

MailFrom, Mailto, StmpServer and Subject are very generic, notice that subject is using dynamic property “BuildNumber” to concatenate with subject to distinguish between various build definitions that use the same template. Build Detail can be directly configured with global variable “BuildDetail” that provides build summary information. The associated changesets is not that simple because the temporary variable that stores this information is not exposed back up to the “Run On Agent” sequence for us to use. So, let’s add a new variable “IList<Changeset>” and set the scope to the “Build On Agent” sequence.

image

Click on the “Variables” button at the bottom of the workflow designer to bring up the list of variables at the “Run On Agent” sequence. Add a new variable called “TempAssociatedChangesets” with variable type “IList<Changeset>” and scope set to “Run On Agent”. Double click on the “Try Compile, Test, and Associate Changesets and Work Items” and “Sequence” in the “Try” block to add an “Assign” activity. Drop the “Assign” activity from “Primitives” tab of toolbox, set “To” to “TempAssociatedChangesets” and “From” to “associatedChangesets”.

image

Now you can go back to the “SendEmailActivity” and set the “BuildAssociatedChangesets” property to “TempAssociatedChangesets”. This should give the build activity access to the associated work items and files.

image

Now save the template and check in the file to save the changes in source control. On your build server you have to do the following things:

  1. Register the custom activity in Global Assembly Cache (GAC)
  2. Run the “Build Host” with the account that has permissions to send email as the mail from account. You can do this by going to the “Team Foundation Server Administration Console” and click on properties to set the build account.

After this, just schedule a build to receive the following email.

image

The email itself is not entirely bug free, like date and time are not correct and some times change sets repeat multiple times which need to be fixed, so feel free to modify the build activity and let me know the changes.

Many thanks to the following folks who helped me in creating this solution.

Jim Lamb - https://blogs.msdn.com/b/jimlamb/archive/2010/02/12/how-to-create-a-custom-workflow-activity-for-tfs-build-2010.aspx
Jacob Ehn - https://geekswithblogs.net/jakob/Default.aspx

Thanks
Anil RV

Comments

  • Anonymous
    March 29, 2011
    Hi,I have noticed that the Build End time is always empty. I think that the SendEmailActivity sends email before the build process is entirely completed. Hence the BuildDetail.EndTime property is empty.Can you help me in getting this value. Thanks in advance.
  • Anonymous
    May 08, 2012
    Typo in text: "StmpServer" should be "SmtpServer".  Great resources, thanks!
  • Anonymous
    August 06, 2012
    How can you add a custom activity WITHOUT GAC'ing... so that any Build Controller can properly access Activities? Thanks
  • Anonymous
    April 24, 2013
    I am always getting "Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender (type SmtpException)" Error.
  • Anonymous
    October 02, 2013
    @Niraj were you able to solved your issue? Same here
  • Anonymous
    April 01, 2014
    Hi I am unable to find the Email activity dll??where can I get that??
  • Anonymous
    September 08, 2014
    I am not able to see the Type “IList<Changeset>” while creating the variable . what can be the reason?
  • Anonymous
    November 02, 2014
    Hi,I am getting below error:The build process failed validation. Details:Validation Error: The private implementation of activity '1: DynamicActivity' has the following validation error:   Compiler error(s) encountered processing expression "AssociatedChangeset".'AssociatedChangeset' is a type and cannot be used as an expression.
  • Anonymous
    August 15, 2015
    The comment has been removed