Skip to content

Instantly share code, notes, and snippets.

@eagletmt
Created March 29, 2018 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eagletmt/f66150364d2f88daa20da7c1ab84ea13 to your computer and use it in GitHub Desktop.
Save eagletmt/f66150364d2f88daa20da7c1ab84ea13 to your computer and use it in GitHub Desktop.
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