Skip to content

Instantly share code, notes, and snippets.

@8bitDesigner
Last active December 10, 2015 00:19
Show Gist options
  • Save 8bitDesigner/4350418 to your computer and use it in GitHub Desktop.
Save 8bitDesigner/4350418 to your computer and use it in GitHub Desktop.
Express route to listen for hooks from GitHub on `POST /hook', and if a matching hook is found, then run `git pull` and crash the server. Handy if you want to be able to auto-update a running server whenever code is pushed to master - just run the server in Forever or Node-Supervisor and have that daemon restart the server on crash.
{exec} = require 'child_process'
target =
repo: 'platform'
branch: 'refs/heads/master'
update = ->
exec 'npm install && git pull', (err, stdout, stderr) ->
if err then console.error "Could not update application:", err
process.exit(0)
app.post '/hook', (req, res) ->
res.send 204 # Hang up
# Sanity check to make sure this is a valid GitHub request
unless req?.body?.payload and req.headers['x-github-event']
console.error "Bad hook received at POST:/hook - #{req.body}"
return
try # Format our hook's payload
hook = JSON.parse(req.body.payload)
catch e
console.error "Invalid JSON from GitHub: #{req.body.payload}"
return
if hook.repository.name is target.repo and hook.ref is target.branch
update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment