The hooks file tells webhook how to handle incoming requests, and which script it should run if it receives an incoming request that matches the pre-arranged criteria, including a shared 'secret'. The scripts can include items such as Telegram notifications.
Install the webhook binary onto server:
Install webhook
sudoaptinstallwebhook
Create a webhooks directory on the system and create a JSON file for the hooks:
Set relevant trigger rules (such as the branch being pushed to). A v4 UUID is used as a 'secret' and can be generated at https://www.uuidgenerator.net/
#!/bin/bash -e# Note the '-e' in the line above. This is required for error trapping implemented below.# Original author Annson Van Doren https://ansonvandoren.com/posts/telegram-notification-on-deploy/# Adapted by AJR July 2022# Repo name on GitHub - **BE SURE TO USE SSH RATHER THAN HTTPS**REMOTE_REPO=git@github.com:EDIflyer/linux-notes.git
# A place to clone the remote repo so the static site generator can build from it; can't use $HOME as runs as rootWORKING_DIRECTORY=/home/<user>/repositories/linux-notes
# Location (server block) where the Nginx container looks for content to servePUBLIC_WWW=/var/www/docs.alanjrobertson.co.uk/html
# Backup folder in case something goes wrong during this scriptBACKUP_WWW=/home/<user>/docs/backup_html
# Domain name so Hugo can generate links correctlyMY_DOMAIN=docs.alanjrobertson.co.uk
# Set up TelegramTOKEN=INSERT_TOKEN_HERE
CHAT_ID=INSERT_CHAT_ID_HERE
BOT_URL="https://api.telegram.org/bot$TOKEN/sendMessage"# Send messages to Telegram botfunctionsend_msg(){# Use "$1" to get the first argument (desired message) passed to this function# Set parsing mode to HTML because Markdown tags don't play nice in a bash script# Redirect curl output to /dev/null since we don't need to see it# (it just replays the message from the bot API)# Redirect stderr to stdout so we can still see an error message in curl if it occurscurl-s-XPOST$BOT_URL-dchat_id=$CHAT_ID-dtext="$1"-dparse_mode="HTML">/dev/null2>&1}# These parameters are passed by the webhook to the script - see the hooks.json `pass-arguments-to-command` sectioncommit_message=$1pusher_name=$2commit_id=$3# If something goes wrong, put the previous verison back in placefunctioncleanup{ERROR=$?echo"A problem occurred. Reverting to backup."rsync-aqz--del$BACKUP_WWW/$PUBLIC_WWWrm-rf$WORKING_DIRECTORY# Use $? to get the error message that caused the failuresend_msg"<b>Deployment of $MY_DOMAIN failed:</b> $ERROR"}# Call the cleanup function if this script exits abnormally. The -e flag# in the shebang line ensures an immediate abnormal exit on any errortrapcleanupEXIT
# Clear out the working directoryrm-rf$WORKING_DIRECTORY# Make a backup copy of current website version# --mkpath flag causes destination directories to be createdrsync-avz--mkpath$PUBLIC_WWW/$BACKUP_WWW# Clone the new version from GitHubgitclone$REMOTE_REPO$WORKING_DIRECTORYsend_msg"<i>Successfully cloned Github repo for $MY_DOMAIN</i><code>Message: $commit_message</code><code>Commit ID: $commit_id</code><code>Pushed by: $pusher_name</code>"# Delete old versionrm-rf$PUBLIC_WWW/*
# Have mkdocs-material generate the new static HTML directly into the public WWW folder# Save the output to send to Telegrammkdocs_response=$(dockerrun--rm-i-v$WORKING_DIRECTORY:/docscustom/mkdocs-materialbuild)cp-r$WORKING_DIRECTORY/site/*$PUBLIC_WWW# Send response to bot as a fenced code block to preserve formattingsend_msg"<pre>$mkdocs_response</pre>"# All done!send_msg"<b>Deployment successful!</b>"# Clear out working directoryrm-rf$WORKING_DIRECTORY# Exit without trapping, since everything went welltrap-EXIT
Now create the folder from named in triggerscript.sh for Github downloads:
Secret:the UUID previously generated and placed in the webhooks.json file
Enable SSL verification: True
Which events would you like to trigger this webhook?: Just the push event
Active: True
You should receive confirmation of a successful ping and a HTTP/200 response. Note there is a tab called Recent Deliveries in the Github webhook management screen that shows the status of recent webhook messages and lets them be resent.
We now need to install git on the server and also use an SSH key to connect to our GitHub account.
Setup SSH key on server and copy to GitHub account
cd~
ssh-keygen-ted25519-C"<EMAIL ADDRESS>"
Enter github_sync when prompted for filename to save the key. This will then create a private key called github_sync and a public key called github_sync.pub
Then copy private key to the .ssh sub-directory in home directory:
Once the GitHub pulic key fingerprint is accepted there should be a confirmation message of Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Root vs standard user
Remember the triggerscript will be run as root user, therefore in addition to the above you need to copy these credentials across to the root user otherwise you will get an authentication error from Github when trying to pull down the repository.