forked from github/platform-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add *two* new samples for CIs and deployments
- Loading branch information
1 parent
a0dceac
commit 5b3fb77
Showing
8 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
source "http://rubygems.org" | ||
|
||
gem "json", "1.7.7" | ||
gem 'sinatra', '~> 1.3.5' | ||
gem "shotgun" | ||
gem "octokit", '~> 3.0' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
addressable (2.3.6) | ||
faraday (0.9.0) | ||
multipart-post (>= 1.2, < 3) | ||
json (1.7.7) | ||
multipart-post (2.0.0) | ||
octokit (3.0.0) | ||
sawyer (~> 0.5.3) | ||
rack (1.5.2) | ||
rack-protection (1.5.2) | ||
rack | ||
sawyer (0.5.4) | ||
addressable (~> 2.3.5) | ||
faraday (~> 0.8, < 0.10) | ||
shotgun (0.9) | ||
rack (>= 1.0) | ||
sinatra (1.3.6) | ||
rack (~> 1.4) | ||
rack-protection (~> 1.3) | ||
tilt (~> 1.3, >= 1.3.3) | ||
tilt (1.4.1) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
json (= 1.7.7) | ||
octokit (~> 3.0) | ||
shotgun | ||
sinatra (~> 1.3.5) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require "./server" | ||
run CITutorial |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'sinatra/base' | ||
require 'json' | ||
require 'octokit' | ||
|
||
class CITutorial < Sinatra::Base | ||
|
||
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!! | ||
# Instead, set and test environment variables, like below | ||
ACCESS_TOKEN = ENV['MY_PERSONAL_TOKEN'] | ||
|
||
before do | ||
@client ||= Octokit::Client.new(:access_token => ACCESS_TOKEN) | ||
end | ||
|
||
post '/event_handler' do | ||
@payload = JSON.parse(params[:payload]) | ||
|
||
case request.env['HTTP_X_GITHUB_EVENT'] | ||
when "pull_request" | ||
if @payload["action"] == "opened" | ||
process_pull_request(@payload["pull_request"]) | ||
end | ||
end | ||
end | ||
|
||
helpers do | ||
def process_pull_request(pull_request) | ||
puts "Processing pull request..." | ||
@client.create_status(pull_request['head']['repo']['full_name'], pull_request['head']['sha'], 'pending') | ||
sleep 2 # do busy work... | ||
@client.create_status(pull_request['head']['repo']['full_name'], pull_request['head']['sha'], 'success') | ||
puts "Pull request processed!" | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
source "http://rubygems.org" | ||
|
||
gem "json", "1.7.7" | ||
gem 'sinatra', '~> 1.3.5' | ||
gem "shotgun" | ||
gem "octokit", '~> 3.0' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
addressable (2.3.6) | ||
faraday (0.9.0) | ||
multipart-post (>= 1.2, < 3) | ||
json (1.7.7) | ||
multipart-post (2.0.0) | ||
octokit (3.0.0) | ||
sawyer (~> 0.5.3) | ||
rack (1.5.2) | ||
rack-protection (1.5.2) | ||
rack | ||
sawyer (0.5.4) | ||
addressable (~> 2.3.5) | ||
faraday (~> 0.8, < 0.10) | ||
shotgun (0.9) | ||
rack (>= 1.0) | ||
sinatra (1.3.6) | ||
rack (~> 1.4) | ||
rack-protection (~> 1.3) | ||
tilt (~> 1.3, >= 1.3.3) | ||
tilt (1.4.1) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
json (= 1.7.7) | ||
octokit (~> 3.0) | ||
shotgun | ||
sinatra (~> 1.3.5) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require "./server" | ||
run DeploymentTutorial |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'sinatra/base' | ||
require 'json' | ||
require 'octokit' | ||
|
||
class DeploymentTutorial < Sinatra::Base | ||
|
||
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!! | ||
# Instead, set and test environment variables, like below | ||
ACCESS_TOKEN = ENV['MY_PERSONAL_TOKEN'] | ||
|
||
before do | ||
@client ||= Octokit::Client.new(:access_token => ACCESS_TOKEN) | ||
end | ||
|
||
post '/event_handler' do | ||
@payload = JSON.parse(params[:payload]) | ||
|
||
case request.env['HTTP_X_GITHUB_EVENT'] | ||
when "pull_request" | ||
if @payload["action"] == "closed" && @payload["pull_request"]["merged"] | ||
start_deployment(@payload["pull_request"]) | ||
end | ||
when "deployment" | ||
process_deployment | ||
when "deployment_status" | ||
update_deployment_status | ||
end | ||
end | ||
|
||
helpers do | ||
def start_deployment(pull_request) | ||
user = pull_request['user']['login'] | ||
payload = JSON.generate(:environment => 'production', :deploy_user => user) | ||
@client.create_deployment(pull_request['head']['repo']['full_name'], pull_request['head']['sha'], {:payload => payload, :description => "Deploying my sweet branch"}) | ||
end | ||
|
||
def process_deployment | ||
payload = JSON.parse(@payload['payload']) | ||
# you can send this information to your chat room, monitor, pager, e.t.c. | ||
puts "Processing '#{@payload['description']}' for #{payload['deploy_user']} to #{payload['environment']}" | ||
sleep 2 # simulate work | ||
@client.create_deployment_status("repos/#{@payload['repository']['full_name']}/deployments/#{@payload['id']}", 'pending') | ||
sleep 2 # simulate work | ||
@client.create_deployment_status("repos/#{@payload['repository']['full_name']}/deployments/#{@payload['id']}", 'success') | ||
end | ||
|
||
def update_deployment_status | ||
puts "Deployment status for #{@payload['id']} is #{@payload['state']}" | ||
end | ||
end | ||
end |