An automated deployment with bitbucket webhook listener

Pedro Kayser
Dec 28, 2018 · 6 min read

CONTEXT

WEBHOOK

APPLICATION OVERVIEW

IMPLEMENTATION

$ mkdir bitbucket-webhook-listener
# bitbucket-webhook-listener/Gemfilesource 'https://rubygems.org'gem 'json'
gem 'puma'
gem 'sinatra'
gem 'sinatra-contrib'
# bitbucket-webhook-listener/config.rurequire 'bundler/setup'
Bundler.require
require './application'
run Sinatra::Application
# bitbucket-webhook-listener/application.rbrequire 'sinatra'
require 'sinatra/json'
require 'sinatra/reloader'
configure do
set :server, :puma
end
before do
content_type 'application/json'
end
get '/' do
json message: 'Hello World!'
end
$ bundle install
$ rackup
# bitbucket-webhook-listener/application.rb...post '/' do
begin
req = JSON.parse request.body.read
repo = req['repository']unless repo['type'] == 'repository' && repo['name'] == 'app'
return 204
end
push = req['push']['changes'][0]['new']unless push['type'] == 'branch' && push['name'] == 'test'
return 204
end
# Runs the deployment processreturn json message: 'The deployment process was run!'
rescue
return 204
end
end
{    
"repository": {
"type": "repository",
"name": "app"
},
"push": {
"changes": [
{
"new": {
"type": "branch",
"name": "test"
}
}
]
}
}

DEPLOYMENT

BITBUCKET SETUP

CONCLUSION

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade