Class: Putpaws::CodeBuild::ProjectCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/putpaws/code_build/project_command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region:, project_name_prefix: nil) ⇒ ProjectCommand

Returns a new instance of ProjectCommand.



13
14
15
16
17
18
# File 'lib/putpaws/code_build/project_command.rb', line 13

def initialize(region:, project_name_prefix: nil)
  @codebuild_client = Aws::CodeBuild::Client.new({region: region})
  @region = region
  @project_name_prefix = project_name_prefix
  @codebuild_project = nil
end

Instance Attribute Details

#codebuild_client ⇒ Object (readonly)

Returns the value of attribute codebuild_client.



10
11
12
# File 'lib/putpaws/code_build/project_command.rb', line 10

def codebuild_client
  @codebuild_client
end

#codebuild_project ⇒ Object

Returns the value of attribute codebuild_project.



12
13
14
# File 'lib/putpaws/code_build/project_command.rb', line 12

def codebuild_project
  @codebuild_project
end

#project_name_prefix ⇒ Object (readonly)

Returns the value of attribute project_name_prefix.



11
12
13
# File 'lib/putpaws/code_build/project_command.rb', line 11

def project_name_prefix
  @project_name_prefix
end

#region ⇒ Object (readonly)

Returns the value of attribute region.



11
12
13
# File 'lib/putpaws/code_build/project_command.rb', line 11

def region
  @region
end

Class Method Details

.config(config) ⇒ Object



6
7
8
# File 'lib/putpaws/code_build/project_command.rb', line 6

def self.config(config)
  new(**config.codebuild_command_params)
end

Instance Method Details

#build_source_version(type:, version:) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/putpaws/code_build/project_command.rb', line 61

def build_source_version(type:, version:)
  type = type.to_sym
  if type == :branch
    "refs/heads/#{version}"
  elsif type == :tag
    "refs/tags/#{version}"
  elsif type == :commit_id
    version
  else
    raise "Please specify valid type: branch or tag or commit_id"
  end
end

#list_codebuild_projects ⇒ Object



20
21
22
23
# File 'lib/putpaws/code_build/project_command.rb', line 20

def list_codebuild_projects
  res = codebuild_client.batch_get_projects(names: [project_name_prefix])
  res.projects.select{|pr| pr.name.start_with?(project_name_prefix)}
end

#start_build_with_specific_version(source_version:, environment_variables: {}, buildspec_override: nil, artifacts_override: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/putpaws/code_build/project_command.rb', line 25

def start_build_with_specific_version(source_version:, environment_variables: {}, buildspec_override: nil, artifacts_override: nil)
  current_variables = codebuild_project.environment.environment_variables.map{|x| [x.name, {type: x.type, value: x.value}]}.to_h
  new_variables = environment_variables.reduce(current_variables) {|acc,(k,v)| acc[k] = {type: 'PLANTTEXT', value: v}}
  environment_variables_override = new_variables.map{|k,v| v.merge(name: k)}
  params = {
    project_name: codebuild_project.name,
    source_version: source_version,
    environment_variables_override: environment_variables_override,
  }
  if buildspec_override
    params = params.merge(buildspec_override: buildspec_override)
  end
  if artifacts_override
    params = params.merge(artifacts_override: artifacts_override)
  end

  codebuild_client.start_build(**params)
end

#wait_until_build_complete(build_id, timeout: 3600, interval: 10) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/putpaws/code_build/project_command.rb', line 44

def wait_until_build_complete(build_id, timeout: 3600, interval: 10)
  deadline = Time.now + timeout
  last_phase = nil
  loop do
    res = codebuild_client.batch_get_builds(ids: [build_id])
    build = res.builds.first
    raise "Build not found: #{build_id}" unless build
    if build.current_phase != last_phase
      last_phase = build.current_phase
      puts "  phase: #{last_phase}"
    end
    return build if build.build_status != 'IN_PROGRESS'
    raise "Timed out waiting for build to complete: #{build_id}" if Time.now > deadline
    sleep interval
  end
end