Class: Artifactory::Resource::Build

Inherits:
Base
  • Object
show all
Defined in:
lib/artifactory/resources/build.rb

Constant Summary collapse

BUILD_SCHEMA_VERSION =
'1.0.1'.freeze
BUILD_TYPES =

valid build types as dictated by the Artifactory API

%w( ANT IVY MAVEN GENERIC GRADLE )

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

attribute, attributes, #attributes, #client, #client=, #client?, #extract_client!, extract_client!, find_from_config, format_repos!, #format_repos!, from_url, has_attribute?, #initialize, #inspect, list_from_config, #set, #to_hash, #to_json, #to_matrix_properties, #to_query_string_parameters, #to_s, url_safe, #url_safe

Constructor Details

This class inherits a constructor from Artifactory::Resource::Base

Class Method Details

.all(name, options = {}) ⇒ Array<Resource::Build>

Search for all builds in the system.

Options Hash (options):



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/artifactory/resources/build.rb', line 40

def all(name, options = {})
  client = extract_client!(options)
  client.get("/api/build/#{url_safe(name)}")['buildsNumbers'].map do |build_number|
    # Remove the leading / from the `uri` value. Converts `/484` to `484`.
    number = build_number['uri'].slice(1..-1)
    find(name, number, client: client)
  end.compact.flatten
rescue Error::HTTPError => e
  # Artifactory returns a 404 instead of an empty list when there are no
  # builds. Whoever decided that was a good idea clearly doesn't
  # understand the point of REST interfaces...
  raise unless e.code == 404
  []
end

.find(name, number, options = {}) ⇒ Resource::Build?

Find (fetch) data for a particular build of a component

Examples:

Find data for a build of a component

Build.find('wicket', 25) #=> #<Build name: 'wicket' ...>

Options Hash (options):



75
76
77
78
79
80
81
82
# File 'lib/artifactory/resources/build.rb', line 75

def find(name, number, options = {})
  client = extract_client!(options)
  response = client.get("/api/build/#{url_safe(name)}/#{url_safe(number)}")
  from_hash(response['buildInfo'], client: client)
rescue Error::HTTPError => e
  raise unless e.code == 404
  nil
end

.from_hash(hash, options = {}) ⇒ Object



87
88
89
90
91
92
# File 'lib/artifactory/resources/build.rb', line 87

def from_hash(hash, options = {})
  super.tap do |instance|
    instance.started = Time.parse(instance.started) rescue nil
    instance.duration_millis = instance.duration_millis.to_i
  end
end

Instance Method Details

#agentObject

Return this object’s agent



102
# File 'lib/artifactory/resources/build.rb', line 102

attribute :agent, {}

#agent=(value) ⇒ Object

Set this object’s agent



102
# File 'lib/artifactory/resources/build.rb', line 102

attribute :agent, {}

#agent?Boolean

Determines if the agent value exists and is truthy



102
# File 'lib/artifactory/resources/build.rb', line 102

attribute :agent, {}

#artifactory_principalObject

Return this object’s artifactory_principal



105
# File 'lib/artifactory/resources/build.rb', line 105

attribute :artifactory_principal

#artifactory_principal=(value) ⇒ Object

Set this object’s artifactory_principal



105
# File 'lib/artifactory/resources/build.rb', line 105

attribute :artifactory_principal

#artifactory_principal?Boolean

Determines if the artifactory_principal value exists and is truthy



105
# File 'lib/artifactory/resources/build.rb', line 105

attribute :artifactory_principal

#build_agentObject

Return this object’s build_agent



101
# File 'lib/artifactory/resources/build.rb', line 101

attribute :build_agent, {}

#build_agent=(value) ⇒ Object

Set this object’s build_agent



101
# File 'lib/artifactory/resources/build.rb', line 101

attribute :build_agent, {}

#build_agent?Boolean

Determines if the build_agent value exists and is truthy



101
# File 'lib/artifactory/resources/build.rb', line 101

attribute :build_agent, {}

#build_retentionObject

Return this object’s build_retention



110
# File 'lib/artifactory/resources/build.rb', line 110

attribute :build_retention, {}

#build_retention=(value) ⇒ Object

Set this object’s build_retention



110
# File 'lib/artifactory/resources/build.rb', line 110

attribute :build_retention, {}

#build_retention?Boolean

Determines if the build_retention value exists and is truthy



110
# File 'lib/artifactory/resources/build.rb', line 110

attribute :build_retention, {}

#diff(previous_build_number) ⇒ Hash<String, Hash>

Compare a build artifacts/dependencies/environment with an older build to see what has changed (new artifacts added, old dependencies deleted etc).

