Skip to content

Instantly share code, notes, and snippets.

@mgreensmith
Last active January 30, 2016 00:12
Show Gist options
  • Save mgreensmith/9af1a2081516fdcc257b to your computer and use it in GitHub Desktop.
Save mgreensmith/9af1a2081516fdcc257b to your computer and use it in GitHub Desktop.
GoCD circuit breaker / readiness check script.
#!/usr/bin/env ruby
# Copyright (c) 2016 Matt Greensmith and Cozy Services Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
### GoCD circuit breaker / readiness check script.
###
### Should be configured as a downstream pipeline for all pipelines in PIPELINE_GROUP.
### When run, checks to see whether there are any pipelines currently building from the
### upstream group, and whether there are labels for upstream builds that are newer than
### the build that triggered this build. Fails if there are in-progress builds or if there
### is a build that will trigger this pipeline is the future.
require 'go_api_client'
PIPELINE_GROUP = 'my-upstream-pipelines'
GOCD_SERVER_HOST = 'my.ci.server.tld'
GOCD_SERVER_PORT = 80
failed = false
client = GoApiClient::Client.new(host:GOCD_SERVER_HOST, port: GOCD_SERVER_PORT)
pipelines = client.api(:config).pipelines(group_name: PIPELINE_GROUP)
building = []
last_build_label = {}
pipelines.each do |p|
building << p unless client.api(:cctray).projects(:pipeline_name => p.name, :activity => 'Building').empty?
last_build_label[p.name] = client.api(:cctray).projects(:pipeline_name => p.name)[0].last_build_label rescue nil
end
unless building.empty?
building.each do |p|
puts "#{p.name} is building"
end
failed = true
end
pipelines.each do |p|
# p.name looks like some-upstream-job
# env var looks like GO_DEPENDENCY_LABEL_SOME_UPSTREAM_JOB=6
env_var_name = "GO_DEPENDENCY_LABEL_#{p.name.upcase.gsub(/-/, '_')}"
current_build_label = ENV[env_var_name]
if current_build_label == last_build_label[p.name]
puts "#{p.name} build label #{current_build_label} triggered me, it is the newest build"
else
puts "#{p.name} build label #{current_build_label} triggered me, but newer label #{last_build_label[p.name]} exists"
failed = true
end
end
if failed
puts "Not ready to deploy."
exit 1
end
puts 'Ready to deploy!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment