Tuesday, October 20, 2015

A Simple Shell Script to Generate a Manual tar.gz Build File of Your NodeJS App

Hello my fellow Code Junkies...

Who doesn't love to Shell Script??


In most of my current projects I use Dokku, which let's me deploy new versions of my NodeJS app to Production using a git push. If you have not heard of Dokku (Which is basically a Heroku Clone), you should as its pretty cool.

But I also have a bunch of Projects where I need to create a manual build of my App and then copy it to my server, unpack it and run it. I'm sure many of you do this as well.

To streamline this manual build process, I include a standard Shell Script (.sh) file in all my Projects. When you run it, it reads in your package.json file, extracts the "name" and "version" values and creates a .tar.gz build for you (thats a GZIPed Tar File) and puts it in a builds/ directory.

So basically as you work on a new version of your App (and you increment your version number in package.json) and you are ready to deploy it, you just run the Shell Script and it generates a tar.gz file for you with the naming convention appname-version.tar.gz and puts it in a builds/ directory.

e.g. myapp-0.0.1.tar.gz

To keep your build clean, the script ignores the "node_modules/" and "builds/" folders.

Copy the Shell Script below and include it in your Project and then run it in your Terminal like so:

. make_build.sh

... or better yet, include it in your package.json under the "scripts" section like so:

"scripts": {

    "start": "",

    "test": "",

    "makebuild": ". make_build.sh"

}

... and then run it like so from terminal:

npm run makebuild

Here is the source for make_build.sh:

#!/bin/bash
# Generate a tar.gz build using name and version from package.json
# In your app root you need a builds/ directory, this is where your tar.gz file will be placed. Or change this location in line 25
# The script will ignore your node_modules and builds folders

# get app name from package.json
name=`grep "name" package.json | sed 's/,//g'`
name=`sed 's/"name"://g' <<< $name`
name=`sed 's#"# #g' <<< $name`

# get app version from package.json
version=`grep "version" package.json | sed 's/,//g'`
version=`sed 's/"version"://g' <<< $version`
version=`sed 's#"# #g' <<< $version`

# append them both and remove all whitespace and append .tar.gz to the file name string
filename=$name"-"$version
filename=`sed 's/ //g' <<< $filename`
filename+='.tar.gz'

echo "--------------------------"
echo "lets make a new tar.gz"
echo "--------------------------"

# create the new tar.gz in your builds/ folder (exclude node_modules folder)
tar -zcvf "builds/"$filename --exclude "node_modules/" --exclude "builds/" *

echo "--------------------------"
echo "created new build tar.gz with filename = "$filename
echo "--------------------------"


Or grab the Gist from here if your prefer:
https://gist.github.com/newbreedofgeek/db2b3fc1fc0cac592173


Happy Coding!

No comments:

Post a Comment

Fork me on GitHub