Class: Capistrano::Deploy::Strategy::JenkinsArtifact

Inherits:
Base
  • Object
show all
Defined in:
lib/capistrano/recipes/deploy/strategy/jenkins_artifact.rb

Instance Method Summary collapse

Instance Method Details

#deploy!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/capistrano/recipes/deploy/strategy/jenkins_artifact.rb', line 27

def deploy!
  dir_name = exists?(:is_multibranch_job) && fetch(:is_multibranch_job) ? fetch(:branch) : fetch(:build_project)

  jenkins_origin = fetch(:jenkins_origin) or abort ":jenkins_origin configuration must be defined"
  client = JenkinsApi::Client.new(server_url: jenkins_origin.to_s)
  set(:artifact_url) do
    uri = ''
    if exists?(:artifact_relative_path)
      uri = client.job.find_artifact_with_path(dir_name, fetch(:artifact_relative_path))
    else
      uri = client.job.find_artifact(dir_name)
    end
    abort "No artifact found for #{dir_name}" if uri.empty?
    URI.parse(uri).tap {|uri|
      uri.scheme = jenkins_origin.scheme
      uri.host = jenkins_origin.host
      uri.port = jenkins_origin.port
    }.to_s
  end

  build_num = client.job.get_last_successful_build_number(dir_name, "origin/#{fetch(:branch)}")
  timestamp = client.job.get_build_details(dir_name, build_num)['timestamp']
  deploy_at = Time.at(timestamp / 1000)

  set(:release_name, deploy_at.strftime('%Y%m%d%H%M%S'))
  set(:release_path, "#{fetch(:releases_path)}/#{fetch(:release_name)}")
  set(:latest_release, fetch(:release_path))

  run <<-SCRIPT
    mkdir -p #{fetch(:release_path)} && \
    (curl -s #{fetch(:artifact_url)} | \
    tar --strip-components=1 -C #{fetch(:release_path)} -jxf -)
  SCRIPT
end