Class: Capistrano::Deploy::SCM::Bamboo

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

Instance Method Summary collapse

Instance Method Details

#artifact_nameObject



12
13
14
# File 'lib/capistrano/recipes/deploy/scm/bamboo.rb', line 12

def artifact_name
  @artifact_name
end

#checkout(revision, destination) ⇒ Object Also known as: export



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/capistrano/recipes/deploy/scm/bamboo.rb', line 37

def checkout(revision, destination)
  # TODO: graceful error handling
  result = load_result

  artifact = result["artifacts"]["artifact"].select { |artifact| artifact["name"] == variable(:artifact) }

  if (artifact.empty?)
    raise ArgumentError, "[err] build artifacts not found: perhaps you didn't correctly specify the artifact parameter?"
  end

  artifactUrl = artifact[0]["link"]["href"]
  build_actual = result["number"]

  # now do clever hackiness to detect whether this is a directory to be synced, or a single inline file/attachment
  artifact_headers = Typhoeus::Request.head(artifactUrl).headers_hash

  if (!artifact_headers.include?("Content-Disposition"))
    @artifact_name = variable(:artifact)
    %Q{TMPDIR=`mktemp -d -t tmp.XXXXXXXXXX` && cd $TMPDIR && wget -m -nH -q #{artifactUrl} && mv artifact/#{plan_key}/shared/build-#{build_actual}/#{variable(:artifact)}/ "#{destination}" && rm -rf "$TMPDIR"}
  else
    # if there's a content disposition of attachment, downolad the file directly. if inline, then ?. if no content disposition, then wget the whole directory.
    artifact_content_disposition = artifact_headers["Content-Disposition"]

    # get the filename
    @artifact_name = artifact_content_disposition.match(/filename="(.*?)"/)[1]
    %Q{TMPDIR=`mktemp -d -t tmp.XXXXXXXXXX` && cd $TMPDIR && wget -m -nH -q #{artifactUrl} -O #{@artifact_name} && mkdir #{destination} && mv #{@artifact_name} "#{destination}/" && rm -rf "$TMPDIR"}
  end
  
  ## previous artifact copy when things were only directories. for posterity only.
  # %Q{TMPDIR=`mktemp -d` && cd $TMPDIR && wget -m -nH -q #{artifactUrl} && mv artifact/#{plan_key}/shared/build-#{build_actual}/#{variable(:artifact)}/ "#{destination}" && rm -rf "$TMPDIR"}
rescue ArgumentError => e
  logger.log(Logger::IMPORTANT, e.message)
  exit
end

#diff(from, to = nil) ⇒ Object

def log(from, to=nil)

log_build_message(from, to)
log_scm_message(from, to)
'true'

end



91
92
93
94
# File 'lib/capistrano/recipes/deploy/scm/bamboo.rb', line 91

def diff(from, to=nil)
  logger.info 'bamboo does not support diff'
  'true'
end

#headObject



21
22
23
# File 'lib/capistrano/recipes/deploy/scm/bamboo.rb', line 21

def head
  "#{plan_key}-#{query_revision(variable(:build_number))}"
end

#load_resultObject



16
17
18
19
# File 'lib/capistrano/recipes/deploy/scm/bamboo.rb', line 16

def load_result
  response = Typhoeus::Request.get("#{repository}/result/#{plan_key}/#{variable(:build_number)}.json?expand=artifacts", :userpwd => "#{variable(:scm_username)}:#{variable(:scm_passphrase)}")
  result = JSON.parse(response.body)
end

#plan_keyObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/capistrano/recipes/deploy/scm/bamboo.rb', line 72

def plan_key
  if (variable(:plan_key)) 
    variable(:plan_key)
  elsif (variable(:build_key))
    pk = variable(:build_key)
    pk.slice(0...pk.rindex('-'))
  else
    puts "d'oh"
  end
end

#query_revision(revision) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/capistrano/recipes/deploy/scm/bamboo.rb', line 25

def query_revision(revision)
  if (revision.to_s =~ /\d+$/)
    revision
  elsif (revision =~ /latest$/) 
    result = load_result
    
    result["number"]
  else
    raise "invalid revision: #{revision}"
  end
end