Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'json'
require 'net/http'
require 'hako/script'
module Hako
module Scripts
class JenkinsTag < Script
JENKINS_BASE = 'https://jenkins.example.com'
TARGET_TAG = 'jenkins'
def configure(options)
super
@job_name = options.fetch('job', "docker-#{@app.id}") # default naming convention
end
def deploy_starting(containers)
app = containers.fetch('app')
if app.definition['tag'] == TARGET_TAG
rewrite_tag(app)
end
end
alias_method :oneshot_starting, :deploy_starting
private
def rewrite_tag(container)
tag = fetch_revision(@job_name)
container.definition['tag'] = tag
Hako.logger.info("Rewrite tag to #{container.image_tag}")
end
def fetch_revision(job_name)
body = Net::HTTP.get(URI.parse("#{JENKINS_BASE}/job/#{@job_name}/lastSuccessfulBuild/git/api/json"))
git_data = JSON.parse(body)
git_data['lastBuiltRevision']['SHA1']
rescue => e
Hako.logger.error("Unable to fetch revision from #{JENKINS_BASE}/job/#{@job_name}/")
raise e
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment