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

#_compression_type_to_switch(type) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/capistrano/recipes/deploy/strategy/jenkins_artifact.rb', line 48

def _compression_type_to_switch(type)
  case type
  when :gzip  then 'z'
  when :bzip2 then 'j'
  when :xz    then 'J'
  when :raw   then '' # raw tarball
  else abort "Invalid compression type: #{type}"
  end
end

#_guess_compression_type(filename) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/capistrano/recipes/deploy/strategy/jenkins_artifact.rb', line 33

def _guess_compression_type(filename)
  case filename.downcase
  when /\.tar\.gz$/, /\.tgz$/
    :gzip
  when /\.tar\.bz2$/, /\.tbz$/
    :bzip2
  when /\.tar\.xz$/, /\.txz$/
    :xz
  when /\.tar$/
    :raw
  else
    :bzip2
  end
end

#deploy!Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/capistrano/recipes/deploy/strategy/jenkins_artifact.rb', line 58

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_last_successful_artifact_with_path(dir_name, fetch(:artifact_relative_path))
    else
      uri = client.job.find_last_successful_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

  last_successful_build = client.job.get_last_successful_build(dir_name)
  deploy_at = Time.at(last_successful_build['timestamp'] / 1000)

  compression_type = fetch(
    :artifact_compression_type,
    _guess_compression_type(fetch(:artifact_url))
  )
  compression_switch = _compression_type_to_switch(compression_type)

  tar_opts = []
  strip_level = fetch(:artifact_strip_level, 1)
  if strip_level && strip_level > 0
    tar_opts << "--strip-components=#{strip_level}"
  end

  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 "    mkdir -p \#{fetch(:release_path)} && \\\n    (curl -s \#{fetch(:artifact_url)} | \\\n    tar \#{tar_opts.join(' ')} -C \#{fetch(:release_path)} -\#{compression_switch}xf -)\n  SCRIPT\nend\n"