DEV Community

Dr Nic Williams
Dr Nic Williams

Posted on

./bin/commits-since-last-production-deploy

When you work in a team, with multiple people taking on responsibility for moving work into production, it can mean that you are never 100% confident whether the current main/master branch has been fully deployed or not. What's running on prod?

Or rather, what's NOT running on prod yet?

Can I deploy my new commits merged into main through to production because they are all mine, and mine alone?

Or are there some gnarly new contributions that have been foisted on to the shared work branch, and you thoroughly want to review them before you take responsibility for sending them off into production, and potentially causing problems for customers, support team, and then the dev team in a few minutes or hours?

I wrote a script to that I have started adding to every Heroku-hosted project ./bin/commits-since-last-production-deploy that is shown below and hosted on a Gist.

To compare the current branch against a specific Heroku application:

./bin/commits-since-last-production-deploy -a myapp-stg
Enter fullscreen mode Exit fullscreen mode

Any additional args are forwarded on to the git log command, such as --oneline:

./bin/commits-since-last-production-deploy -a myapp-stg --oneline
Enter fullscreen mode Exit fullscreen mode

If you have a default heroku remote so you can git push heroku HEAD:main like a wild thing, then you can drop the -a flag:

./bin/commits-since-last-production-deploy
Enter fullscreen mode Exit fullscreen mode

The script is below, and is hosted on a Gist

#!/bin/bash

help() {
  echo "Usage: ./bin/commits-since-last-production-deploy [-a myapp ] [ -h ]"
  exit 2
}

if ! command -v heroku &>/dev/null; then
  echo "Install 'heroku' CLI"
  exit 1
fi

if ! command -v jq &>/dev/null; then
  echo "Install 'jq' CLI"
  exit 1
fi

while getopts 'a:h' opt; do
  case "$1" in
  -a)
    appname="$2"
    shift 2
    ;;
  -h | --help)
    help
    exit 2
    ;;
  esac
done

last_deploy_result=$(heroku releases ${appname:+-a $appname} --json | jq -r ".[0].description")

if [[ $last_deploy_result =~ ^(Deploy (.*)) ]]; then
  sha="${BASH_REMATCH[2]}"
  git log "$sha"..HEAD "$@"
else
  echo "Last deploy to production did not succeed"
  echo "--> $last_deploy_result"
  exit 1
fi
Enter fullscreen mode Exit fullscreen mode

Fundamentally, this runs heroku releases on your app, gets the Git SHA-1 that was deployed, then compares it against the list of commits on the current branch.

If you see no output, then there are no new commits that are not yet on production. You are free. Production is all yours. BWAHAHA.

Top comments (1)

Collapse
 
davidteren profile image
David Teren

Great solution Doc. Somehow I can relate to this being useful 😉