From 971f5d4864038637bb9b7c828cd64dd29866d744 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 10 Oct 2014 17:55:39 +0100 Subject: Fix problem with branch names. Branch names that contained slashes like feature/whatever were not parsed properly. --- jekyll-hook.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/jekyll-hook.js b/jekyll-hook.js index 1094f07..5f2453a 100755 --- a/jekyll-hook.js +++ b/jekyll-hook.js @@ -1,4 +1,4 @@ -#!/usr/bin/env node +//#!/usr/bin/env node var config = require('./config.json'); var fs = require('fs'); @@ -32,25 +32,30 @@ app.use(express.bodyParser({ err.status = 403; throw err; } + + req.request_verified = true; } })); // Receive webhook post -app.post('/hooks/jekyll/:branch', function(req, res) { - +app.post('/hooks/jekyll/*', function(req, res) { + // Prevent non verified request from reaching this. + if (!req.request_verified) { + return res.status(403).send('The request could not be verified.'); + } // Close connection res.send(202); // Queue request handler tasks.defer(function(req, res, cb) { var data = req.body; - var branch = req.params.branch; + var branch = req.params[0]; var params = []; // Parse webhook data for internal variables data.repo = data.repository.name; - data.branch = data.ref.split('/')[2]; + data.branch = data.ref.replace('refs/heads/', ''); data.owner = data.repository.owner.name; // End early if not permitted account -- cgit v1.2.3 From b53a599ef270f4271de685ff86b131dee5313b58 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 10 Oct 2014 18:09:21 +0100 Subject: Allow users to specify a different script for each branch. --- config.sample.json | 2 ++ jekyll-hook.js | 37 +++++++++++++++++++++++++++++-------- readme.md | 12 +++++++++--- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/config.sample.json b/config.sample.json index eba0920..566b329 100644 --- a/config.sample.json +++ b/config.sample.json @@ -3,8 +3,10 @@ "temp": "/home/ubuntu/jekyll-hook", "public_repo": true, "scripts": { + "#default": { "build": "./scripts/build.sh", "publish": "./scripts/publish.sh" + } }, "secret": "", "email": { diff --git a/jekyll-hook.js b/jekyll-hook.js index 5f2453a..baf26df 100755 --- a/jekyll-hook.js +++ b/jekyll-hook.js @@ -32,18 +32,12 @@ app.use(express.bodyParser({ err.status = 403; throw err; } - - req.request_verified = true; } })); // Receive webhook post app.post('/hooks/jekyll/*', function(req, res) { - // Prevent non verified request from reaching this. - if (!req.request_verified) { - return res.status(403).send('The request could not be verified.'); - } // Close connection res.send(202); @@ -87,8 +81,35 @@ app.post('/hooks/jekyll/*', function(req, res) { /* source */ params.push(config.temp + '/' + data.owner + '/' + data.repo + '/' + data.branch + '/' + 'code'); /* build */ params.push(config.temp + '/' + data.owner + '/' + data.repo + '/' + data.branch + '/' + 'site'); + // Script by branch. + var build_script = null; + try { + build_script = config.scripts[data.branch].build; + } + catch(err) { + try { + build_script = config.scripts['#default'].build; + } + catch(err) { + throw new Error('No default build script defined.'); + } + } + + var publish_script = null; + try { + publish_script = config.scripts[data.branch].publish; + } + catch(err) { + try { + publish_script = config.scripts['#default'].publish; + } + catch(err) { + throw new Error('No default publish script defined.'); + } + } + // Run build script - run(config.scripts.build, params, function(err) { + run(build_script, params, function(err) { if (err) { console.log('Failed to build: ' + data.owner + '/' + data.repo); send('Your website at ' + data.owner + '/' + data.repo + ' failed to build.', 'Error building site', data); @@ -98,7 +119,7 @@ app.post('/hooks/jekyll/*', function(req, res) { } // Run publish script - run(config.scripts.publish, params, function(err) { + run(publish_script, params, function(err) { if (err) { console.log('Failed to publish: ' + data.owner + '/' + data.repo); send('Your website at ' + data.owner + '/' + data.repo + ' failed to publish.', 'Error publishing site', data); diff --git a/readme.md b/readme.md index 44d91e9..1759576 100644 --- a/readme.md +++ b/readme.md @@ -62,8 +62,12 @@ Configuration attributes: - `temp` A directory to store code and site files - `public-repo` Whether the repo is public or private (default is public) - `scripts` - - `build` A script to run to build the site - - `publish` A script to run to publish the site + - `[branch-name]` (optional) + - `build` The build script to run for a specific branch. + - `publish` The publish script to run for a specific branch. + - `#default` + - `build` The build script to run if no match was found for the branch specified in the webhook. + - `publish` The publish script to run if match was found for the branch specified in the webhook. - `email` Optional. Settings for sending email alerts - `isActivated` If set to true email will be sent after each trigger - `user` Sending email account's user name (e.g. `example@gmail.com`) @@ -81,6 +85,8 @@ they generate a site with Jekyll and publish it to an NGINX web directory. Set a [Web hook](https://developer.github.com/webhooks/) on your GitHub repository that points to your jekyll-hook server `http://example.com:8080/hooks/jekyll/:branch`, where `:branch` is the branch you want to publish. Usually this is `gh-pages` or `master` for `*.github.com` / `*.github.io` repositories. +For every branch you want to build/publish (as defined in the `scripts`) you need to set up a different webhook. + ## Configure a webserver (nginx) The default `publish.sh` is setup for nginx and copies `_site` folder to `/usr/share/nginx/html/rep_name`. @@ -150,4 +156,4 @@ server { Replace this script with whatever you need for your particular hosting environment. You probably want to configure your server to only respond POST requests from GitHub's -public IP addresses, found on the webhooks settings page. +public IP addresses, found on the webhooks settings page. \ No newline at end of file -- cgit v1.2.3 From e354f857e5e4b22da9f29fbf18806e6304964478 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 13 Oct 2014 11:32:51 +0100 Subject: Remove forgotten comment. --- jekyll-hook.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jekyll-hook.js b/jekyll-hook.js index baf26df..14f73e4 100755 --- a/jekyll-hook.js +++ b/jekyll-hook.js @@ -1,4 +1,4 @@ -//#!/usr/bin/env node +#!/usr/bin/env node var config = require('./config.json'); var fs = require('fs'); -- cgit v1.2.3