Fix up the sample pre-deploy hook
- Fix the shebang - Extract a GithubStatusChecks class
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
#!/bin/sh
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
# A sample pre-deploy hook
|
# A sample pre-deploy hook
|
||||||
#
|
#
|
||||||
@@ -16,8 +16,6 @@
|
|||||||
# MRSK_ROLE (if set)
|
# MRSK_ROLE (if set)
|
||||||
# MRSK_DESTINATION (if set)
|
# MRSK_DESTINATION (if set)
|
||||||
|
|
||||||
#!/usr/bin/env ruby
|
|
||||||
|
|
||||||
# Only check the build status for production deployments
|
# Only check the build status for production deployments
|
||||||
if ENV["MRSK_COMMAND"] == "rollback" || ENV["MRSK_DESTINATION"] != "production"
|
if ENV["MRSK_COMMAND"] == "rollback" || ENV["MRSK_DESTINATION"] != "production"
|
||||||
exit 0
|
exit 0
|
||||||
@@ -41,41 +39,70 @@ def exit_with_error(message)
|
|||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
def first_status_url(combined_status, state)
|
class GithubStatusChecks
|
||||||
first_status = combined_status[:statuses].find { |status| status[:state] == state }
|
attr_reader :remote_url, :git_sha, :github_client, :combined_status
|
||||||
first_status && first_status[:target_url]
|
|
||||||
|
def initialize
|
||||||
|
@remote_url = `git config --get remote.origin.url`.strip.delete_prefix("https://github.com/")
|
||||||
|
@git_sha = `git rev-parse HEAD`.strip
|
||||||
|
@github_client = Octokit::Client.new(access_token: ENV["GITHUB_TOKEN"])
|
||||||
|
refresh!
|
||||||
|
end
|
||||||
|
|
||||||
|
def refresh!
|
||||||
|
@combined_status = github_client.combined_status(remote_url, git_sha)
|
||||||
|
end
|
||||||
|
|
||||||
|
def state
|
||||||
|
combined_status[:state]
|
||||||
|
end
|
||||||
|
|
||||||
|
def first_status_url
|
||||||
|
first_status = combined_status[:statuses].find { |status| status[:state] == state }
|
||||||
|
first_status && first_status[:target_url]
|
||||||
|
end
|
||||||
|
|
||||||
|
def complete_count
|
||||||
|
combined_status[:statuses].count { |status| status[:state] != "pending"}
|
||||||
|
end
|
||||||
|
|
||||||
|
def total_count
|
||||||
|
combined_status[:statuses].count
|
||||||
|
end
|
||||||
|
|
||||||
|
def current_status
|
||||||
|
if total_count > 0
|
||||||
|
"Completed #{complete_count}/#{total_count} checks, see #{first_status_url} ..."
|
||||||
|
else
|
||||||
|
"Build not started..."
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
remote_url = `git config --get remote.origin.url`.strip.delete_prefix("https://github.com/")
|
|
||||||
git_sha = `git rev-parse HEAD`.strip
|
|
||||||
|
|
||||||
repository = Octokit::Repository.from_url(remote_url)
|
$stdout.sync = true
|
||||||
github_client = Octokit::Client.new(access_token: ENV["GITHUB_TOKEN"])
|
|
||||||
|
puts "Checking build status..."
|
||||||
attempts = 0
|
attempts = 0
|
||||||
|
checks = GithubStatusChecks.new
|
||||||
|
|
||||||
begin
|
begin
|
||||||
loop do
|
loop do
|
||||||
combined_status = github_client.combined_status(remote_url, git_sha)
|
case checks.state
|
||||||
state = combined_status[:state]
|
|
||||||
first_status_url = first_status_url(combined_status, state)
|
|
||||||
|
|
||||||
case state
|
|
||||||
when "success"
|
when "success"
|
||||||
puts "Build passed, see #{first_status_url}"
|
puts "Checks passed, see #{checks.first_status_url}"
|
||||||
exit 0
|
exit 0
|
||||||
when "failure"
|
when "failure"
|
||||||
exit_with_error "Build failed, see #{first_status_url}"
|
exit_with_error "Checks failed, see #{checks.first_status_url}"
|
||||||
when "pending"
|
when "pending"
|
||||||
attempts += 1
|
attempts += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
puts "Waiting #{ATTEMPTS_GAP} more seconds for build to complete#{", see #{first_status_url}" if first_status_url}..."
|
exit_with_error "Checks are still pending, gave up after #{MAX_ATTEMPTS * ATTEMPTS_GAP} seconds" if attempts == MAX_ATTEMPTS
|
||||||
|
|
||||||
if attempts == MAX_ATTEMPTS
|
|
||||||
exit_with_error "Build status is still pending, gave up after #{MAX_ATTEMPTS * ATTEMPTS_GAP} seconds"
|
|
||||||
end
|
|
||||||
|
|
||||||
|
puts checks.current_status
|
||||||
sleep(ATTEMPTS_GAP)
|
sleep(ATTEMPTS_GAP)
|
||||||
|
checks.refresh!
|
||||||
end
|
end
|
||||||
rescue Octokit::NotFound
|
rescue Octokit::NotFound
|
||||||
exit_with_error "Build status could not be found"
|
exit_with_error "Build status could not be found"
|
||||||
|
|||||||
Reference in New Issue
Block a user