Class: Buildkite::Builder::Pipeline

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Definition::Helper, LoggingUtils
Defined in:
lib/buildkite/builder/pipeline.rb

Constant Summary collapse

PIPELINE_DEFINITION_FILE =
Pathname.new('pipeline.rb').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoggingUtils

#benchmark, #pluralize

Methods included from Definition::Helper

#load_definition

Constructor Details

#initialize(root, logger: nil) ⇒ Pipeline

Returns a new instance of Pipeline.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/buildkite/builder/pipeline.rb', line 26

def initialize(root, logger: nil)
  @root = root
  @logger = logger || Logger.new(File::NULL)
  @artifacts = []
  @dsl = Dsl.new(self)
  @extensions = ExtensionManager.new(self)
  @data = Data.new

  use(Extensions::Use)
  use(Extensions::Lib)
  use(Extensions::Env)
  use(Extensions::Notify)
  use(Extensions::Steps)
  use(Extensions::Plugins)
  use(Extensions::Agents)
end

Instance Attribute Details

#artifactsObject (readonly)

Returns the value of attribute artifacts.



19
20
21
# File 'lib/buildkite/builder/pipeline.rb', line 19

def artifacts
  @artifacts
end

#dataObject (readonly)

Returns the value of attribute data.



19
20
21
# File 'lib/buildkite/builder/pipeline.rb', line 19

def data
  @data
end

#dslObject (readonly)

Returns the value of attribute dsl.



19
20
21
# File 'lib/buildkite/builder/pipeline.rb', line 19

def dsl
  @dsl
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



19
20
21
# File 'lib/buildkite/builder/pipeline.rb', line 19

def extensions
  @extensions
end

#loggerObject (readonly)

Returns the value of attribute logger.



19
20
21
# File 'lib/buildkite/builder/pipeline.rb', line 19

def logger
  @logger
end

#rootObject (readonly)

Returns the value of attribute root.



19
20
21
# File 'lib/buildkite/builder/pipeline.rb', line 19

def root
  @root
end

Instance Method Details

#to_hObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/buildkite/builder/pipeline.rb', line 73

def to_h
  @pipeline_hash ||= begin
    results = benchmark("\nDone (%s)".color(:springgreen)) do
      dsl.instance_eval(&pipeline_definition)
      extensions.build
    end
    logger.info(results)
    # Build the pipeline definition from pipeline data.
    Pipelines::Helpers.sanitize(data.to_definition)
  end
end

#to_yamlObject



85
86
87
# File 'lib/buildkite/builder/pipeline.rb', line 85

def to_yaml
  YAML.dump(to_h)
end

#uploadObject



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
71
# File 'lib/buildkite/builder/pipeline.rb', line 43

def upload
  # Generate the pipeline YAML first.
  contents = to_yaml

  upload_artifacts

  if data.steps.empty?
    logger.info "+++ :pipeline: No steps defined, skipping pipeline upload"
  else
    # Upload the pipeline.
    Tempfile.create(['pipeline', '.yml']) do |file|
      file.sync = true
      file.write(contents)

      logger.info "+++ :pipeline: Uploading pipeline"

      result = Buildkite::Pipelines::Command.pipeline(:upload, file.path)

      unless result.success?
        logger.info "Pipeline upload failed with #{result.stderr}, saving as artifact…"
        Buildkite::Pipelines::Command.artifact!(:upload, file.path)
        abort
      end
    end
  end

  logger.info "+++ :toolbox: Setting job meta-data to #{Buildkite.env.job_id.color(:yellow)}"
  Buildkite::Pipelines::Command.meta_data!(:set, Builder..fetch(:job), Buildkite.env.step_id)
end