Class: PipelineDefinition

Inherits:
Object
  • Object
show all
Defined in:
app/models/pipeline_definition.rb

Direct Known Subclasses

BasicPipeline

Defined Under Namespace

Classes: PipelineConfigError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipeline, event) ⇒ PipelineDefinition

Returns a new instance of PipelineDefinition.



36
37
38
39
40
# File 'app/models/pipeline_definition.rb', line 36

def initialize(pipeline, event)
  @pipeline = pipeline
  @event = event
  @job_creation_idx = 0
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



2
3
4
# File 'app/models/pipeline_definition.rb', line 2

def event
  @event
end

#pipelineObject (readonly)

Returns the value of attribute pipeline.



2
3
4
# File 'app/models/pipeline_definition.rb', line 2

def pipeline
  @pipeline
end

Class Method Details

.as_jsonObject



91
92
93
94
95
96
# File 'app/models/pipeline_definition.rb', line 91

def self.as_json
  {
      stages: stages.map { |stage| stage_options.merge(stage: stage) },
      name: name,
  }
end

.ensure_stage(name) ⇒ Object



32
33
34
# File 'app/models/pipeline_definition.rb', line 32

def self.ensure_stage(name)
  ensure_stages << name.to_sym
end

.ensure_stagesObject



23
24
25
# File 'app/models/pipeline_definition.rb', line 23

def self.ensure_stages
  @ensure ||= []
end

.stage(name, opts = {}) ⇒ Object



27
28
29
30
# File 'app/models/pipeline_definition.rb', line 27

def self.stage(name, opts={})
  stages << name.to_sym
  stage_options[name.to_sym] = opts
end

.stage_optionsObject



19
20
21
# File 'app/models/pipeline_definition.rb', line 19

def self.stage_options
  @stage_options ||= {}
end

.stagesObject



15
16
17
# File 'app/models/pipeline_definition.rb', line 15

def self.stages
  @stages ||= []
end

.start(event) ⇒ Object



7
8
9
# File 'app/models/pipeline_definition.rb', line 7

def self.start(event)
  PipelineRunnerJob.perform_later(nil, event.data, self.name)
end

.trigger_when?(event) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'app/models/pipeline_definition.rb', line 11

def self.trigger_when?(event)
  true
end

Instance Method Details

#create_job(key: nil, project: nil, scm: nil, spec: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/pipeline_definition.rb', line 42

def create_job(key: nil, project: nil, scm: nil, spec: nil)
  project = self.project unless project

  scm = {} if scm.nil?
  if project
    scm[:owner] = project.repo_owner
    scm[:name] = project.repo_name
    scm[:provider] = project.repo_provider
  end

  @job_creation_idx += 1
  key = "#{pipeline.definition.underscore}.#{pipeline.id}.#{stage}-#{@job_creation_idx}" unless key

  Job.create!(
      pipeline: pipeline,
      pipeline_stage: stage,
      status: 'QUEUED',
      spec: spec.merge(repo: scm),
      commit: scm[:commit],
      branch: scm[:branch],
      key: key,
  )
end

#nextObject



79
80
81
# File 'app/models/pipeline_definition.rb', line 79

def next
  pipeline.run
end

#projectObject



71
72
73
# File 'app/models/pipeline_definition.rb', line 71

def project
  @project ||= pipeline.project
end

#project=(project) ⇒ Object



66
67
68
69
# File 'app/models/pipeline_definition.rb', line 66

def project=(project)
  pipeline.update!(project_id: project.id)
  @project = project
end

#saveObject



87
88
89
# File 'app/models/pipeline_definition.rb', line 87

def save
  pipeline.update!(variables: variables.to_h)
end

#stageObject



75
76
77
# File 'app/models/pipeline_definition.rb', line 75

def stage
  pipeline.stage
end

#variablesObject



83
84
85
# File 'app/models/pipeline_definition.rb', line 83

def variables
  @variables ||= OpenStruct.new(pipeline.variables)
end