Created
March 29, 2018 08:17
-
-
Save eagletmt/f66150364d2f88daa20da7c1ab84ea13 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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