Continuous Integration/script

From CDOT Wiki
Revision as of 17:55, 8 April 2011 by Minooz (talk | contribs)
Jump to: navigation, search
#!/bin/bash

# script for automating synchronization of 
# Internal Repoand External Repo of NexJ 
# CDOT - 22/Oct/2010

#changed to /usr/bin/rm for using in cmd
PATH=/usr/bin:$PATH

nexj_home="D:/cygwin/data/hudson/nexj_express"
patch="${nexj_home}/patches"
src="${nexj_home}/src"
doc="${nexj_home}/doc"
ANT="${nexj_home}/java/apache-ant-1.7.1/bin/ant"
tmpInternal="${nexj_home}/tmpInternal"
tmpExternal="${nexj_home}/tmpExternal/core"
hgExt="D:/cygwin/home/NexJExpress/External"
hgExtURL="https://ossbuild:go_express@cvs-ext.nexj.com/oss/core"
hgIntDir="${nexj_home}/hg.nexj.com/core"
hgIntURL="D:/cygwin/home/NexJExpress/Internal"

# add this info to the name of report file
reportDate=`date +%Y-%m-%0e-TIME-%H-%M`;
# add this info to the name of build and test log files
buildDate=`date +%Y-%m-%0e`;
# flag to check if there was any error for results in Hudson
error=0


# go to the working directory and clean up everything
cd ${nexj_home} 2> /dev/null
if [ $? != 0 ]; then
    echo "No such directory ${nexj_home}"
    exit 1
fi


#Remove temporary repositories
if [ -d tmpExternal ]; then
	rm -r tmpExternal
	echo "tmpExternal removed"
fi


##########################
## Working with Internal
##########################
cd $hgIntDir 2> /dev/null
if [ $? != 0 ]; then
	mkdir $hgIntDir
	hg clone -b default $hgIntURL $hgIntDir
	cd $hgIntDir
fi


# Get latest code from internal
hg pull -u -b default 2> /dev/null
if [ $? != 0 ]; then
    echo "hg pull failed for hgIntURL ${hgIntURL}"
    exit 1
fi


# Get latest internal revision number
revInternal=`hg log -r tip -b default --template '{node}'`
echo "Internal Rev $revInternal"
if [[ $revInternal =~ "+" ]]; then
	echo "========================================="
	echo "ERROR! Revision $revInternal is not commited yet" 
	echo "========================================="
	exit 1
fi


##########################
## Working with External
##########################
# Get the latest code from external repo
mkdir -p ${tmpExternal} 2> /dev/null
if [ $? != 0 ]; then
    echo "could not create ${tmpExternal}"
    exit 1
fi


hg clone ${hgExt} ${tmpExternal}    # later will be hg clone ${hgExtURL} ${tmpExternal}
if [ $? != 0 ]; then
	echo "Clone was not successful"
	exit 1
fi


# extract the revision number of latest changeset commited to the External Repo
cd ${tmpExternal}   
revExternal=`hg log -r tip | grep rev `
if [ $? != 0 ]; then
    echo "There is no repository availble at ${hgExt}"
    exit 1
fi


# extract the Internal revision number from summary
revExternal=${revExternal#*revInternal:}
echo "External Rev $revExternal"


# report file name is created based on date
report="Report-(${reportDate})"


echo -e "\n" >> $doc/$report.txt
if [ $? != 0 ]; then
    echo "2: No such file $doc/${report}.txt"
    exit 1
fi


echo "REPORT FOR $reportDate" >> $doc/$report.txt
echo "========================================" >> $doc/$report.txt


# if there is no  new change set, end the script
if [ ${revInternal} == ${revExternal} ]; then
    echo "There are no new changesets." >> $doc/$report.txt 
	echo "========================================" >> $doc/$report.txt 
	cat $doc/$report.txt
    exit 0
fi


cd ${nexj_home}
# get the list of the latest changesets since last time committing to the external repo
revList=`hg log ${hgIntDir} -r ${revExternal}: --template '{node}\n'`
if [ $? != 0 ]; then
	echo "No repository available at ${hgIntDir}"
	exit 1
fi
for rev in $revList; do
	if [ ${rev} != ${revExternal} ]; then
		echo "Revision of hgIntDir is $rev"


		cd ${hgIntDir}
		exPatch=$patch/patch${rev}.patch
		# if there is any patch with $rev number, remove it
		if [ -e ${exPatch} ]; then
			rm ${exPatch}
		fi


		hg export -r ${rev} > ${exPatch}
		if [ $? != 0 ]; then
			echo "export was not successful"
			exit 1
		fi
		
		# extract hgIntDir's tip summary and add rev number 
		# to the commit summary for tmpExternal Repo
		sumHgIntDir=`hg log -r $rev --template '{desc}\n'`
		if [ $? != 0 ]; then
			echo "No repository available"
			exit 1
		fi
		sumTmpExternal="${sumHgIntDir}-revInternal:${rev}"
		
		cd $tmpExternal
		if [ $? != 0 ]; then
			echo "2: No such directory $tmpExternal"
			exit 1
		fi		
		
		# applying the patch to tmpExternal Repo
		hg import -m "$sumTmpExternal" ${exPatch} 
		if [ $? != 0 ]; then
			echo "import of ${exPatch} was not successful"
			exit 1
		fi
		
		cp "${src}/build.properties" "${tmpExternal}/work/core/build/build.properties" 2> /dev/null
		if [ $? != 0 ]; then
			echo "2: No such file or directory"
			exit 1
		fi
		######################
		#Build the imported changeset
		######################
		cd ${tmpExternal}/work/core/build
		if [ $? != 0 ]; then
			echo "2: No such directory ${tmpExternal}/work/core/build"
			exit 1cd ../
		fi
		# build log's name is created besed on the rev number and date
		dateLog="Rev${rev}(${buildDate})"
		$ANT -logfile $doc/build$dateLog.txt app.ear 


		#Check if the build was successful, push it to the hgExtURL
		grep -w "BUILD SUCCESSFUL" $doc/build$dateLog.txt 2>/dev/null
		if [ $? -eq 0 ]; then
			echo "BUILD & TEST SUCCESSFUL - Revision $rev" >> $doc/$report.txt
			# if build, test and applying the patch was successful then push it
			echo "PATCH APPLIED SUCCESSFULY - Revision $rev" >> $doc/$report.txt
			hg push
			error=0
		else 
			# if build was not successful apply the created patch as a Broken changeset to the tmpExternal
			echo "BUILD FAILED - Revision $rev" >> $doc/$report.txt
			error=1
		fi
	fi
done


# Display the report
echo "========================================" >> $doc/$report.txt 
echo -e "\n" >> $doc/$report.txt
cat $doc/$report.txt


exit $error