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

Posted on Ewald Hofman See other posts from Ewald Hofman
Published on Tue, 09 Nov 2010 15:51:00 +0100 Indexed on 2010/12/29 18:00 UTC
Read the original article Hit count: 545

Filed under:
|

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


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.


© Ewald Hofman or respective owner

Related posts about Team Build

Related posts about VS 2010