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

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

Defined Under Namespace

Modules: ApiClient, Helpers

Instance Method Summary collapse

Instance Method Details

#_compression_type_to_switch(type) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/capistrano/recipes/deploy/strategy/jenkins_artifact.rb', line 55

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/capistrano/recipes/deploy/strategy/jenkins_artifact.rb', line 40

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



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
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/capistrano/recipes/deploy/strategy/jenkins_artifact.rb', line 65

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

  release_name_from = fetch(:release_name_from, :build_at)
  if release_name_from != :build_at && release_name_from != :deploy_at
    abort ':release_name_from must be either `:build_at` or `:deploy_at`'
  end

  jenkins_origin = fetch(:jenkins_origin) or abort ":jenkins_origin configuration must be defined"
  last_successful_build = ApiClient.get_last_successful_build(jenkins_origin, dir_name)
  build_at = Time.at(last_successful_build['timestamp'] / 1000)

  set(:artifact_url) do
    artifact_finder = exists?(:artifact_relative_path) ?
      ->(artifact) { artifact['relativePath'] == fetch(:artifact_relative_path) } :
      ->(artifact) { true }
    uri = Helpers.get_artifact_url_by_build(last_successful_build, &artifact_finder)
    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

  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

  if release_name_from == :build_at
    set(:release_name, build_at.strftime('%Y%m%d%H%M%S'))
  end
  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"