DEV Community

Dr Nic Williams
Dr Nic Williams

Posted on


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:

Enter fullscreen mode Exit fullscreen mode

The script is below, and is hosted on a Gist


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

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

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

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

if [[ $last_deploy_result =~ ^(Deploy (.*)) ]]; then
  git log "$sha"..HEAD "$@"
  echo "Last deploy to production did not succeed"
  echo "--> $last_deploy_result"
  exit 1
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)

davidteren profile image
David Teren

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