From 2d54fb24cac0c2944d5b2d3c06844e31c2d4aaa4 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 4 Oct 2023 17:35:12 +0200 Subject: [PATCH 01/11] initial blind implementation & documentation of a webhook --- .github/webhooks/deploy.sh | 24 +++++++++++++++++ .github/webhooks/webhook.conf.template | 37 ++++++++++++++++++++++++++ README.md | 12 +++++++++ 3 files changed, 73 insertions(+) create mode 100755 .github/webhooks/deploy.sh create mode 100644 .github/webhooks/webhook.conf.template diff --git a/.github/webhooks/deploy.sh b/.github/webhooks/deploy.sh new file mode 100755 index 0000000..f15df0a --- /dev/null +++ b/.github/webhooks/deploy.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Find current directory & configure paths +SCRIPT_PATH=$(realpath $0) +SCRIPT_DIR=$(dirname $SCRIPT_PATH) +PROJECT_ROOT=$SCRIPT_DIR/../.. +DEPLOY_DIR=$i +BUILD_DIR=$PROJECT_ROOT/docs/.vuepress/dist +GIT_REF=$(git rev-parse --short HEAD) +DEPLOY_DIR_REF=$1-$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 +cp $BUILD_DIR $DEPLOY_DIR_REF +ln -sf $DEPLOY_DIR_REF $DEPLOY_DIR \ No newline at end of file diff --git a/.github/webhooks/webhook.conf.template b/.github/webhooks/webhook.conf.template new file mode 100644 index 0000000..84b25a4 --- /dev/null +++ b/.github/webhooks/webhook.conf.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/README.md b/README.md index 6554e3a..c4e7990 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,18 @@ 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. + +To work follow these steps (using alpine): +```bash +apk add webhook +cp .github/webhooks/webhook.conf /etc/webhook.conf +# adjust content of /etc/webhook.conf accordingly +systemctl enable webhook +systemctl start webhook +``` ## How it works This repository utilizes `vuepress-deploy` to automatically deploy the current `master` branch to github pages. From db34565455e2f49be2b8c913ac764b693ea5b44e Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 4 Oct 2023 18:51:08 +0200 Subject: [PATCH 02/11] update webhook deploy.sh & ignore hooks.json --- .github/webhooks/deploy.sh | 4 ++++ .gitignore | 1 + 2 files changed, 5 insertions(+) diff --git a/.github/webhooks/deploy.sh b/.github/webhooks/deploy.sh index f15df0a..86b7fb2 100755 --- a/.github/webhooks/deploy.sh +++ b/.github/webhooks/deploy.sh @@ -6,6 +6,10 @@ SCRIPT_DIR=$(dirname $SCRIPT_PATH) PROJECT_ROOT=$SCRIPT_DIR/../.. DEPLOY_DIR=$i 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=$1-$GIT_REF 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 From 206da726a521f17b72402daaa45863086c10e12e Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 4 Oct 2023 18:57:20 +0200 Subject: [PATCH 03/11] renamed webhook.conf.template to hooks.json.template --- .github/webhooks/{webhook.conf.template => hooks.json.template} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/webhooks/{webhook.conf.template => hooks.json.template} (100%) diff --git a/.github/webhooks/webhook.conf.template b/.github/webhooks/hooks.json.template similarity index 100% rename from .github/webhooks/webhook.conf.template rename to .github/webhooks/hooks.json.template From f563c48242e033129862c8345193ac7591d4c3db Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 4 Oct 2023 19:03:57 +0200 Subject: [PATCH 04/11] added webhook.tempolate --- .github/webhooks/webhook.template | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .github/webhooks/webhook.template 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 From d997708a77320bd15ef1e24dbde26c29ac36f3bb Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 4 Oct 2023 19:08:15 +0200 Subject: [PATCH 05/11] deploy.sh using /bin/sh --- .github/webhooks/deploy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/webhooks/deploy.sh b/.github/webhooks/deploy.sh index 86b7fb2..d80cd8d 100755 --- a/.github/webhooks/deploy.sh +++ b/.github/webhooks/deploy.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # Find current directory & configure paths SCRIPT_PATH=$(realpath $0) From b5b3959fc4c36ffa6f718c884c142b1272757bb5 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 4 Oct 2023 19:10:57 +0200 Subject: [PATCH 06/11] update cp in deploy.sh --- .github/webhooks/deploy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/webhooks/deploy.sh b/.github/webhooks/deploy.sh index d80cd8d..ca555c7 100755 --- a/.github/webhooks/deploy.sh +++ b/.github/webhooks/deploy.sh @@ -24,5 +24,5 @@ rm -R $BUILD_DIR npm run build # Copy files and Sym link -cp $BUILD_DIR $DEPLOY_DIR_REF +cp $BUILD_DIR/* $DEPLOY_DIR_REF/ ln -sf $DEPLOY_DIR_REF $DEPLOY_DIR \ No newline at end of file From bed3d08b51687f85c9c2a9092fc954cca861587d Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 4 Oct 2023 19:14:26 +0200 Subject: [PATCH 07/11] update readme --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c4e7990..1a81544 100644 --- a/README.md +++ b/README.md @@ -46,14 +46,52 @@ npm test 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. -To work follow these steps (using alpine): +For this to work follow these steps (using alpine): ```bash apk add webhook -cp .github/webhooks/webhook.conf /etc/webhook.conf -# adjust content of /etc/webhook.conf accordingly -systemctl enable webhook -systemctl start 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. From 0071c610ecbf49db9f3a795cd106d6df7f91f1ef Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 4 Oct 2023 19:18:00 +0200 Subject: [PATCH 08/11] test commit to trigger hook --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1a81544..e075592 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ npm test 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): +For this to work follow these steps (using alpine linux): ```bash apk add webhook cp .github/webhooks/hooks.json.template .github/webhooks/hooks.json From 72f336f2773010f923b08c82c9cce33079887eb4 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 4 Oct 2023 19:22:24 +0200 Subject: [PATCH 09/11] make directory for deploy version --- .github/webhooks/deploy.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/webhooks/deploy.sh b/.github/webhooks/deploy.sh index ca555c7..4ef747a 100755 --- a/.github/webhooks/deploy.sh +++ b/.github/webhooks/deploy.sh @@ -4,14 +4,14 @@ SCRIPT_PATH=$(realpath $0) SCRIPT_DIR=$(dirname $SCRIPT_PATH) PROJECT_ROOT=$SCRIPT_DIR/../.. -DEPLOY_DIR=$i +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=$1-$GIT_REF +DEPLOY_DIR_REF=$DEPLOY_DIR-$GIT_REF # Parameter is a proper directory? if [ -d "$DEPLOY_DIR_REF" ]; then @@ -24,5 +24,6 @@ rm -R $BUILD_DIR npm run build # Copy files and Sym link +mkdir $DEPLOY_DIR_REF/ cp $BUILD_DIR/* $DEPLOY_DIR_REF/ ln -sf $DEPLOY_DIR_REF $DEPLOY_DIR \ No newline at end of file From f629ec012d52a0f97d482658c71f4c0e42b4fa16 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 4 Oct 2023 19:24:36 +0200 Subject: [PATCH 10/11] make cp recursive --- .github/webhooks/deploy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/webhooks/deploy.sh b/.github/webhooks/deploy.sh index 4ef747a..3f19e30 100755 --- a/.github/webhooks/deploy.sh +++ b/.github/webhooks/deploy.sh @@ -25,5 +25,5 @@ npm run build # Copy files and Sym link mkdir $DEPLOY_DIR_REF/ -cp $BUILD_DIR/* $DEPLOY_DIR_REF/ +cp -r $BUILD_DIR/* $DEPLOY_DIR_REF/ ln -sf $DEPLOY_DIR_REF $DEPLOY_DIR \ No newline at end of file From 774df93933903f34712a2dceb91e74609b1aed51 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 4 Oct 2023 19:26:08 +0200 Subject: [PATCH 11/11] test commit to trigger webhook --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e075592..1a81544 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ npm test 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 linux): +For this to work follow these steps (using alpine): ```bash apk add webhook cp .github/webhooks/hooks.json.template .github/webhooks/hooks.json