Examples:

List all properties for an artifact

build.diff(35) #=> { 'artifacts'=>{}, 'dependencies'=>{}, 'properties'=>{} }


128
129
130
131
# File 'lib/artifactory/resources/build.rb', line 128

def diff(previous_build_number)
  endpoint = api_path + '?' "diff=#{url_safe(previous_build_number)}"
  client.get(endpoint, {})
end

#duration_millisObject

Return this object’s duration_millis



104
# File 'lib/artifactory/resources/build.rb', line 104

attribute :duration_millis

#duration_millis=(value) ⇒ Object

Set this object’s duration_millis



104
# File 'lib/artifactory/resources/build.rb', line 104

attribute :duration_millis

#duration_millis?Boolean

Determines if the duration_millis value exists and is truthy



104
# File 'lib/artifactory/resources/build.rb', line 104

attribute :duration_millis

#governanceObject

Return this object’s governance



112
# File 'lib/artifactory/resources/build.rb', line 112

attribute :governance

#governance=(value) ⇒ Object

Set this object’s governance



112
# File 'lib/artifactory/resources/build.rb', line 112

attribute :governance

#governance?Boolean

Determines if the governance value exists and is truthy



112
# File 'lib/artifactory/resources/build.rb', line 112

attribute :governance

#license_controlObject

Return this object’s license_control



109
# File 'lib/artifactory/resources/build.rb', line 109

attribute :license_control, {}

#license_control=(value) ⇒ Object

Set this object’s license_control



109
# File 'lib/artifactory/resources/build.rb', line 109

attribute :license_control, {}

#license_control?Boolean

Determines if the license_control value exists and is truthy



109
# File 'lib/artifactory/resources/build.rb', line 109

attribute :license_control, {}

#modulesObject

Return this object’s modules



111
# File 'lib/artifactory/resources/build.rb', line 111

attribute :modules, []

#modules=(value) ⇒ Object

Set this object’s modules



111
# File 'lib/artifactory/resources/build.rb', line 111

attribute :modules, []

#modules?Boolean

Determines if the modules value exists and is truthy



111
# File 'lib/artifactory/resources/build.rb', line 111

attribute :modules, []

#nameObject

Return this object’s name



98
# File 'lib/artifactory/resources/build.rb', line 98

attribute :name, ->{ raise 'Build component missing!' }

#name=(value) ⇒ Object

Set this object’s name



98
# File 'lib/artifactory/resources/build.rb', line 98

attribute :name, ->{ raise 'Build component missing!' }

#name?Boolean

Determines if the name value exists and is truthy



98
# File 'lib/artifactory/resources/build.rb', line 98

attribute :name, ->{ raise 'Build component missing!' }

#numberObject

Return this object’s number



99
# File 'lib/artifactory/resources/build.rb', line 99

attribute :number, ->{ raise 'Build number missing!' }

#number=(value) ⇒ Object

Set this object’s number



99
# File 'lib/artifactory/resources/build.rb', line 99

attribute :number, ->{ raise 'Build number missing!' }

#number?Boolean

Determines if the number value exists and is truthy



99
# File 'lib/artifactory/resources/build.rb', line 99

attribute :number, ->{ raise 'Build number missing!' }

#promote(target_repo, options = {}) ⇒ Hash

Move a build’s artifacts to a new repository optionally moving or copying the build’s dependencies to the target repository and setting properties on promoted artifacts.

Examples:

promote the build to ‘omnibus-stable-local’

build.promote('omnibus-stable-local')

promote a build attaching some new properites

build.promote('omnibus-stable-local'
  properties: {
    'promoted_by' => 'hipchat:[email protected]'
  }
)

Options Hash (options):

  • :status (String) — default: default: 'promoted'

    new build status (any string)

  • :comment (String) — default: default: ''

    an optional comment describing the reason for promotion

  • :user (String) — default: default: +Artifactory.username+

    the user that invoked promotion

  • :dry_run (Boolean) — default: default: +false+

    pretend to do the promotion

  • :copy (Boolean) — default: default: +false+

    whether to copy instead of move

  • :dependencies (Boolean) — default: default: +false+

    whether to move/copy the build’s dependencies

  • :scopes (Array) — default: default: []

    an array of dependency scopes to include when “dependencies” is true

  • :properties (Hash<String, Array<String>>) — default: default: []

    a list of properties to attach to the build’s artifacts

  • :fail_fast (Boolean) — default: default: +true+

    fail and abort the operation upon receiving an error



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/artifactory/resources/build.rb', line 174

def promote(target_repo, options = {})
  request_body = {}.tap do |body|
    body[:status]       = options[:status] || 'promoted'
    body[:comment]      = options[:comment] || ''
    body[:ciUser]       = options[:user] || Artifactory.username
    body[:dryRun]       = options[:dry_run] || false
    body[:targetRepo]   = target_repo
    body[:copy]         = options[:copy] || false
    body[:artifacts]    = true # always move/copy the build's artifacts
    body[:dependencies] = options[:dependencies] || false
    body[:scopes]       = options[:scopes] || []
    body[:properties]   = options[:properties] || {}
    body[:failFast]     = options[:fail_fast] || true
  end

  endpoint = "/api/build/promote/#{url_safe(name)}/#{url_safe(number)}"
  client.post(endpoint, JSON.fast_generate(request_body),
    'Content-Type' => 'application/json'
  )
end

#propertiesObject

Return this object’s properties



96
# File 'lib/artifactory/resources/build.rb', line 96

attribute :properties, {}

#properties=(value) ⇒ Object

Set this object’s properties



96
# File 'lib/artifactory/resources/build.rb', line 96

attribute :properties, {}

#properties?Boolean

Determines if the properties value exists and is truthy



96
# File 'lib/artifactory/resources/build.rb', line 96

attribute :properties, {}

#saveBoolean

Creates data about a build.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/artifactory/resources/build.rb', line 200

def save
  raise Error::InvalidBuildType.new(type) unless BUILD_TYPES.include?(type)

  file = Tempfile.new("build.json")
  file.write(to_json)
  file.rewind

  client.put('/api/build', file,
    'Content-Type' => 'application/json'
  )
  true
ensure
  if file
    file.close
    file.unlink
  end
end

#startedObject

Return this object’s started



103
# File 'lib/artifactory/resources/build.rb', line 103

attribute :started, Time.now.utc.iso8601(3)

#started=(value) ⇒ Object

Set this object’s started



103
# File 'lib/artifactory/resources/build.rb', line 103

attribute :started, Time.now.utc.iso8601(3)

#started?Boolean

Determines if the started value exists and is truthy



103
# File 'lib/artifactory/resources/build.rb', line 103

attribute :started, Time.now.utc.iso8601(3)

#typeObject

Return this object’s type



100
# File 'lib/artifactory/resources/build.rb', line 100

attribute :type, 'GENERIC'

#type=(value) ⇒ Object

Set this object’s type



100
# File 'lib/artifactory/resources/build.rb', line 100

attribute :type, 'GENERIC'

#type?Boolean

Determines if the type value exists and is truthy



100
# File 'lib/artifactory/resources/build.rb', line 100

attribute :type, 'GENERIC'

#urlObject

Return this object’s url



106
# File 'lib/artifactory/resources/build.rb', line 106

attribute :url

#url=(value) ⇒ Object

Set this object’s url



106
# File 'lib/artifactory/resources/build.rb', line 106

attribute :url

#url?Boolean

Determines if the url value exists and is truthy



106
# File 'lib/artifactory/resources/build.rb', line 106

attribute :url

#vcs_revisionObject

Return this object’s vcs_revision



107
# File 'lib/artifactory/resources/build.rb', line 107

attribute :vcs_revision

#vcs_revision=(value) ⇒ Object

Set this object’s vcs_revision



107
# File 'lib/artifactory/resources/build.rb', line 107

attribute :vcs_revision

#vcs_revision?Boolean

Determines if the vcs_revision value exists and is truthy



107
# File 'lib/artifactory/resources/build.rb', line 107

attribute :vcs_revision

#vcs_urlObject

Return this object’s vcs_url



108
# File 'lib/artifactory/resources/build.rb', line 108

attribute :vcs_url

#vcs_url=(value) ⇒ Object

Set this object’s vcs_url



108
# File 'lib/artifactory/resources/build.rb', line 108

attribute :vcs_url

#vcs_url?Boolean

Determines if the vcs_url value exists and is truthy



108
# File 'lib/artifactory/resources/build.rb', line 108

attribute :vcs_url

#versionObject

Return this object’s version



97
# File 'lib/artifactory/resources/build.rb', line 97

attribute :version, BUILD_SCHEMA_VERSION

#version=(value) ⇒ Object

Set this object’s version



97
# File 'lib/artifactory/resources/build.rb', line 97

attribute :version, BUILD_SCHEMA_VERSION

#version?Boolean

Determines if the version value exists and is truthy



97
# File 'lib/artifactory/resources/build.rb', line 97

attribute :version, BUILD_SCHEMA_VERSION