Testing shell scripts

Shell scripts serve an import role in a lot of cases, sadly mostly they are also the mother of all legacy pre. Don’t get me wrong, those one-off scripts are important and great, but after they serve there purpose there are really 2 options,ensure that they are well maintained, or get rid of them. Just leaving them around will just make them fight you later on.

This to me means that shell scripts need to be tested like any other software, and even though it does not seem obvious, testing shell scripts is quite easy. My personal tool of choice is assert.sh, which is simple enough and integrates nicely into a testing workflow.

Just a quick example on how this works, the script to test is example.sh
https://gist.github.com/sideshowcoder/7172327.js?file=example.sh

the tests go in example.test.sh
https://gist.github.com/sideshowcoder/7172327.js?file=example.test.sh

and finally running everything is as easy as
https://gist.github.com/sideshowcoder/7172327.js?file=run

And that’s it, a simple test harness around shell scripts.

post-receive hook and branches

Installing a post-receive hook in a git repository can be used to automate a lot
of things, i.e. initiate deployment, precompile a static page, or similar. Using
a hook is quite easy, simply create an executable file named like the hook inside
the hooks folder. So here we go

Init a bare repository

$ mkdir ~/test.git && cd ~/test.git
$ git init --bare

Init a repository

$ mkdir ~/test && cd ~/test
$ git init
$ git remote add origin ~/test.git

Create a hook

$ touch ~/test.git/hooks/post-receive
$ echo 'echo "I am a post receive hook"' >> ~/test.git/hooks/post-receive
$ chmod +x ~/test.git/hook/post-recive

Pushing to the repository means the hook is executed

$ cd ~/test
$ touch foo.txt
$ git add foo.txt
$ git commit -m 'foo'
$ git push origin master
...
remote: I am a post receive hook
...

To do more fun stuff it is sometimes important to know which branch got pushed,
to do this a couple of things are passed to the hook via stdin, so

https://gist.github.com/sideshowcoder/5751530.js

to detect the branch simply check against the $ref.

Happy hooking ;)

Using shell script to deploy automatically

Automatic deployment is essential for any project which is supposed to be worked on and run continuously. Until recently I was using capistrano to do this, but since development seems to have kind of stalled recently I decided to look around for an alternative, which I found in deploy a shell script by TJ Holowaychuk. To check it out I am currently using it to deploy a small Codeignitor project I am working on the side and use to try out new stuff. It really is easy to use and quite flexible due to allowing pre and post deploy hooks as well as automatic tests to check if the deployed version is working, and rollback if it is not. The use with CI is quite simple the only thing todo is to make sure that the right permissions are set which can be easily handled by a post-deploy shell script.

Usage

On the server it creates a simple directory structure with

ENV_ROOT/current
ENV_ROOT/shared
ENV_ROOT/source

in my case for both staging and production. This is done via an initial

deploy [ENV] setup

all following deploys are done by issueing

deploy [ENV]

deploy shell script

Modifications

For some reason some commands were not working for me in Bash ond my Mac so I had to do some small ajustments, I guess it is due to the version of bash I am using being not current maybe. You can find them in my fork