Skip to content

Instantly share code, notes, and snippets.

@mtrl
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtrl/19731526b241bf58b08c to your computer and use it in GitHub Desktop.
Save mtrl/19731526b241bf58b08c to your computer and use it in GitHub Desktop.
A very simple Git post receive hook and coffeescript script for hubot notifications on Git deployments
# The Hubot coffee (name it git-post-receive.coffee) and put it in [hubot]/scripts/ and enable in hubot-scripts.json
# Description:
# Notifies a chat room of a git post receive hook POST notification
#
# Dependencies:
# None
#
# Configuration:
#
# Author:
# MWilliams
module.exports = (robot) ->
robot.router.post '/hubot/git-post-receive/:room', (req, res) ->
room = req.params.room
msg = "#{req.body.msg}"
#msg += "[#{req.params.branch}]\n"
robot.messageRoom room, msg
res.writeHead 204, { 'Content-Length': 0 }
res.end()
#!/usr/bin/env ruby
# I'm using git to deploy changes to production with a "git push prod" command. This post receive hook deploys to the prod directory and also sends a HTTP POST request to a hubot service to send a notification to the chat room that a deployment has happened
require 'net/http'
# post-receive
# 1. Read STDIN (Format: "from_commit to_commit branch_name")
from, to, branch = ARGF.read.split " "
# 2. Only deploy if master branch was pushed
if (branch =~ /master$/) == nil
puts "Received branch #{branch}, not deploying."
exit
end
# 3. Copy files to deploy directory
deploy_to_dir = File.expand_path('[relative path to web root]')
`GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f master`
puts "DEPLOY: master(#{to}) copied to '#{deploy_to_dir}'"
puts "SUCCESS!: Deployed master branch"
# 4.TODO: Deployment Tasks
# i.e.: Run Puppet Apply, Restart Daemons, etc
# Notify hubot when someone pushes.
uri = URI('http://[hubot URL]:8080/hubot/git-post-receive/[hipchat room ID]@conf.hipchat.com')
puts "Notified hubot"
Net::HTTP.post_form uri, 'msg' => '[repo name] master branch deployed to prod'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment