Class: Ci::Workloads::WorkloadDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/ci/workloads/workload_definition.rb

Overview

This class knows how to take a minimal set of attributes and construct a valid CI job yaml definition. It may in future be able to construct the definitions for other ways of running a workload (e.g. CI steps)

Constant Summary collapse

DEFAULT_TIMEOUT =
2.hours

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ WorkloadDefinition

Returns a new instance of WorkloadDefinition.

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
23
# File 'lib/ci/workloads/workload_definition.rb', line 18

def initialize
  self.timeout = DEFAULT_TIMEOUT
  @variables = {}
  @commands = []
  yield self if block_given?
end

Instance Attribute Details

#artifacts_pathsObject

Returns the value of attribute artifacts_paths.



10
11
12
# File 'lib/ci/workloads/workload_definition.rb', line 10

def artifacts_paths
  @artifacts_paths
end

#cacheObject

Returns the value of attribute cache.



10
11
12
# File 'lib/ci/workloads/workload_definition.rb', line 10

def cache
  @cache
end

#commandsObject

Returns the value of attribute commands.



10
11
12
# File 'lib/ci/workloads/workload_definition.rb', line 10

def commands
  @commands
end

#imageObject

Returns the value of attribute image.



10
11
12
# File 'lib/ci/workloads/workload_definition.rb', line 10

def image
  @image
end

#tagsObject

Returns the value of attribute tags.



10
11
12
# File 'lib/ci/workloads/workload_definition.rb', line 10

def tags
  @tags
end

#timeoutObject

Returns the value of attribute timeout.



10
11
12
# File 'lib/ci/workloads/workload_definition.rb', line 10

def timeout
  @timeout
end

#variablesObject

Returns the value of attribute variables.



10
11
12
# File 'lib/ci/workloads/workload_definition.rb', line 10

def variables
  @variables
end

Instance Method Details

#add_variable(name, value) ⇒ Object



25
26
27
# File 'lib/ci/workloads/workload_definition.rb', line 25

def add_variable(name, value)
  self.variables = variables.merge(name => value)
end

#to_job_hashObject

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ci/workloads/workload_definition.rb', line 29

def to_job_hash
  raise ArgumentError, "image cannot be empty" unless image.present?
  raise ArgumentError, "commands cannot be empty" unless commands.any?

  result = {
    image: image,
    stage: 'build',
    timeout: "#{timeout} seconds",
    variables: variables_without_expand,
    script: commands
  }

  result[:artifacts] = { paths: artifacts_paths } if artifacts_paths.present?
  result[:cache] = cache if cache.present?

  result[:tags] = tags if tags.present?

  result
end