Skip to content

CruiseControl.NET: PowerShell tasks II

September 1, 2011

In this post I describe how to pass build number into powershell tasks from CC config and how generate readable report for multiple powershell tasks after their execution by Cruise Control .NET.

For general overview see previous post  about powershell task

First of all below is example of  how to use dynamic values within ccnet.config file. Here we are using replacement of build number  that are taken from <projectname>.state file

...
<powershell>
<scriptDirectory>C:\CruiseControl.NET\Scripts</scriptDirectory>
<script>scriptname.ps1</script>
<dynamicValues>
<replacementValue property="buildArgs">
<format>{0}</format>
<parameters>
<namedValue name="$CCNetLabel" value="Default"/>
</parameters>
</replacementValue>
</dynamicValues>
<executable>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</executable>
</powershell>
...

You also can use the following parameters: $CCNetArtifactDirectory, $CCNetBuildCondition, $CCNetBuildDate, $CCNetBuildTime, $CCNetFailureUsers, $CCNetIntegrationStatus, $CCNetLabel, $CCNetListenerFile, $CCNetModifyingUsers, $CCNetProject, $CCNetProjectUrl, $CCNetUser, $CCNetRequestSource, $CCNetWorkingDirectory

And here is short example powershell script that takes one argument as parameter

$build_number = $args[0];

$results_dir = resolve-path "..\Results\";
$output_dir = "$results_dir\$build_number\";<wbr>

# create output directory basing on buildnumber
 if ((Test-Path -path $output_dir) -ne $True)
{
New-Item $output_dir -type directory
}
$filename = [System.IO.Path]::GetFileName(<wbr>$myinvocation.mycommand.<wbr>definition);
#example result output                              <wbr>                              <wbr>
Write-Host "Test name:" $filename
Write-Host "test_status:fail";
{exit 1}

After building CC.NET generates the build log (with msbuild and powershell tasks), e.g.:

<cruisecontrol project="Analyzer 2">
  <request source="xxxx" buildCondition="ForceBuild"><wbr>Build (ForceBuild) triggered from xxxx</request>
 <parameters>
 <parameter name="$CCNetArtifactDirectory" value="C:\CruiseControl.NET\<wbr>artifacts" />
 ...
 <parameter name="$CCNetWorkingDirectory" value="C:\CruiseControl.NET\<wbr>working" />
 </parameters>
 <modifications />
 <integrationProperties>
 <CCNetArtifactDirectory>C:\<wbr>CruiseControl.NET\_ccnet_<wbr>history_analyzer2\artifacts</<wbr>CCNetArtifactDirectory>
 ...
 <CCNetWorkingDirectory>C:\<wbr>CruiseControl.NET\_ccnet_<wbr>history_analyzer2\working</<wbr>CCNetWorkingDirectory>
 <CCNetUser />
 <LastIntegrationStatus><wbr>Success</<wbr>LastIntegrationStatus>
 <<wbr>LastSuccessfulIntegrationLabel<wbr>>00005</<wbr>LastSuccessfulIntegrationLabel<wbr>>
 <LastModificationDate>8/31/<wbr>2011 12:54:41 PM</LastModificationDate>
 </integrationProperties>
 <build date="2011-09-01 12:54:20" buildtime="00:00:19" buildcondition="ForceBuild">

<msbuild startTime="09/01/2011 12:54:23" elapsedTime="00:00:01" elapsedSeconds="1" success="true" />

<msbuild startTime="09/01/2011 12:54:26" elapsedTime="00:00:00" elapsedSeconds="0" success="true" />

<buildresults>
 <message> Directory: C:\CruiseControl.NET\_ccnet_<wbr>history_analyzer2\working\<wbr>Scripts\Co</message>
 <message> ntinuousIntegration\Results</<wbr>message>
 <message>Mode LastWriteTime Length Name </message>
 <message>---- ------------- ------ ---- </message>
 <message>d---- 9/1/2011 12:54 PM 00004 </message>
 <message>Test name: HistoryAnalyzer2_<wbr>unitMonitorAlerts_1200.ps1</<wbr>message>
 <message>test_status:fail</<wbr>message>
 <message>exit 1</message>
</buildresults>

<buildresults>
 <message>Test name: HistoryAnalyzer2b_<wbr>unitMonitorAlerts_10800.ps1</<wbr>message>
 <message>test_status:fail</<wbr>message>
 <message>exit 1</message>
</buildresults>

<buildresults>
 <message>Test name: HistoryAnalyzer2b_<wbr>unitMonitorAlerts_21600.ps1</<wbr>message>
 <message>test_status:fail</<wbr>message>
 <message>exit 1</message>
</buildresults>
</build>
</cruisecontrol>

After that we need to parse this log and make report. For it we used custom xsl file:

<?xml version="1.0"?>
<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output method="html"/>

<xsl:template match="/">
 <table width="98%" cellpadding="0" cellspacing="0" style="margin-top:10px;">
 <tr>
 <xsl:variable name="script.count" select="/cruisecontrol/build/buildresults" />
 <td style="background-color: #2E8A2E;color: #FFFFFF;padding-left:5px;">Powershell output (<xsl:value-of select="count($script.count)" />)</td>
 </tr>
 </table>
 <table style=" cellpadding:0; cellspacing:0;width:98%;">
 <thead>
 <tr>
 <th style="background-color:#c0c0c0;border:1px solid black;">Test Name</th>
 <th style="background-color:#c0c0c0">Result</th>
 </tr>
 </thead>
 <xsl:for-each select="/cruisecontrol/build/buildresults">
 <tbody>
 <tr>
 <xsl:for-each select="message">
 <xsl:if test="contains(current(), 'Test name:')">
 <td style="padding-left:10px;border:1px solid black;">
 <xsl:value-of select="substring(current(), string-length('Test name:') + 1)"/>
 </td>
 </xsl:if>
 <xsl:if test="contains(current(),'test_status:pass')">
 <td style="background-color:#00CC00; text-align:center;">
 <xsl:value-of select="current()"/>
 </td>
 </xsl:if>
 <xsl:if test="contains(current(),'test_status:fail')">
 <td style="background-color:#ff3300; text-align:center;">
 <xsl:value-of select="current()"/>
 </td>
 </xsl:if>
 </xsl:for-each>
 </tr>
 </tbody>
 </xsl:for-each>
 </table>
 </xsl:template>
 </xsl:stylesheet>
 

Create and copy file with contents above  into Cruise Control .NET server/xsl and webdasboard/xsl folders. After that you need to add it in publishers section of your project config and dashboard.config files:

</pre>
...
<xslFiles>
 <file>xsl\header.xsl</file>
 <file>xsl\modifications.xsl</file>
...
 <file>xsl\ConsoleOutput.xsl</file>
...
 </xslFiles>
...

And you will get report on your ccnet dashboard and e-mail notification that will look like:

Advertisements
One Comment
  1. anonim permalink

    So using xpath we can parse output log and transform it for reporting… am I right?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: