Last active
January 24, 2021 10:10
-
-
Save piorus/0402f7239b1649bc9fefe73b570c009a to your computer and use it in GitHub Desktop.
GitHub webhooks install script
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 | |
echo -n "Where are your websites stored? [/var/www]: " | |
read WEBSITES_ROOT_DIRECTORY | |
if [ -z "$WEBSITES_ROOT_DIRECTORY" ] | |
then | |
WEBSITES_ROOT_DIRECTORY="/var/www" | |
fi | |
echo -n "git user (user that SSH key was added to the GitHub) [root]: " | |
read USER | |
if [ -z "$USER" ] | |
then | |
USER="root" | |
fi | |
echo -n "GitHub production branch [main]: " | |
read GH_MAIN_BRANCH | |
if [ -z "$GH_MAIN_BRANCH" ] | |
then | |
GH_MAIN_BRANCH="main" | |
fi | |
echo -n "GitHub webhook secret [@see https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/securing-your-webhooks]: " | |
read WEBHOOK_SECRET | |
cd ~ | |
sudo apt update | |
sudo apt install curl git | |
curl -O https://storage.googleapis.com/golang/go1.15.6.linux-amd64.tar.gz | |
tar -xvf go1.15.6.linux-amd64.tar.gz | |
rm -rf go1.15.6.linux-amd64.tar.gz | |
sudo chown -R root:root ./go | |
sudo mv go /usr/local | |
sudo ln -s /usr/local/go/bin/go /usr/local/bin/go | |
go get github.com/adnanh/webhook | |
go build github.com/adnanh/webhook | |
mkdir -p $WEBSITES_ROOT_DIRECTORY/webhooks/commands | |
sudo mv webhook $WEBSITES_ROOT_DIRECTORY/webhooks | |
cat << EOF > $WEBSITES_ROOT_DIRECTORY/webhooks/hooks.json | |
[ | |
{ | |
"id": "deploy", | |
"execute-command": "/var/www/webhooks/commands/simple-pull.sh", | |
"pass-arguments-to-command": [ | |
{ | |
"source": "payload", | |
"name": "repository.name" | |
} | |
], | |
"trigger-rule": { | |
"and": [ | |
{ | |
"match": | |
{ | |
"type": "payload-hash-sha1", | |
"secret": "$WEBHOOK_SECRET", | |
"parameter": | |
{ | |
"source": "header", | |
"name": "X-Hub-Signature" | |
} | |
} | |
}, | |
{ | |
"match": | |
{ | |
"type": "value", | |
"value": "refs/heads/$GH_MAIN_BRANCH", | |
"parameter": | |
{ | |
"source": "payload", | |
"name": "ref" | |
} | |
} | |
} | |
] | |
} | |
} | |
] | |
EOF | |
cat << EOF > $WEBSITES_ROOT_DIRECTORY/webhooks/commands/simple-pull.sh | |
#!/bin/bash | |
sudo -u $USER sh -s "\$@" <<'EOF' | |
EOF | |
echo ' cd /var/www/$1 | |
git pull | |
EOF' >> $WEBSITES_ROOT_DIRECTORY/webhooks/commands/simple-pull.sh | |
chmod +x $WEBSITES_ROOT_DIRECTORY/webhooks/commands/simple-pull.sh | |
sudo echo " | |
[Unit] | |
Description=Webhooks | |
[Service] | |
ExecStart=$WEBSITES_ROOT_DIRECTORY/webhooks/webhook -hooks $WEBSITES_ROOT_DIRECTORY/webhooks/hooks.json -hotreload | |
[Install] | |
WantedBy=multi-user.target | |
" > /etc/systemd/system/webhook.service | |
sudo systemctl enable webhook.service | |
sudo systemctl start webhook.service |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed bug in https://gist.github.com/piotrusin/0402f7239b1649bc9fefe73b570c009a#file-script-sh-L87
Added escape character to generate bash script correctly.