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
Team Build
|VS 2010
In the series the following parts have been published
- 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
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.
- Add a ConsoleApplication project to your solution you are building.
- In the Main function set the ExitCode to 1
- 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
- Now modify the Build Process Template CustomTemplate.xaml
- Add an argument ErrornousScript
- Scroll down beneath the TryCatch activity called “Try Compile, Test, and Associate Changesets and Work Items”
- Add an Sequence activity to the template
- In the Sequence, add a ConvertWorkspaceItem and an InvokeProcess activity (see Part 14: Execute a PowerShell script for more detailed steps)
- In the FileName property of the InvokeProcess use the ErrornousScript so the ConsoleApplication will be called.
- Modify the build definition and make sure that the ErrornousScript is executing the exe that is setting the ExitCode to 1.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("This is an error in the script.");
Environment.ExitCode = 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!
To solve this, you can make use of the Result property on the InvokeProcess activity.
So lets change our Build Process Template.
- Add the new variables (scoped to the sequence where you run the Console Application) called ExitCode (type = Int32) and ErrorMessage
- Click on the InvokeProcess activity and change the Result property to ExitCode
- In the Handle Standard Output of the InvokeProcess add a Sequence activity
- In the Sequence activity, add an Assign primitive. Set the following properties:
To = ErrorMessage
Value = If(Not String.IsNullOrEmpty(ErrorMessage), Environment.NewLine + ErrorMessage, "") + stdOutput - And add the default BuildMessage to the sequence that outputs the stdOutput
- Add beneath the InvokeProcess activity and If activity with the condition ExitCode <> 0
- In the Then section add a Throw activity and set the Exception property to New Exception(ErrorMessage)
The complete workflow looks now like
When you now check in the Build Process Template and run the build, you get the following result
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