| Steven's profileDevelopmentalBlogLists | Help |
|
06 April TeamBuild, PowerShell, and Exit CodesAt my work I am starting to integrate PowerShell scripts into the TeamBuild projects for our nightly build. Including PowerShell is very easy: you just need to ensure that PowerShell is installed on the build server. Installing PowerShell will adjust the PATH environment variable, but you may need to restart the build service in order for the build server to pick up this new path. Including a PowerShell script in your TeamBuild (or MSBuild) targets is very easy. You might have a target that looks something like this:
As with most environments, we need to change the execution policy so that it will allow scripts to be run. We only need to do this once for our whole build, so best to create it in its own target and place a dependency to it from other targets that will be calling PS scripts. In the above example, we are calling a script in the Scripts folder called testscript.ps1. Pretty simple really. If an error occurs in that script, then the exit code will indicate an error, and the build will fail. However, we may not want the build to fail. Perhaps only a warning is required. Or perhaps we want the script to do things that might break the build AND raise warnings. How can we handle this? An easy way is to use exit codes. Most EXEC calls will return an exit code. 0 usually means success, and 1 or higher means failure. In MSBuild (ie. TeamBuild) that means 1 or higher will break the build. So what if we want our PowerShell script to return all sorts of exit codes, and only have certain exit codes break the build, and others raise warnings? This is easy too, but requires some jigging. First, we call the EXEC task with an attribute that tells it to ignore the exit code that results from the call. This means that even failed calls will not break the build. Second, we capture the exit code of the EXEC task with the Output attribute and assign it to a Property. The target might then look like this:
Cool... we now have the exit code in the property 'ScriptExitCode' and can manually do what we like with it. Using the Warning and Error tasks that already exist, we can react in a more custom way:
In this example, if the script returns exit code 1, the build still fails. However if it returns 2, then only a warning is raised, and the build continues on. So only one thing remains: how do we tell PowerShell scripts to return a specific exit code? Well that's just as easy. In our PS script, we can use this line:
$host is a reserved variable in PowerShell. In the above code, we are telling the script to return exit code 2 when it exits. Note that this line does not tell the script to exit straight away. To do that, call the command 'exit'. So using this knowledge, we can sprinkle the relative exit codes throughout our scripts and let MSBuild handle it on the outside. 03 April How to lose an entire week in conferences and presentationsBrisbane: A rich ecosystem of technology, enthusiasm, and deliverance. I often wonder how I manage to stay married when there is so much going on in this town that keeps me distracted. I'm sure its just as rich in other cities, but hell, I don't care about those infestations. Brisbane is great for the developer/enthusiast and here's why: my week since last Wednesday. Weds 26th March - Heroes Happen Thurs 27th March - Sql Server User Group Mon 1st April - QUT Alumi Presentation Tues 2nd April - Security Interest Group Weds 3rd April - Briztalk User Group So as you can see, Brisbane is a rich fertile ground of development. I've learnt so much over the last week, its ridiculous. Luckily the VSTS user group (was suppose to be Friday morning) was postponed, help give meaning to the weeks ahead! Next week looks rather quiet, which should be good because I have some interesting ideas around F# that I would like to explore. See you at the next Brisbane community event. |
|
|