Skip to content

Instantly share code, notes, and snippets.

@UndefinedOffset
Last active July 9, 2021 10:19
Show Gist options
  • Save UndefinedOffset/282bcd3fe376c9f525ee to your computer and use it in GitHub Desktop.
Save UndefinedOffset/282bcd3fe376c9f525ee to your computer and use it in GitHub Desktop.
A simple git deploy hook for notifying New Relic of deployments, it assumes that the base folder name of the repository is the application name. To set git to use this script you simply add this file into your .git/hooks folder as "post-merge".
#!/bin/bash
#New Relic API Key see https://docs.newrelic.com/docs/apis/rest-api-v2/requirements/api-key#creating
NR_API_KEY="INSERT_API_KEY_HERE"
#Find New Relic App Name, if your top level folder is not the name of your application change the below two lines to simply NR_APP_NAME="MY_APP_NAME".
NR_APP_NAME=$(git rev-parse --show-toplevel)
NR_APP_NAME=$(basename "$NR_APP_NAME")
#Get the current git branch
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
#Get the head reference
GIT_HEAD_REVISION=$(git rev-list --walk-reflogs --max-count=1 "$GIT_BRANCH")
#Get the previous head reference
GIT_PREV_REVISION=$(git rev-list --walk-reflogs --skip=1 --max-count=1 "$GIT_BRANCH")
#Get the git reference logs
GIT_LOG_CONTENT=$(git log "$GIT_PREV_REVISION...$GIT_HEAD_REVISION")
echo "Notifying New Relic of Deployment: $GIT_PREV_REVISION...$GIT_HEAD_REVISION"
#Notify New Relic of the Deployment
curl -s -S -H "x-api-key:$NR_API_KEY" -d "deployment[app_name]=$NR_APP_NAME" -d "deployment[description]=Deployment through Git" -d "deployment[revision]=$GIT_PREV_REVISION...$GIT_HEAD_REVISION" --data-urlencode "deployment[changelog]=$GIT_LOG_CONTENT" https://api.newrelic.com/deployments.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment