Integrating CPAN into the Perl pipeline

August 14, 2013

Any impediment towards releasing often is a bad thing. As a result, I’ve decided to implement a little bash script that runs the regression tests of a Perl module and upon success, posts the module to CPAN.

# This script is used to automate and move along the CPAN pipeline of
# module creation, and upload to CPAN.

set -e

function die {
        echo "$1"
        exit 1


if [ -e /tmp/$PID ]; then
        die "The directory /tmp/$PID already exists - please remove it before proceding"

if [ ! -e Makefile.PL ]; then
        die "This doesn't appear to be a Makefile.PL controlled module - Exiting"

# Register our cleanup trap
trap "rm -rf /tmp/$PID" EXIT

# Generate a distfile
echo "Generating distfile .."
perl Makefile.PL && make dist && make distclean || die 'Could not create distfile'
DISTFILE=$(ls -1rt *.tar.gz | tail -1)

mkdir -p /tmp/$PID/
mv $DISTFILE /tmp/$PID/
cd /tmp/$PID/

tar zxpvf $DISTFILE
DIRNAME=$( echo $DISTFILE | awk -F".tar.gz" '{print $1}' )
cd $DIRNAME || die "Could not cd to $DIRNAM"

fakeroot perl Makefile.PL && make test || die 'Failed regression tests'

echo "Uploading to PAUSE .."
make dist && cpan-upload $(cpan-upload-options) $(ls -1rt *.tar.gz | tail -1) || die 'Failed to upload to PAUSE'

This way, as soon as I’ve tested and pushed my commits to git, I can promote my new distimage straight to CPAN without the hassle of logging into the web tool. This little extra convenience means more releases, more often.

Discussion, links, and tweets

Follow me on Twitter