Skip to content

Instantly share code, notes, and snippets.

@zakariaboualaid
Created September 28, 2016 10:19
Show Gist options
  • Save zakariaboualaid/12c20267f1e1b3ba7fa209fa082c24a3 to your computer and use it in GitHub Desktop.
Save zakariaboualaid/12c20267f1e1b3ba7fa209fa082c24a3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Gems needed : sinatra, jenkins_api_client
# This file is a light web-server using Sinatra that responds to Github events.
# The purpose of this file is to run jobs when some events are triggered in Github.
require 'sinatra'
require 'json'
require 'net/http'
require 'jenkins_api_client'
enable :logging
@client = JenkinsApi::Client.new(:server_ip => 'jenkins.beneple.com',
:username => 'Bot-Beneple', :password => '6ac553f6723d4f313c508e18681458ee')
set :bind, '0.0.0.0'
set :port, 4567
#@client.job.build "beneple-test-master"
post '/event_handler' do
@payload = JSON.parse(request.body.read)
case request.env['HTTP_X_GITHUB_EVENT']
when "pull_request"
if @payload["action"] == "closed" && @payload["pull_request"]["merged"]
puts "A pull request was merged! A deployment to the development environment should start now..."
deploy_master_to_dev_box(@payload["pull_request"])
clean_branch_deployment(@payload["pull_request"])
end
end
end
helpers do
def deploy_master_to_dev_box(pr)
puts "Deploying master branch to the dev box after merge of Pull Request : #{pr['title']}"
@client.job.build("beneple-test-master")
end
def clean_branch_deployment(pr)
puts "Cleaning dev box from a deployed branch of Pull Request : #{pr['title']}"
Dir.chdir("/home/jenkins/beneple") do
system "git pull origin master"
system "cap dev deploy:clean[#{pr['head']['ref']}]"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment