Invoking a post-build batch file in TFS 2010

The scenario: you are using TFS 2010 as your build server, and you would like to deploy files to a remote server upon successful build by invoking a batch file.

Do note that the following steps work great in conjunction with Visual Studio T4 Templates, whereby multiple .config files (e.g. Web.Dev.config, Web.Test.config) can be generated from a master template to reflect different connection strings, etc. Oleg Sych has a nice write-up on this. Hint: hand-edit your .csproj file and use the DependentUpon element to keep your project view clean in Visual Studio—multiple .tt files can be nested under the main .tt file.

  1. Make a copy of DefaultTemplate.xaml and check it in. Let’s call it BatchFilePostBuild.xaml.
  2. Open BatchFilePostBuild.xaml in the designer and add 2 new Arguments:
    • BatchFile (Direction = In, Argument type = String, Default value = [blank])
    • BatchFileArguments (Direction = In, Argument type = String, Default value = [blank])
  3. Expand “Run On Agent”. Drag “InvokeProcess” from the Toolbox and place it after “Try Compile, Test, and Associate …”
  4. In the Properties pane for InvokeProcess:
    Property Value
    Arguments BatchFileArguments
    EnvironmentVariables New Dictionary(Of String, String) From {{"SourcesDirectory", SourcesDirectory}, {"BinariesDirectory", BinariesDirectory}}
    FileName BatchFile.Replace("$(SourceDir)", SourcesDirectory)
  5. Double-click InvokeProcess:
    • Drag “WriteBuildMessage” from the Toolbox and place it under stdOutput. Set Importance = High, Message = stdOutput.
    • Drag “WriteBuildWarning” from the Toolbox and place it under stdError. Set Message = stdError.
  6. Save and check in BatchFilePostBuild.xaml.
  7. In Team Explorer, create a new build definition, with its process template being BatchFilePostBuild.xaml.
  8. In the Process tab, there will be 2 properties under Misc:
    Property Value
    BatchFile Here you can specify the path and file name to your batch file relative to $(SourceDir). Refer to the folder structures in the Workspace tab.
    BatchFileArguments A list of arguments separated by spaces. In your batch file, they will be %1, %2, %3, and so on.
  9. Create and check in your batch file. In this batch file you will be able to refer to %SourcesDirectory% and %BinariesDirectory%, in addition to %1, %2, %3, etc. above. Assuming %1 is the environment type (e.g. “Test”) and %2 is the server name, here are some useful commands:
    Clean target folder del /s /q "\\%2\path\to\remote\folder\*"
    Copy directory structure xcopy "%BinariesDirectory%\path\to\folder\*" "\\%2\path\to\remote\folder" /s /y
    Copy Web.Test.config to the Test server copy /y "%BinariesDirectory%\_PublishedWebsites\MyProject\Web.%1.config" "\\%2\path\to\remote\folder\Web.config"
    Start remote Windows service sc \\%2 start "My Windows Service"
    Stop remote Windows service sc \\%2 stop "My Windows Service"
    Dirty wait for around 30 seconds ping 127.0.0.1 -n 30 > NUL

4 June 2012 | .NET | Comments

6 Responses to “Invoking a post-build batch file in TFS 2010”

  1. 1 Jeremy 21 September 2012 @ 3:19 am

    I got this correct up to 8 and 9. Can I see what you have in your batch file?

  2. 2 Jeremy 21 September 2012 @ 3:35 am

    Forgot to mention… awesome article by the way! I’m so close. I’m trying to delete a web.config before it moves a copy to a secondary location that I created in the .xaml

  3. 3 Shweta 25 September 2012 @ 7:24 pm

    Thanks for the good post… I was trying the same with a sample batch file Hello.bat
    The contents of the batch is
    echo hello
    notepad.exe
    I have added the activity InvokeProcess after Try Compile, Test, and Associate… On TFS my batch file execution does not seem to happen, neither am I getting the echo message nor the notepad.exe opening.
    In the properties of InvokeProcess I have only provided the FileName as “D:\Hello.bat”.
    Please help.

  4. 4 Create batch file 5 February 2013 @ 7:32 am

    Quite useful, thanks! But it’s much better to create batch files with tool called Dr.Batcher - you should also mention it.

  5. 5 Roy 6 March 2013 @ 12:57 am

    For Step 8, there is no “Misc” in the Process tab. All I see are: 1. Required, 2. Basic and 3. Advanced, non of which contain “Misc” when expanded.

    Any idea why?

  6. 6 Roy 6 March 2013 @ 1:30 am

    Mystery solved. Once I checked in the template and my batch file, “Misc” appeared in the Process tab.

Comments:

  1.  
  2.  
  3.