Posted on Leave a comment

Custom build script for GIT based projects

Git Logo

When managing smaller GIT based projects I find it helpful to opt for custom build scripts rather than having to deal with the overhead of setting up proper Jenkins (or-likewise) runtime environments.

In order to deal with this need I’ve created a simple build script that creates a compressed TAR file based on committed files.

Furthermore, since I use version numbers for e.g. JavaScript builds it’s always nice to have automatic file inclusion/exlusion based on this number, say, read from a config file.

In this particular example I want to read the version number from a variable called MY_VERSION in file config/base.php and include a special JavaScript build-folder in the target compressed TAR file. In addition, I only want to include files in the src-folder, as well as manually include other folders.

So, here it is:

#!/bin/sh
#
# REQUIREMENTS:
# - git
# - php
# - sed
# - xargs
# - tar
#

#(absolute) path to PHP, or simply 'php' if it is in the PATH
PHP_PATH="php"

#release number should be read from config/base.php
PHP_REQUIRE="require('$(pwd)/config/base.php');"
RELEASE_NUMBER=`$PHP_PATH -r "$PHP_REQUIRE echo MY_VERSION;"`

#hold list of files to be added to release tar.gz
TMP_SRC_FILE_LIST=$(pwd)/TMP_RELEASE_INCLUDE

#get list of git's tracked files
git ls-files > $TMP_SRC_FILE_LIST

#filter only SRC directory
sed -i '/^src./!d' $TMP_SRC_FILE_LIST

#manually include additional folders of your choice
sed -i '/^src\/js\/folder1/d' $TMP_SRC_FILE_LIST
sed -i '/^src\/js\/folder2/d' $TMP_SRC_FILE_LIST

#add RELEASE directory only
`echo "src/js/release/$RELEASE_NUMBER" >> $TMP_SRC_FILE_LIST`

# enclose lines in "" to enable spaces in file and folder names
sed -i 's/^/"/g' $TMP_SRC_FILE_LIST
sed -i 's/$/"/g' $TMP_SRC_FILE_LIST

#create compresse TAR
cat $TMP_SRC_FILE_LIST | xargs tar czf release_$RELEASE_NUMBER.tar.gz

#clean up
rm $TMP_SRC_FILE_LIST

That’s it – Feel free to adjust it to your needs.