Browse Source

Provide simple gateway service between GitHub + (internal) Jenkins system

github/hook_server.rb provides a simple demo gateway between GitHub
and an (internal) Jenkins server to receive Git push actions from
GitHub and trigger Jenkins jobs accordingly. Place this script on
a host which is available from GitHub and also has access to the
(internal) Jenkins system.

Development sponsored by Sipwise.com
tags/v0.8.0
Michael Prokop 8 years ago
parent
commit
21a4d4c5a8
4 changed files with 112 additions and 0 deletions
  1. +4
    -0
      github/Gemfile
  2. +19
    -0
      github/README
  3. +3
    -0
      github/config.yml
  4. +86
    -0
      github/hook_server.rb

+ 4
- 0
github/Gemfile View File

@@ -0,0 +1,4 @@
source 'https://rubygems.org'
gem "httparty"
gem "json"
gem "sinatra"

+ 19
- 0
github/README View File

@@ -0,0 +1,19 @@
README for github directory in jenkins-debian-glue
--------------------------------------------------

The script hook_server.rb provides a simple gateway between GitHub and
an (internal) Jenkins server to receive Git push actions from GitHub and
trigger Jenkins jobs accordingly. Place this script on a host which is
available from GitHub and has access to the (internal) Jenkins system as
well.

Instructions for usage:

* configure hook_server settings via config.yml
* adapt script hook_server.rb as needed
* install dependencies via bundle [http://bundler.io/]:
bundle install --path=bundled --verbose --binstubs
* start service:
bundle exec ./hook_server.rb
* enable hook settings of according project(s) under GitHub's "WebHook
URL", usage example: http://webhook.example.local:8042/trigger

+ 3
- 0
github/config.yml View File

@@ -0,0 +1,3 @@
token: github_jenkins_ci
cause: git_commit_triggered
server: https://jenkins.example.local/

+ 86
- 0
github/hook_server.rb View File

@@ -0,0 +1,86 @@
#!/usr/bin/env ruby

require 'httparty'
require 'json'
require 'net/http'
require 'sinatra'

set :port, 8042

## config
CONFIG = YAML.load_file('config.yml')
TOKEN = CONFIG["token"] || "unconfigured"
CAUSE = CONFIG["cause"] || "git_commit_triggered"
SERVER = CONFIG["server"] || "https://jenkins/"


## helper functions
def trigger_job(job, branch)
uri = URI("#{SERVER}job/#{job}/buildWithParameters")

req = Net::HTTP::Post.new(uri.path)
req.set_form_data('token' => TOKEN,
'cause' => CAUSE,
'branch' => branch)

res = Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https',
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
http.request(req)
end

case res
when Net::HTTPSuccess, Net::HTTPRedirection
p "OK"
else
res.value
end
end


def job_names
json = HTTParty.get("#{SERVER}/api/json")
json["jobs"].map {|job| job["name"]}
end


def job_details(job_name)
HTTParty.get("#{SERVER}/job/#{job_name}/api/json")
end


def build_details(job_name, build_number)
HTTParty.get("#{SERVER}/job/#{job_name}/#{build_number}/api/json")
end

## main sinatra app

# this is where GitHub actually ends up at, configured as
# WebHook URL at https://github.com/$OWNER/$PROJECT/settings/hooks
post '/trigger' do
push = JSON.parse(params[:payload])
# p "JSON data: #{push.inspect}"
# p JSON.pretty_generate(push)

project = push["repository"]["name"]
project = project + "-source" # we use $job-source as default entry point
branch = push["ref"]

# make sure we do not trigger a build with a branch that was just deleted
deleted = push["deleted"]

if deleted
puts "branch #{branch} was deleted, not triggering build"
elsif job_names.include? project
trigger_job(project, branch)
else
puts "no such job #{project}"
end
end

# debugging helper
post '/debug' do
puts params[:payload]
end

## END OF FILE #################################################################

Loading…
Cancel
Save