Thursday, 19 April 2012

Using BUILD_LOG_REGEX in jenkins email notification

Jenkins provide 'Email-ext' plugin, which allows to configure every aspect of email notifications.
One of my requirement is to send in mail the build package URL, which is already getting displayed in console output.

I found a token 
${BUILD_LOG_REGEX, regex, linesBefore, linesAfter, maxMatches, showTruncatedLines, substText, escapeHtml, matchedLineHtmlStyle} - Displays lines from the build log that match the regular expression. 

Here is example usage of it
${BUILD_LOG_REGEX, regex="^Build package URL:", showTruncatedLines=false}

This will extract the line starting with "Build package URL:" from the build log and send it through mail.

Wednesday, 18 April 2012

Using unix find Command to delete files older than a year

One of the boring task is to clean up data when ever disk usage exceeds the threshold limit. Among the many ways do it, here is the simple usage of Unix find command to delete files older than a year.

   find . -type f -mtime +365 -delete

Where -mtime +365 suggests to list files older than 365 days.
or
  find . -type f -atime +365 -delete

Where -atime stands for access time which is when the file was last read.


Some more examples

find . -mtime 0 # find files modified between now and 1 day ago 
# (i.e., within the past 24 hours)


find . -mtime -1 # find files modified less than 1 day ago 
# (i.e., within the past 24 hours, as before)

find . -mtime 1 # find files modified between 24 and 48 hours ago 

find . -mtime +1 # find files modified more than 48 hours ago

find . -mmin +5 -mmin -10 # find files modified between 
# 6 and 9 minutes ago


Usage of -exec option 
Here is the simple use case.
I want to find all the files with the name 'latest' in a directory and I want to run 'ls -l' command on the result, since I want to know whether 'latest' is a directory or soft link.
find . -maxdepth 2 -name latest -exec ls -l '{}' \;
Here the 
  -maxdepth option restrict the search to be conducted 2 levels of directory hierarchy.
The syntax '{}' \; is mandatory, which is tedious.

Tuesday, 17 April 2012

Using wget to download files recursively from https site

wget is a command used mostly in Linux to download files from web links (http, https or ftp). To download a file, a simple invocation of wget like
  wget http://mylibrary.com/build-kits/rel-2.7.1/prod1/prod1-2.7.1.10.tar
will download prod1-2.7.1.10.tar to the current directory.

To download set of files (ex .tar files) from a secured https site, we need to provide several options to it
  wget --no-check-certificate -r -l1 --no-parent -A.tar http://mylibrary.com/build-kits/rel-2.7.1/prod1/
where
  --no-check-certificatedon't validate the server's certificate
  -r or --recursive: specify recursive download
  -l1 or --level=NUMBER: maximum recursion depth (inf or 0 for infinite).
 --no-parentdon't ascend to the parent directory
 -A or --accept=LIST:  comma-separated list of accepted extensions.
 -nd or --no-directories: don't create directories.

Thursday, 5 April 2012

Ant script to replace tokens (VERSION) in a file (build.properties)

Version of a product ever changes for main releases, SP releases, hotfix releases, etc. We can't hard-code version value in source files. One of the effective solution to tackle this is to use tokens in the place of version values and replace it before the start of build. The most common tool used to make string replacements are scripts written in perl/shell. Though scripts works well, but why to depend on scripts which are closely tied with Unix systems, to perform token replacement operations on platform independent Java build systems. 


 Java builds are usually managed through Ant/Maven. If perl/shell scripts are not stored along with Java build system, then if anyone checkouts only java code from source control, there build may fail due to improper version characters.


Here is an illustration of performing token replacement using Ant script.


Input file: build.properties file with tokens for various version formats like VERSION_UNDERSCORE, VERSION_NODOT, etc

$ cat build.properties
# KMJC Version properties
version=VERSION_NODOT
dot.version=VERSION_DOUBLE
dot.version.double=VERSION_DOUBLE
dot.version.string=VERSION_DOUBLE
underscore.version=VERSION_UNDERSCORE


After string replacement it should become like


$ cat build.properties
# KMJC Version properties
version=40
dot.version=4.0
dot.version.double=4.0
dot.version.string=4.0
underscore.version=4_0


Ant script

$ cat build.xml
<project name="begin" default="must" basedir="." >
        <condition property="noVersion" value="true">
                <not>
                        <isset property="VERSION"/>
                </not>
        </condition>
        <target name="checkVersion" if="noVersion">
                <echo message="ERROR: VERSION property not defined. Supply it through -DVERSION argument" />
                <echo message="ant -DVERSION=version-number" />
                <echo message="EX: ant -DVERSION=4.0" />
                <fail message="version not supplied"/>
        </target>
        <target name="must" depends="checkVersion" >
                <echo> VERSION=${VERSION} </echo>
                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                <propertyregex property="VERSION_UNDERSCORE" input="${VERSION}" regexp="\." replace="_" global="true" />
                <propertyregex property="VERSION_NODOT" input="${VERSION}" regexp="\." replace="" global="true" />
                <echo> VERSION_UNDERSCORE=${VERSION_UNDERSCORE} </echo>
                <echo> VERSION_NODOT=${VERSION_NODOT} </echo>
                <replace file="build.properties" token="VERSION_DOUBLE" value="${VERSION}"/>
                <replace file="build.properties" token="VERSION_UNDERSCORE" value="${VERSION_UNDERSCORE}"/>
                <replace file="build.properties" token="VERSION_NODOT" value="${VERSION_NODOT}"/>
        </target>
</project>

Usage:
  1) If VERSION Variable is not defined. It will fail
      $ ant
Buildfile: C:\cygwin\home\guruss1\ant\build.xml


checkVersion:
     [echo] ERROR: VERSION property not defined. Supply it through -DVERSION argument
     [echo] ant -DVERSION=version-number
     [echo] EX: ant -DVERSION=4.0


BUILD FAILED
C:\cygwin\home\guruss1\ant\build.xml:11: version not supplied

2) Proper usage
   $ ant -DVERSION=4.1
Buildfile: C:\cygwin\home\guruss1\ant\build.xml

checkVersion:

must:
     [echo]  VERSION=4.1
     [echo]  VERSION_UNDERSCORE=4_1
     [echo]  VERSION_NODOT=41

BUILD SUCCESSFUL


Requirement: propertyregex task needs ant-contrib.jar. Download it and place it under lib directory of Ant installation and add the line <taskdef resource="net/sf/antcontrib/antcontrib.properties"/> to build.xml file
 Alternatively ant-contrib.jar can also be placed in directory of you choice and add line <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${ant-contrib.jar}" /> to build.xml