Skip to content

Commit

Permalink
Merge pull request #16 from developmentseed/improvements
Browse files Browse the repository at this point in the history
Making jekyll-hook compatible with Ubuntu 14.0
  • Loading branch information
scisco committed Jul 17, 2014
2 parents ea87493 + 54d4b6c commit 086585c
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 93 deletions.
13 changes: 7 additions & 6 deletions config.sample.json
@@ -1,18 +1,19 @@
{
"gh_server": "github.com",
"temp": "/home/ubuntu/jekyll-hook",
"public_repo": true,
"scripts": {
"build": "./scripts/build.sh",
"publish": "./scripts/publish.sh"
},
"email": {
"user": "",
"password": "",
"host": "",
"isActivated": false,
"user": "",
"password": "",
"host": "",
"ssl": true
},
"accounts": [
"developmentseed",
"mapbox"
"developmentseed"
]
}
}
29 changes: 19 additions & 10 deletions jekyll-hook.js
Expand Up @@ -20,7 +20,7 @@ app.post('/hooks/jekyll/:branch', function(req, res) {

// Queue request handler
tasks.defer(function(req, res, cb) {
var data = JSON.parse(req.body.payload);
var data = req.body;
var branch = req.params.branch;
var params = [];

Expand All @@ -47,7 +47,14 @@ app.post('/hooks/jekyll/:branch', function(req, res) {
/* repo */ params.push(data.repo);
/* branch */ params.push(data.branch);
/* owner */ params.push(data.owner);
/* giturl */ params.push('git@' + config.gh_server + ':' + data.owner + '/' + data.repo + '.git');

/* giturl */
if (config.public_repo) {
params.push('https://' + config.gh_server + '/' + data.owner + '/' + data.repo + '.git');
} else {
params.push('git@' + config.gh_server + ':' + data.owner + '/' + data.repo + '.git');
}

/* source */ params.push(config.temp + '/' + data.owner + '/' + data.repo + '/' + data.branch + '/' + 'code');
/* build */ params.push(config.temp + '/' + data.owner + '/' + data.repo + '/' + data.branch + '/' + 'site');

Expand Down Expand Up @@ -105,13 +112,15 @@ function run(file, params, cb) {
}

function send(body, subject, data) {
if (config.email && data.pusher.email) {
var message = {
text: body,
from: config.email.user,
to: data.pusher.email,
subject: subject
};
mailer.send(message, function(err) { if (err) console.warn(err); });
if (config.email.isActivated) {
if (config.email && data.pusher.email) {
var message = {
text: body,
from: config.email.user,
to: data.pusher.email,
subject: subject
};
mailer.send(message, function(err) { if (err) console.warn(err); });
}
}
}
150 changes: 104 additions & 46 deletions readme.md
@@ -1,38 +1,130 @@
# jekyll-hook

A server that listens for webhook posts from GitHub, generates a website with Jekyll, and moves it somewhere to be published. Use this to run your own GitHub Pages-style web server. Great for when you need to serve your websites behind a firewall, need extra server-level features like HTTP basic auth (see below for an NGINX config with basic auth), or want to host your site directly on a CDN or file host like S3. It's cutomizable with two user-configurable shell scripts and a config file.
A server that listens for webhook posts from GitHub, generates a website with
Jekyll, and moves it somewhere to be published. Use this to run your own GitHub
Pages-style web server. Great for when you need to serve your websites behind a
firewall, need extra server-level features like HTTP basic auth (see below for an
NGINX config with basic auth), or want to host your site directly on a CDN or
file host like S3. It's cutomizable with two user-configurable shell scripts
and a config file.

*This guide is tested on Ubuntu 14.0*

## Dependencies Installation

First install main dependencies

$: sudo apt-get update
$: sudo apt-get install git nodejs ruby ruby1.9.1-dev npm

Symlink nodejs to node

$: sudo ln -s /usr/bin/nodejs /usr/bin/node

To keep server running we use Forever:

$: sudo npm install -g forever

We also need Jekyll and Nginx

$: sudo gem install jekyll rdiscount json
$: sudo apt-get install nginx

## Installation

- run `$ npm install` to install app dependencies
- Set a [Web hook]() 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.
Clone the repo

$: git clone https://github.com/developmentseed/jekyll-hook.git

Install dependencies:

$: cd jekyll-hook
jekyll-hook $: npm install

If you receive an error similar to this `npm ERR! Error: EACCES, mkdir
'/home/ubuntu/tmp/npm-2223-4myn3niN'` run:

$: sudo chown -R ubuntu:ubuntu /home/ubuntu/tmp
$: npm install

*You should replace `ubuntu` with your username*

## Configuration

Adjust `build.sh` and `publish.sh` to suit your workflow. By default, they generate a site with Jekyll and publish it to an NGINX web directory.
Copy `config.sample.json` to `config.json` in the root directory and customize:

Copy `config.sample.json` to `config.json` in the root directory and customize.
$: cp config.sample.json config.json
$: vim config.json

Configuration attributes:

- `gh_server` The GitHub server from which to pull code
- `gh_server` The GitHub server from which to pull code, e.g. github.com
- `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
- `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`)
- `password` Sending email account's password
- `host` SMTP host for sending email account (e.g. `smtp.gmail.com`)
- `host` SMTP host for sending email account (e.g. `smtp.gmail.com`)
- `ssl` `true` or `false` for SSL
- `accounts` An array of accounts or organizations whose repositories can be used with this server
## Usage
- `accounts` An array of accounts or organizations whose repositories can be used
with this server

You can also adjust `build.sh` and `publish.sh` to suit your workflow. By default,
they generate a site with Jekyll and publish it to an NGINX web directory.

## Webhook Setup on Github

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.

## Configure a webserver (nginx)

The default `publish.sh` is setup for nginx and copies `_site` folder to `/usr/share/nginx/html/rep_name`.

If you would like to copy the website to another location, make sure to update
nginx virtual hosts which is located at `/etc/nginx/nginx/site-available` on Ubuntu 14.

You also need to update `publish.sh`

For more information Google or [read this](https://www.digitalocean.com/community/tutorials/how-to-configure-the-nginx-web-server-on-a-virtual-private-server):

- run as executable: `$ ./jekyll-hook.js`
## Launch

$: ./jekyll-hook.js

To launch in background run:

$: forever start jekyll-hook.js

To kill or restart the background job:

```
$: forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] ZQMF /usr/bin/nodejs jekyll-hook.js 4166 4168 /home/ubuntu/.forever/ZQMF.log 0:0:1:22.176
$: forever stop 0
```

## Publishing content

### S3

To publish the site on Amazon S3, you need to install S3cmd. On Ubuntu run:

$: sudo apt-get install s3cmd
$: s3cmd --configure

For more information [read this](http://xmodulo.com/2013/06/how-to-access-amazon-s3-cloud-storage-from-command-line-in-linux.html).

`scripts/publish-s3.sh` does the rest of the job for you. Just make sure to add your bucket name there.

### More details on build.sh

The stock `build.sh` copies rendered site files to subdirectories under a web server's `www` root directory. For instance, use this script and NGINX with the following configuration file to serve static content behind HTTP basic authentication:

```
Expand All @@ -57,39 +149,5 @@ 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.

## Dependencies

Here's a sample script to install the approriate dependencies on an Ubuntu server:

```sh
#!/bin/sh

# Install node and depencencies
sudo apt-get update -y
sudo apt-get install python-software-properties python g++ make -y
# On Ubuntu 12.10 and greater, add-apt-repository is provided by the software-properties-common package
#sudo apt-get install software-properties-common -y
sudo add-apt-repository ppa:chris-lea/node.js -y
sudo apt-get update -y
sudo apt-get install nodejs -y

# Forever to keep server running
sudo npm install -g forever

# Git
sudo apt-get install git -y

# Ruby
sudo apt-get install ruby1.8 -y
sudo apt-get install rubygems -y

# Jekyll
sudo gem install jekyll --version "0.12.0"
sudo gem install rdiscount -- version "1.6.8"
sudo gem install json --version "1.6.1"

# Nginx for static content
sudo apt-get install nginx -y
```
You probably want to configure your server to only respond POST requests from GitHub's
public IP addresses, found on the webhooks settings page.
29 changes: 0 additions & 29 deletions scripts/build-v1.sh

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/build.sh
Expand Up @@ -25,5 +25,5 @@ cd -

# Run jekyll
cd $source
jekyll $source $build --no-server --no-auto
jekyll build -s $source -d $build
cd -
4 changes: 3 additions & 1 deletion scripts/publish.sh
Expand Up @@ -13,8 +13,10 @@ source=$5
build=$6

# Set the path of the hosted site
site="/usr/share/nginx/www/$repo"
site="/usr/share/nginx/html/$repo"

# Remove old site files, move new ones in place
# On amazon EC2 use sudo if nginx html forlder has root ownership

rm -rf $site
mv $build $site

0 comments on commit 086585c

Please sign in to comment.