Last active
July 13, 2021 01:41
-
-
Save kvendrik/a936ed0eb38f7e1ffba2ee04743d6794 to your computer and use it in GitHub Desktop.
Simple Github Hook Receiver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'sinatra' | |
| require 'json' | |
| get '/' do | |
| end | |
| post '/payload' do | |
| request.body.rewind | |
| payload_body = request.body.read | |
| verify_signature(payload_body) | |
| json = JSON.parse(payload_body) | |
| ssh_url = json["repository"]["ssh_url"] | |
| name = json["repository"]["name"] | |
| if Dir.exists?(name) | |
| puts "Update #{ssh_url}" | |
| `cd #{name} && git pull origin master` | |
| else | |
| puts "Clone #{ssh_url}" | |
| `git clone #{ssh_url}` | |
| end | |
| `cd #{name} && ./scripts/setup && ./scripts/start` | |
| end | |
| def verify_signature(payload_body) | |
| signature = 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), ENV['SECRET_TOKEN'], payload_body) | |
| puts signature | |
| return halt 500, "Signatures didn't match!" unless Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE']) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment