Wednesday, August 5, 2015

Shell script fun - pull, compile, and display errors...

I am working on a project at work that has multiple project dependencies, and people from other time zones constantly updating those projects, so it is helpful to stay in sync so I can avoid merge issues.

I wrote a shell script to pull and build the projects. We are using maven for our projects, so a nice side effect is that the maven build outputs "SUCCESS" or "FAILURE" for the project build. I just grep for "FAILURE" (or "error"), pipe the result to a text file, and then check each text file to see if it is 0 bytes or not.  If the file has data, then I print a build error message, cat the file, and then delete it. I output the failed file in red if the build failed.

Here is a simple version of the script:

#!/bin/sh
source ~/.bash_profile

export WORK_HOME=~/dev/source

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
#echo "${red}red text ${green}green text${reset}"

grep_file()
{
  if [ -s "$WORK_HOME/$2" ]
  then
    echo "${red}******** $1 had errors!${reset}"
    echo ""
    echo "${red}"
    cat $WORK_HOME/$2
    echo "${reset}"
  else
    rm -f $WORK_HOME/$2
  fi
}

do_build()
{
  echo ""
  echo "${green}Updating $2.${reset}"
  echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
  cd $WORK_HOME/$2
  git pull
  if [ "$1" == "mci" ]
  then
    mvn clean install 2>&1 | grep FAILURE | grep -iv failures | grep -v grep > $WORK_HOME/$3
  else
    mvn clean package 2>&1 | grep FAILURE | grep -iv failures | grep -v grep > $WORK_HOME/$3
  fi
  echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
}

do_activator_build()
{
  echo ""
  echo "${green}Running activator build for $1.${reset}"
  echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
  cd $WORK_HOME/$1
  git pull
  activator update
  activator compile | grep -i error | grep -v grep > $WORK_HOME/$2
  echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
}

# project 1
do_build "mci" "project1" "proj1_failed.txt"

# project 2
do_activator_build "project2" "proj2_failed.txt"

echo ""
echo "Checking for failures..."
echo ""

grep_file "project1" "proj1_failed.txt"
grep_file "project2" "proj2_failed.txt"
echo "Done."


Here is the output:


Updating project1.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Already up-to-date.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Running activator build for project2.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Already up-to-date.
[info] Loading project definition from /Users/user/dev/source/project2
[info] Updating {file:/Users/user/dev/source/project2/}project2...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[success] Total time: 16 s, completed Aug 5, 2015 11:06:25 PM
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Checking for failures...

Done.

No comments:

Post a Comment