Last active
March 20, 2019 03:40
-
-
Save DrPaulBrewer/63a44633f8014b94989f5152d4797f4b to your computer and use it in GitHub Desktop.
git-hook for on-push nginx configuration, testing, and alerting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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