Merge pull request #68 from IT4Change/webhook

feat(other): implement webhook
This commit is contained in:
Ulf Gebhardt 2023-10-05 00:07:41 +02:00 committed by GitHub
commit 43485e78d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 121 additions and 0 deletions

29
.github/webhooks/deploy.sh vendored Executable file
View File

@ -0,0 +1,29 @@
#!/bin/sh
# Find current directory & configure paths
SCRIPT_PATH=$(realpath $0)
SCRIPT_DIR=$(dirname $SCRIPT_PATH)
PROJECT_ROOT=$SCRIPT_DIR/../..
DEPLOY_DIR=$1
BUILD_DIR=$PROJECT_ROOT/docs/.vuepress/dist
# assuming you are already on the right branch
git pull -ff
GIT_REF=$(git rev-parse --short HEAD)
DEPLOY_DIR_REF=$DEPLOY_DIR-$GIT_REF
# Parameter is a proper directory?
if [ -d "$DEPLOY_DIR_REF" ]; then
return "Directory '$DEPLOY_DIR_REF' does already exist" 2>/dev/null || exit 1
fi
# Build the project
cd $PROJECT_ROOT
rm -R $BUILD_DIR
npm run build
# Copy files and Sym link
mkdir $DEPLOY_DIR_REF/
cp -r $BUILD_DIR/* $DEPLOY_DIR_REF/
ln -sf $DEPLOY_DIR_REF $DEPLOY_DIR

37
.github/webhooks/hooks.json.template vendored Normal file
View File

@ -0,0 +1,37 @@
[
{
"id": "github",
"execute-command": "$PROJECT_ROOT/.github/webhooks/deploy.sh",
"pass-arguments-to-command": [
{
"source": "string",
"name": "$DEPLOY_DIR"
}
],
"command-working-directory": "$PROJECT_ROOT",
"trigger-rule": {
"and": [
{
"match": {
"type": "payload-hash-sha1",
"secret": "$WEBHOOK_GITHUB_SECRET",
"parameter": {
"source": "header",
"name": "X-Hub-Signature"
}
}
},
{
"match": {
"type": "value",
"value": "refs/heads/$WEBHOOK_GITHUB_BRANCH",
"parameter": {
"source": "payload",
"name": "ref"
}
}
}
]
}
}
]

4
.github/webhooks/webhook.template vendored Normal file
View File

@ -0,0 +1,4 @@
#!/sbin/openrc-run
command=webhook
command_args="-hooks $PROJECT_ROOT/.github/webhooks/hooks.json &"

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/node_modules/
/docs/.vuepress/dist/
/.github/webhooks/hooks.json

View File

@ -42,6 +42,56 @@ Run the tests to ensure everything is working as expected
npm test
```
## Deploy
You can use the webhook template `webhook.conf.template` and the `deploy.sh` script in `.github/webhooks/` to implement an automatic deployment from a (github) webhook.
For this to work follow these steps (using alpine):
```bash
apk add webhook
cp .github/webhooks/hooks.json.template .github/webhooks/hooks.json
vi .github/webhooks/hooks.json
# adjust content of .github/webhooks/hooks.json
# replace all variables accordingly
# copy webhook service file
cp .github/webhooks/webhook.template /etc/init.d/webhook
vi /etc/init.d/webhook
# adjust content of /etc/init.d/webhook
chmod +x /etc/init.d/webhook
service webhook start
rc-update add webhook boot
vi /etc/nginx/http.d/default.conf
# adjust the nginx config
# location /hooks/ {
# proxy_http_version 1.1;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection 'upgrade';
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header Host $host;
#
# proxy_pass http://127.0.0.1:9000/hooks/;
# proxy_redirect off;
#
# #access_log $LOG_PATH/nginx-access.hooks.log hooks_log;
# #error_log $LOG_PATH/nginx-error.backend.hook.log warn;
# }
```
For the github webhook configure the following:
| Field | Value |
|------------------------------------------------------|-------------------------------|
| Payload URL | https://it4c.dev/hooks/github |
| Content type | application/json |
| Secret | A SECRET |
| SSL verification | Enable SSL verification |
| Which events would you like to trigger this webhook? | Send me everything. |
| Active | [x] |
## How it works
This repository utilizes `vuepress-deploy` to automatically deploy the current `master` branch to github pages.