Table Of Contents

Previous topic

Tools

Next topic

Expand tabs

This Page

Ghetto-CI, continuous integration script

Continuous integration server, ghetto style

What it is

Ghetto-CI is a Python script in 145 statements fullfilling your dirty continuous integration needs.

Source code (one file, only 145 statements) is on Github and for your convenience the script is bundled in VVV package on PyPi.

What it does

  1. The script must be run on your server periodically
  2. Runs svn update on a source folder (VCS backend easy to customize)
  3. Checks if there are new commits since the last run
  4. Run tests if the source code in fact was changed
  5. See if the tests status since the last run has changed from success to failure or vice versa
  6. Send email notifications to the team that now fecal matter impacts the rotary ventlidation device

Why to use

To improve the quality and cost effectiveness of your little software project, you want to detect code changes breaking your project [automated tests].

You might want to do this without installing 48 MB of Java software on your server. On the other hand, very good SaaS oriented alternatives are tied to public Github repositories. Homebrew shell scripts for tasks like this are nice, but no one wants to read or touch shell scripts written by others.

Ghetto-CI script is mostly self-contained, easy to understand, easy to hack into pieces, for your very own need. It is a solution that scales down. Just toss it on a corner of a server and it will churn happily and keep its owners proud. Ghetto-CI does not you give fancy web interface, statistics, bling bling or even a pony. However, it tells when someone breaks something and it is time for team building via blanket party.

Installation

Note

As a prerequisitement you need a working Python 3 command installed on your operating system with virtualenv package. For detailed instructions see VVV installation manual

Note

If you don’t feel eggy you can just grab the self-contained source file as long as you have plac package also installed for your Python.

Create Python 3 virtualenv and run Ghetto-CI script using Python interpreter configured under this virtualenv:

# We install GhettoCI directly under our UNIX user home folder
cd ~
virtualenv -p python3 vvv-venv
source vvv-venv/bin/activate
pip install vvv

# ghetto-ci now lives in vvv-venv/bin/ghetto-ci

# Running the script, using the Python environment prepared 
# to see that everything works (source command above 
# has added this to PATH)
ghetto-ci -h

Usage

You need to prepare

  • A software repository folder. This must be pre-checked out Subversion repository where ghetto-ci can run svn up command.
  • A command to execute unit tests and such. This command must return process exit code 0 on success. If you don’t bother writing tests, low end alternative is just lint and validate your source code.
  • A file storing the test status. Ghetto-CI status file keeps track whether the last round or tests succeeded or failed. You’ll get email reports only when the test status changed - there is little need to get “tests succeeded” email for every commit.
  • Email server details to send out notifications. Gmail works perfectly if you are in short of SMTP servers.

Example of a command for running continuous integration against /my/svn/repo checkout where bin/test command is used to run the unit tests:

# Will print output to console because email notification details are not given
ghetto-ci /my/svn/repo /tmp/status-file.ci "cd /my/svn/repo && bin/test"

If the tests status have changed since the last run, or the running fails due to internal error, the command outputs the result. Otherwise command outputs nothing. Exit code 0 indicates that test succeeded.

Then just make Ghetto-CI to poll the repository in UNIX cron clock deamon. Create a dummy UNIX user which can checkout and pull updates on the source code. Create file /etc/cron.hourly/continuous-integration-tests which will hourly run the tests (Ubuntu example):

#/bin/sh
sudo -i -u yourunixuser "ghetto-ci /my/svn/repo /tmp/status-file.ci 'cd /my/svn/repo && bin/test'

Naturally the command to launch the tests is specific to your software project.

On Windows you can accomplish this using any automator provided by your operating system vendor.

Tips

You might to use -force -alwaysoutput arguments with test runs.

You can also evaluate against UNIX command true and false to e.g. test email output:

ghetto-ci -force -alwaysoutput ...email settings here... /repo /tmp/status-file.ci true

Complex usage example

Below is a real life example how you define one shell script, again triggered by Cron job, to poll several SVN repositories which have different tests to run.

continuous-integration.sh:

#!/bin/sh
#
# Example ghetto-ci integration for Plone buildout and custom add-ons.
# Using dummy gmail account for outgoing messages.
#
# Install VVV under buildout
#
# Run this file in buildout main folder:
#
# cd ~/mybuildoutfolder
# src/my-repo/continuos-integration.sh 
#
#

# The list of persons who will receive resports of test status changes
RECEIVERS="person@asdf.com, person2@asdf.com, person3@example.com"

# Ghetto-CI template command which is run against multiple repos / multiple test commands
# We use localhost 25 as the SMTP server -> assume your UNIX server has postfix 
# or something configured... could be gmail.com servers also here
GHETTOCI="vvv-venv/bin/ghetto-ci         -smtpserver smtp.gmail.com         -smtpport 465         -smtpuser mailer@gmail.com         -smtppassword OMGITISFULLofKITT3NS         -receivers "$RECEIVERS"         -envelopefrom "Continuous integration service <mailer@gmail.com>"         -smtpfrom mailer@gmail.com"

# Note that SVN revision info is folding down in the folders
# so you can target tests to a specific SVN repo subfolder

# Note: eval needs to be used due to shell script quotation mark fuzz 

# See that buildout completes (no changes in the external environment, like someone accidentally 
# publishing broken packages on PyPI). We actually place buildout.cfg under this SVN repo
# and then just symlink it
eval $GHETTOCI src/my-repo buildout.ci 'bin/buildout'

# Run tests against hospital product
eval $GHETTOCI src/my-repo/Products.Hospital hospital.ci "bin/test -s Products.Hospital"

# Run tests against patient product
eval $GHETTOCI src/my-repo/Products.Patient patient.ci "bin/test -s Products.Patient" 

Internals

plac rocks for command line parsing, especially with Python 3.

The code is pylint valid - and beautiful, Pythonic.

Future tasks

To make this script even more awesome, the following can be considered

  • Using some Python library as the abstraction layer over different version control systems
  • Using more intelligent Python library for the notifications: have email, IRC and Skype notifications (How Skype bots are built nowadays?)
  • Use a proper emailing library. I still believe it is easier to configure one GMail account for SMTP purposes instead of Postfix or Exim. Also GMail nicely collects outgoing messages to log even if email delivery has temporary problems.
  • I would be happy if someone told how to change -smtpport styles options to –smtp-port with plac
  • I would be also happy if someone shows how to tame shell script quotation marks properly
  • All tips to make Python source even more beautiful welcome

Source and credits

Ghetto-CI lives on Github, in VVV project.

Mikko Ohtamaa, the original author (blog, Twitter)

Licensed under WTFPL.

Ghetto style?.