Run tests inside your deploy script

How to verify that a certain deploy of your app was successful? Sure, tests!

You don't have to run the full test suite of your app – it can be a single test with a checklist for your server non-default setup:

  • the upload directory exists and has proper permissions
  • nginx is configured properly to return CORS headers for given URLs

And the easiest way to incorporate tests into deploy routine is to run tests right inside your deploy bash script:

cd /var/www/my-website-working-copy

# call `git fetch`, `composer install` and the rest of your build commands here

set +e
PHPUNIT_RESULT=`vendor/bin/phpunit`
set -e
if [[ ${PHPUNIT_RESULT} =~ FAILURES ]]; then
    echo "Tests FAILED!";
    printf '%s\n' "$PHPUNIT_RESULT";
    # send notification to Slack or email
    exit 1;
else
    echo "Tests PASSED!"
fi

Do not forget you have to have phpunit installed. Probably your composer install invocation includes --no-dev parameter which prevents installing dev dependencies.

Laravel Forge notes

The script above works perfect inside Laravel Forge.

If the tests fail you will see Forge's generic error message. A button next to this message shows logged output of the deploy script. At the end of this log you will find phpunit output and can guess what went wrong. Anyway "View latest deployment log" button lets you see the same log.

Laravel Forge is configured to exit deploy script on first error. That's why set +e and set -e commands wrap phpunit invocation.

Send notification to Slack

You can use curl to trigger a webhook as seen in Slack docs. But I personally find slacktee command more convenient for such tasks. The snippet below sends PHPUNIT_RESULT to stdout and then sends the same to slacktee.

printf '%s\n' "$PHPUNIT_RESULT" | slacktee -t "Tests FAILED for my-website.com" -a danger --config ./.slacktee



You might also like this deployment related post: How long your web app is down for deployment?

Share on Twitter    Share on Google+

How long your web app is down for deployment?

I usually deploy the web app I work on several times a week. Sometimes I do that even twice a day. The web app is not business critical and it is okay to have downtimes while the web app is being deployed. But having the product unavailable for the customers still made me feel bad and less than 1 minute of each deploy seemed lasting for soooo long.

"You Can't Improve What you Don't Measure". So before improving the deployment process itself I decided to measure what exact time does each downtime take. Here was the simplest solution for my deploy bash script:

timeDown=$(date -u +"%s")

# php artisan down
# the rest of build commands here
# php artisan up

timeUp=$(date -u +"%s")
timeDiff=$(($timeUp-$timeDown))
echo "Application was down during $timeDiff seconds."

If you are (like me) a happy customer of Laravel Forge you can do this measurement easily!

  1. Update your Deploy Script to include the missing bash commands from the listing above.
  2. After each deployment see the measured time by clicking View Latest Deployment Log button on a Site Details page.
Share on Twitter    Share on Google+
Max
Pshenichnikov