Part 15: Fail a build based on the exit code of a console application

by Ewald Hofman 9. November 2010 06:51

In the series the following parts have been published

  1. Part 1: Introduction
  2. Part 2: Add arguments and variables
  3. Part 3: Use more complex arguments
  4. Part 4: Create your own activity
  5. Part 5: Increase AssemblyVersion
  6. Part 6: Use custom type for an argument
  7. Part 7: How is the custom assembly found
  8. Part 8: Send information to the build log
  9. Part 9: Impersonate activities (run under other credentials)
  10. Part 10: Include Version Number in the Build Number
  11. Part 11: Speed up opening my build process template
  12. Part 12: How to debug my custom activities
  13. Part 13: Get control over the Build Output
  14. Part 14: Execute a PowerShell script
  15. Part 15: Fail a build based on the exit code of a console application
  16. Part 16: Specify the relative reference path


When you have a Console Application or a batch file that has errors, the exitcode is set to another value then 0. You would expect that the build would see this and report an error. This is not true however. First we setup the scenario.

  1. Add a ConsoleApplication project to your solution you are building.
  2. In the Main function set the ExitCode to 1
  3.     class Program
        {
           
    static void Main(string
    [] args)
            {
               
    Console.WriteLine("This is an error in the script."
    );
               
    Environment.ExitCode = 1;
            }
        }

  4. Checkin the code. You can choose to include this Console Application in the build or you can decide to add the exe to source control
  5. Now modify the Build Process Template CustomTemplate.xaml
  6. Add an argument ErrornousScript
  7. Scroll down beneath the TryCatch activity called “Try Compile, Test, and Associate Changesets and Work Items”
  8. Add an Sequence activity to the template
  9. In the Sequence, add a ConvertWorkspaceItem and an InvokeProcess activity (see Part 14: Execute a PowerShell script  for more detailed steps)
  10. In the FileName property of the InvokeProcess use the ErrornousScript so the ConsoleApplication will be called.
  11. Modify the build definition and make sure that the ErrornousScript is executing the exe that is setting the ExitCode to 1.

You have now setup a build definition that will execute the errornous Console Application. When you run it, you will see that the build succeeds. This is not what you want!

image

To solve this, you can make use of the Result property on the InvokeProcess activity.

So lets change our Build Process Template.

  1. Add the new variables (scoped to the sequence where you run the Console Application) called ExitCode (type = Int32) and ErrorMessage
  2. Click on the InvokeProcess activity and change the Result property to ExitCode
  3. In the Handle Standard Output of the InvokeProcess add a Sequence activity
  4. In the Sequence activity, add an Assign primitive. Set the following properties:
    To = ErrorMessage
    Value = If(Not String.IsNullOrEmpty(ErrorMessage), Environment.NewLine + ErrorMessage, "") + stdOutput
  5. And add the default BuildMessage to the sequence that outputs the stdOutput
  6. Add beneath the InvokeProcess activity and If activity with the condition ExitCode <> 0
  7. In the Then section add a Throw activity and set the Exception property to New Exception(ErrorMessage)

The complete workflow looks now like

image

When you now check in the Build Process Template and run the build, you get the following result

image

And that is exactly what we want.


 

You can download the full solution at BuildProcess.zip. It will include the sources of every part and will continue to evolve.

Tags:

Team Build | VS 2010

Comments

11/9/2010 6:52:56 AM #

trackback

Part 14: Execute a PowerShell script

Part 14: Execute a PowerShell script

Ewald Hofman |

11/10/2010 9:14:33 AM #

pingback

Pingback from blogs.like10.com

TechDays 2010 Presentations « Microsoft Team Foundation Server, SharePoint Server, .NET, and SQL Server

blogs.like10.com |

1/18/2011 1:56:01 PM #

Gary Miner

I can not figure out how to get the exit code of a powershell script to the result. I have a script that attempts to divide by zero. I see what appears to be an exit code of -1 in the stdOutput but the Result is alway 0.

Error Message:Attempted to divide by zero.
Error: Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available.
-1

Final Property Values
Arguments = -noprofile -nologo -noninteractive -command .\Deploy.ps1 local -execute
EnvironmentVariables =
FileName = powershell.exe
OutputEncoding = System.Text.SBCSCodePageEncoding
Result = 0
WorkingDirectory = D:\R&Dtest\StagingDirectory

Gary Miner United States |

1/19/2011 10:46:33 AM #

Gary Miner

I found my answer in case anyone else is running into this issue. I had to do two things.
1) Instead of calling the script with the -command switch I had to use the -file switch.
   example.   Arguments = -noprofile -nologo -noninteractive -File .\fail.ps1
2) added a catch at the very end of my script then set the exit code to 1

    trap {
      write-host $_
      exit 1
    }

Now my executed script that fails exits with a 1 and actually fails the build.

Gary Miner United States |

1/19/2011 3:22:52 PM #

Ewald Hofman

Very nice solution. Thanks for the update.

Ewald Hofman Netherlands |

5/31/2011 8:22:23 AM #

trackback

Ewald’s posts on TFS Build 2010

Ewald Hofman , an ALM MVP, has written a great series of blog posts on Team Build in Team Foundation Server 2010.  The 2010 release introduces Windows Workflow as the overall orchestrator of the build process.  Ewald walks you through quite a few topics related to this and the other new features.  Check it out! Part 1: Introduction Part 2: Add arguments and variables Part 3: Use more complex arguments Part 4: Create your own activity Part 5: Increase AssemblyVersion Part 6: Use custom type for an argument Part 7: How is the custom assembly found Part 8: Send information to the build log Part 9: Impersonate activities (run under other credentials) Part 10: Include Version Number in the Build Number Part 11: Speed up opening my build process template Part 12: How to debug my custom activities Part 13: Get control over the Build Output Part 14: Execute a PowerShell script Part 15: Fail a build based on the exit code of a console application Part 16: Specify the relative reference path Technorati Tags: tfs 2010 , team build , windows workflow

Buck Hodges |

7/21/2011 9:45:19 AM #

pingback

Pingback from programmersgoodies.com

Capture output of regsvr32 in an InvokeProcess TF Workflow Activity - Programmers Goodies

programmersgoodies.com |

8/10/2011 9:13:14 AM #

pingback

Pingback from blog.agilepartner.net

Setting up FitNesse for Team Foundation Server | Agile Partner Blog

blog.agilepartner.net |

2/9/2013 8:13:43 AM #

pingback

Pingback from blog.softwarefun.nl

Using NDepend as an alternative to the Layered DiagramSoftwarefun | Softwarefun

blog.softwarefun.nl |

Comments are closed

Powered by BlogEngine.NET 1.6.1.0
Theme by Mads Kristensen


ClusterMap

Statistics

Statistics created at 09 Sep 2009

121 posts
487 comments
316 raters
1764121 visit (1044 per day)
21 users online

Recent comments

Comment RSS