Class: Buildkiq::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/buildkiq/job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_name:, environments:, logger: Logger.new(STDOUT)) ⇒ Job

Returns a new instance of Job.



8
9
10
11
12
13
# File 'lib/buildkiq/job.rb', line 8

def initialize(project_name:, environments:, logger: Logger.new(STDOUT))
  @client       = Aws::CodeBuild::Client.new
  @project_name = project_name
  @environments = environments
  @logger       = logger
end

Instance Attribute Details

#buildObject (readonly)

Returns the value of attribute build.



6
7
8
# File 'lib/buildkiq/job.rb', line 6

def build
  @build
end

#environmentsObject (readonly)

Returns the value of attribute environments.



6
7
8
# File 'lib/buildkiq/job.rb', line 6

def environments
  @environments
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/buildkiq/job.rb', line 6

def logger
  @logger
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



6
7
8
# File 'lib/buildkiq/job.rb', line 6

def project_name
  @project_name
end

Instance Method Details

#artifactObject



59
60
61
# File 'lib/buildkiq/job.rb', line 59

def artifact
  @artifact ||= Artifact.new(self)
end

#artifact_upload_success?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/buildkiq/job.rb', line 49

def artifact_upload_success?
  !fetch_build.phases.select { |v|
    v.phase_type == "UPLOAD_ARTIFACTS" && v.phase_status == "SUCCEEDED"
  }.empty?
end

#build_urlObject



41
42
43
# File 'lib/buildkiq/job.rb', line 41

def build_url
  "https://#{@client.config.region}.console.aws.amazon.com/codebuild/home#/builds/#{build[:id]}/view/new"
end

#projectObject



55
56
57
# File 'lib/buildkiq/job.rb', line 55

def project
  @project ||= @client.batch_get_projects(names: [project_name])[0][0]
end

#start(source_version: nil, build_cmd: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/buildkiq/job.rb', line 15

def start(source_version: nil, build_cmd: nil)
  params = {project_name: project_name, environment_variables_override: environments}

  if source_version && source_type == "S3"
    raise ArgumentError.new("source_version parameter can not be used, project source type is S3")
  end

  params[:source_version]     = source_version if source_version
  params[:buildspec_override] = build_spec(build_cmd) if build_cmd
  logger.info("build parameter: #{params}")

  @build = @client.start_build(params).build
end

#statusObject



37
38
39
# File 'lib/buildkiq/job.rb', line 37

def status
  fetch_build.build_status
end

#wait_for_job(timeout_sec: build.timeout_in_minutes * 60, sleep_sec: 5) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/buildkiq/job.rb', line 29

def wait_for_job(timeout_sec: build.timeout_in_minutes * 60, sleep_sec: 5)
  Timeout.timeout(timeout_sec) do
    sleep(sleep_sec) until done?
  end

  self
end

#watch_log_urlObject



45
46
47
# File 'lib/buildkiq/job.rb', line 45

def watch_log_url
  build.logs.deep_link if build
end