diff --git a/.github/webhooks/deploy.sh b/.github/webhooks/deploy.sh new file mode 100755 index 0000000..3f19e30 --- /dev/null +++ b/.github/webhooks/deploy.sh @@ -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 \ No newline at end of file diff --git a/.github/webhooks/hooks.json.template b/.github/webhooks/hooks.json.template new file mode 100644 index 0000000..84b25a4 --- /dev/null +++ b/.github/webhooks/hooks.json.template @@ -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" + } + } + } + ] + } + } + ] \ No newline at end of file diff --git a/.github/webhooks/webhook.template b/.github/webhooks/webhook.template new file mode 100644 index 0000000..be511c5 --- /dev/null +++ b/.github/webhooks/webhook.template @@ -0,0 +1,4 @@ +#!/sbin/openrc-run + +command=webhook +command_args="-hooks $PROJECT_ROOT/.github/webhooks/hooks.json &" \ No newline at end of file diff --git a/.gitignore b/.gitignore index d8ad7d4..7d3cace 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules/ /docs/.vuepress/dist/ +/.github/webhooks/hooks.json diff --git a/README.md b/README.md index 6554e3a..1a81544 100644 --- a/README.md +++ b/README.md @@ -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.