Last Updated: February 25, 2016
·
1.249K
· Chip Castle

Sinatra app to deploy master branch based on SemaphoreApp payload

README

This Sinatra app is installed on our staging server as follows:

[wbrent@dev-web1 deploy]$ pwd

/var/www/deploy

[wbrent@dev-web1 deploy]$ tree

.
|-- Gemfile
|-- Gemfile.lock
|-- app.rb
|-- config.ru
|-- public
`-- tmp

Gemfile

source :rubygems

gem 'shotgun'
gem 'sinatra'
gem 'yajl-ruby'
gem 'tinder'

app.rb

post '/deploy' do

  json = request.body.read
  data = Yajl::Parser.parse(json)
  token = ENV['CAMPFIRE_TOKEN']
  subdomain = ENV['CAMPFIRE_SUBDOMAIN']
  chatroom = ENV['CAMPFIRE_CHATROOM']
  campfire = Tinder::Campfire.new subdomain, :token => token, :ssl_verify => false
  room = campfire.find_room_by_name(chatroom)
  commit = data['commit']

   if data['branch_name'] == 'master' && data['result'] == 'passed'
     room.paste <<-END_OF_MESSAGE
       Deploying to staging
       Commit SHA1: #{commit['id']}
       Author: #{commit['author_name']}
       #{commit['message']}"
     END_OF_MESSAGE

     command = "cd ~/path/to/deployment; git pull origin master; cap deploy staging"
    pipe = IO.popen(command)
    response = pipe.readlines
  else
    room.paste <<-END_OF_MESSAGE
      Skipping SemaphoreApp payload
      Branch: #{data['branch_name']} 
      Build status: #{data['result']}
    END_OF_MESSAGE
  end
end

config.ru

require 'rubygems'
require 'sinatra'
require 'yajl'
require 'tinder'
require File.dirname(__FILE__) + "/app.rb"
run Sinatra::Application

/etc/httpd/conf/httpd.conf

<VirtualHost *:80>
  ServerName your.ip.address
  DocumentRoot /var/www/deploy/public
  <Directory /var/www/deploy/public>
    AllowOverride all
    Options -MultiViews
  </Directory>
</VirtualHost>

For more UNIX tips, please check out
Learning the UNIX Command Line.

Enjoy,
Chip