Skip to content

Instantly share code, notes, and snippets.

@DrPaulBrewer
Last active March 20, 2019 03:40
Show Gist options
  • Save DrPaulBrewer/63a44633f8014b94989f5152d4797f4b to your computer and use it in GitHub Desktop.
Save DrPaulBrewer/63a44633f8014b94989f5152d4797f4b to your computer and use it in GitHub Desktop.
git-hook for on-push nginx configuration, testing, and alerting
#!/bin/bash
# originally /root/nginx-alert.sh
APK='your-mailgun-key' # replace with mailgun key
FROM='Nginx Alert <[email protected]>'
TO='you@email' # replace with recipients
# SUBJECT, TEXT is exported from caller
curl -s --user $APK \
https://api.mailgun.net/v3/mg.econ1.net/messages \
-F from="$FROM" \
-F to="$TO" \
-F subject="$SUBJECT" \
-F text="$TEXT"
# this is the post-receive hook to use in the remote bare git repo ./hooks/post-receive on the remote nginx web server
TESTLOG=/tmp/$$
NG=/usr/sbin/nginx
NGINXCONF=/etc/nginx
sudo GIT_WORK_TREE=$NGINXCONF git checkout -f master
sudo chown -R root:root /etc/nginx
if sudo $NG -q -t >$TESTLOG 2>&1
then
sudo $NG -s reload
SUBJECT="Success: Updated Nginx Configuration"
else
SUBJECT="Failure: Blocked Nginx Configuration"
fi
echo ----------------------- >> $TESTLOG
GIT_WORK_TREE=$NGINXCONF git log --full-diff HEAD~1..HEAD -p >> $TESTLOG
echo ----------------------- >> $TESTLOG
echo ---- ENV variables ---- >> $TESTLOG
printenv >> $TESTLOG
sudo env SUBJECT="$SUBJECT" TEXT="$(cat $TESTLOG)" /root/nginx-alert.sh
rm -f $TESTLOG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment