Email notification on success/failed build using ant target - unit-testing

my current code(build.xml) enables me to send email on successful build, but when failed, nothing happens. The targets are called from a build.bat file through command similar to " ........ -DrepositoryAddress=%1 -DbuildResultUUID=%2 startPublish " (for all targets, in order startActivity->startPublish->mailer->startActivity).
Now, I also want email notification when the build fails.I guess trycatch will help me get the task done, but HOW? Not sure about it, where/how to place it(edit it?)? I kind of used trycatch, it gave me something like " Problem: failed to create task or type trycatch" . What modifications are required in current script/xml file to enable this functionality of sending email indicating status of build (successful or failed). Please guide/help.Thanks so much.
'
<target name="startActivity">
<fail message="Missing repositoryAddress" unless="repositoryAddress"/>
<fail message="Missing buildResultUUID" unless="buildResultUUID"/>
<fail message="Missing activityLabel" unless="activityLabel"/>
<!-- Replace ADMIN with your real credentials. -->
<startBuildActivity
buildResultUUID="${buildResultUUID}"
label="${activityLabel}"
autoComplete="true"
repositoryAddress="${repositoryAddress}"
userId="BuildAdmin"
password="Abc1234"/>
</target>
<target name="startPublish">
<sleep seconds="10"/>
<fail message="Missing repositoryAddress" unless="repositoryAddress"/>
<fail message="Missing buildResultUUID" unless="buildResultUUID"/>
<artifactfilePublisher repositoryAddress="${repositoryAddress}"
userId="BuildAdmin"
password="Abc1234"
buildResultUUID="${buildResultUUID}"
filePath="E:\Setup.msi"
label="Installer" />
</target>
<target name="mailer">
<property name="report" value="E:\Report.html"/>
<mail from="dmin#company.com" messagemimetype="text/html" charset="ISO-7779-1" messagefile="${report}" mailhost="HMMMM.company.com" mailport="25" tolist="admin#company.com" subject="Build status" />
</target>
<taskdef name="startBuildActivity"
classname="com.ibm.team.build.ant.task.StartBuildActivityTask" />
<taskdef name="artifactfilePublisher"
classname="com.ibm.team.build.ant.task.ArtifactFilePublisherTask" />
</project>'

you can implement a BuildListener that sends the email as described in the ant FAQ

Related

ANT build : Variable inside resolver:artifacts is undefined

I have setup this ANT build.xml file which pulls in version info from a txt file , reads the first line, trims it and copies it into a variable called 'versionVal' with below code:
<target name="clean"
.
. do something more
.
<loadfile property="versionValTxt" srcfile="version.txt">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.HeadFilter">
<param name="lines" value="1" />
</filterreader>
</filterchain>
</loadfile>
<loadresource property="versionVal">
<propertyresource name="versionValTxt"/>
<filterchain>
<tokenfilter>
<filetokenizer/>
<replacestring from="V" to=""/>
</tokenfilter>
<striplinebreaks/>
</filterchain>
</loadresource>
<echo>"Building for version: ${versionVal}"</echo>
</target>
And in one of the targets I am trying to refer a resolver artifact which uses this versionVal to find a file with that specific version in its name as shown below:
<resolver:artifacts id="producedArtifacts" >
<resolver:artifact file="${dist.dir}/App.${versionVal}.zip"/>
</resolver:artifacts>
<target name="nexus">
<echo>"versionVal: ${versionVal}"</echo>
<resolver:deploy artifactsref="producedArtifacts">
<resolver:remoterepo refid="ossrh"/>
</resolver:deploy>
</target>
And the build keeps failing as below, where it shows the variable versionVal is undefined.
C:\Users\XYZ\git\App\WebContent\dist\App.${versionVal}.zip does not exist
Note that the block is able to recognize ${dist.dir} but it doesnt recognize ${versionVal}. However I am able to print the value using an echo inside the target-nexus.
Much appreciated if anyone can point me in the right direction. I am not able to figure out why this variable is not recognized under "resolver:artifact file" and if there are any alternatives to this problem.
Realised I had to include below resolver artifact inside a target block and then use that as depends in the nexus block. After making this change the variable 'versionVal' was recognized.
Solution:
<target name="packagedArtifact" >
<resolver:artifacts id="producedArtifacts" >
<resolver:artifact file="${dist.dir}/App.${versionVal}.zip"/>
</resolver:artifacts>
</target>
<target name="nexus" depends="packagedArtifact">
<resolver:deploy artifactsref="producedArtifacts">
<resolver:remoterepo refid="ossrh"/>
</resolver:deploy>
</target>

Emailing XSLT Report using ant with Testng

1)Build.xml (i have tried with both Zip destfile)
<target name="sendMail" depends="makexsltreports">
<zip destfile="${basedir}/XSLT_Reports/output.zip" basedir="${basedir}/XSLT_Reports"/>
<!--zip destfile="D:\drive d\Script\Invoice\XSLT_Reports\output.zip" basedir="D:\drive d\Script\Invoice\XSLT_Reports"/ -->
<mail
tolist="xyz#abc.in"
from="abc#gmail.com"
subject="Invoice report"
mailhost="smtp.gmail.com"
mailport="465"
ssl="true"
user="abc#gmail.com"
password="xyz">
<attachments>
<fileset dir="${project.dir}/XSLT_Reports/">
<include name="**/*.zip"/>
</fileset>
</attachments>
</mail>
</target>
2)cmd Promt display below
sendMail:
[zip] Building zip: D:\drive d\Script\Invoice\XSLT_Reports\output.zip
[mail] Failed to send email: D:\drive d\Script\Invoice\${project.dir}\XSLT_
Reports does not exist.
3) Folder structure
D:\drive d\Script\Invoice\XSLT_Reports\output
Problem : Zip has been created as output.zip but could not able to send mail..
Any reply will be appreciable. Let me know if any additional details needed..

Is there a way to get build status as a property?

I have a ugly Teamcity build configuration using MSBuild. It executes custom application (test runner), which is using custom messaging to report test results to teamcity.
##teamcity[testStarted name='test1']
##teamcity[testFailed name='test1' message='failure message' details='message and stack trace']
Which show in teamcity in build overview and tests tab.
Teamcity recognizes failed tests and if any test fails, it marks the build as failed:
http://i.stack.imgur.com/Qz9UT.png
Later in the MSBuild target I would like to label cvs based on the test results.
Is there a way to get the build status (if it is failed, hanging, warning) as a property? something like %build.status%? The format does not matter - if its a string or number.
PS: I know that best solution to my problem would be to modify the application to return non-zero exit code if test fail.
TeamCty does not seem to expose this directly, but the status can be acquired using the REST api. Here is an example using curl; but you could also uwe PowserShell's Invoke-RestMethod for instance.
Here's the msbuild script that casues test failure I used for testing:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Test">
<Message Importance="high" Text="##teamcity[testStarted name='test1']" />
<Message Importance="high" Text="##teamcity[testFailed name='test1' message='failure message' details='message and stack trace']" />
</Target>
</Project>
Then the script that gets the current build's status, dumps it to a file, reads the file into an msbuild item and then uses regex to get the status out of it. You just have it to supply the tc_user and tc_password properties (or allow guest access) and change the url to match your server.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="GetBuildStatus">
<Target Name="RunCurl">
<PropertyGroup>
<MyTempFile>curl_out</MyTempFile>
</PropertyGroup>
<Exec Command="curl http://localhost/httpAuth/app/rest/builds/id:$(teamcity_build_id) -basic -u $(tc_user):$(tc_password) > $(MyTempFile)"/>
<ReadLinesFromFile File="$(MyTempFile)">
<Output TaskParameter="Lines" ItemName="CurlOutput"/>
</ReadLinesFromFile>
<Delete Files="$(MyTempFile)"/>
</Target>
<Target Name="GetBuildStatus" DependsOnTargets="RunCurl">
<PropertyGroup>
<CurlOutputFull>#(CurlOutput)</CurlOutputFull>
<BuildStatus>$([System.Text.RegularExpressions.Regex]::Match($(CurlOutputFull), `status="(\w*)"`).Groups[ 1 ].Value)</BuildStatus>
</PropertyGroup>
<Message Text="BuildStatus = $(BuildStatus)"/>
</Target>
</Project>
This prints:
BuildStatus = FAILURE

Clean up after nant build failures

I'm looking for my nant build script to be able to clean up after itself if a build goes wrong. I'm looking for something resembling the following execution:
Target= Software.Build
Target= Software.Build.Success *(depends on Software.Build succeeding)*
Target= Software.Build.Failed
I am looking for a solution that if the Software.Build target fails then Software.Build.Failed will be executed e.g. to e-mail someone that the build failed in some way, otherwise Software.Build.Success will be run to allow the build script to continue.
Is this even possible with nant? If so, could anyone point me to a suitable article/solution?
Or if you have global data to be cleaned up, you can use the NAnt OnFailure event.
<property name="nant.onfailure" value="failure" />
<target name="failure">
<!-- Put your cleaning code in here -->
</target>
NAntContrib has a trycatch task:
<trycatch>
<try>
<call target="Software.Build" />
</try>
<catch>
<call target="Software.Build.Failed" />
<fail message="build failed" />
</catch>
<finally>
<!-- execute everything that doesn't depend on success or failure -->
</finally>
</trycatch>
<call target="Software.Build.Success" />

nant script doesn't display unit test details

Can someone please tell me why my build script (nant) doesn't display the unit test details in the command prompt window? I have verbose set to true, but it doesn't want to display any details about my unit tests. Here's the target:
<target name="run-unit-tests" depends="compile, move.assemblies.for.tests, rebuildDatabase">
<mkdir dir="${tests.output.dir}" />
<nunit2 haltonfailure="true" failonerror="true" verbose="true">
<formatter type="Xml" extension=".xml" outputdir="${tests.output.dir}" usefile="true" />
<test assemblyname="${test.assembly.file}" />
</nunit2>
<echo message="Unit Testing Done!" />
</target>
The command prompt window just displays this:
[mkdir] Creating directory 'C:\Projects\TestProject\build\artifacts\UnitTestOutput'.
[echo] Unit Testing Done!
build:
BUILD SUCCEEDED
Am I missing something here?
Thanks!
I found the answer. I looked at the source for CodeCampServer and saw a line
<formatter type="Plain" />
and added it to my build script so it looks like this:
<nunit2 haltonfailure="true" failonerror="true" verbose="true">
<formatter type="Xml" extension=".xml" outputdir="${tests.output.dir}" usefile="true" />
<formatter type="Plain" />
<test assemblyname="${test.assembly.file}" />
</nunit2>
and now it displays the details.
Sorry to ask the question prematurely on here, but at least it might help someone in the future if they have a similar problem.
Is there a log file in ${tests.output.dir} ? If so, what if you set usefile to false and type to "Plain"?