Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge pull request #19374 from code-dot-org/start-circle-build #19393

Merged
merged 1 commit into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions bin/cron/trigger_circle_build
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env ruby

require 'net/http'
require_relative '../../deployment'
require 'cdo/chat_client'
require 'cdo/only_one'

# Base URL for the CircleCI REST API
CIRCLE_API_BASE = 'https://circleci.com/api/v1.1'.freeze

def circle_uri(path)
URI("#{CIRCLE_API_BASE}/#{path}?circle-token=#{CDO.start_circle_builds_token}")
end

def call_api(url, method = 'GET')
uri = circle_uri(url)

case method.upcase
when 'GET'
req = Net::HTTP::Get.new(uri)
when 'PUT'
req = Net::HTTP::Put.new(uri)
when 'POST'
req = Net::HTTP::Post.new(uri)
else
raise "Unknown method #{method}"
end

req['Content-Type'] = 'application/json'

http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true

response = http.start {http.request(req)}

if response.code == "200"
JSON.parse(response.body)
else
raise "HTTP Error #{response.code}: #{response.message} #{response.body}"
end
end

# Run a circle build of the latest commit on the current branch
# Only useful when we have a non-default branch that we want to run regular
# builds against (like staging-next).
def main
return unless CDO.start_circle_builds_token && CDO.start_circle_builds_branch
branchname = CDO.start_circle_builds_branch

last_build = call_api("project/github/code-dot-org/code-dot-org/tree/#{branchname}")[0]
return unless last_build['lifecycle'] == 'not_run'

call_api("project/github/code-dot-org/code-dot-org/tree/#{branchname}", 'POST')
rescue Exception => e
ChatClient.message(
'infra-staging-next',
"Failed to start a Circle build: #{e.message}",
color: 'yellow'
)
end

main if only_one_running?(__FILE__)
1 change: 1 addition & 0 deletions cookbooks/cdo-apps/templates/default/crontab.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

if node.chef_environment == 'test' && node.name == 'staging-next'
cronjob at:'*/1 * * * *', do:deploy_dir('bin', 'cron', 'update_dtsn')
cronjob at:'*/5 * * * *', do:deploy_dir('bin', 'cron', 'trigger_circle_build')
end

if node.chef_environment == 'test' && node.name == 'test' # 'real' test only
Expand Down