Class: Codebuild::Start

Inherits:
Object
  • Object
show all
Includes:
AwsServices
Defined in:
lib/codebuild/start.rb

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #codebuild

Methods included from AwsServices::Helpers

#are_you_sure?, #inferred_project_name, #inferred_stack_name, #project_name_convention, #stack_exists?

Constructor Details

#initialize(options) ⇒ Start

Returns a new instance of Start.



5
6
7
8
9
# File 'lib/codebuild/start.rb', line 5

def initialize(options)
  @options = options
  @project_name = options[:project_name] || inferred_project_name
  @full_project_name = project_name_convention(@project_name)
end

Instance Method Details

#environment_variables_overrideObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/codebuild/start.rb', line 25

def environment_variables_override
  @options[:env_vars].map do |s|
    k, v = s.split('=')
    ssm = false
    if /^ssm:(.*)/.match(v)
      v = $1
      ssm = true
    end

    {
      name: k,
      value: v,
      type: ssm ? "PARAMETER_STORE" : "PLAINTEXT"
    }
  end
end

#project_nameObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/codebuild/start.rb', line 42

def project_name
  if project_exists?(@full_project_name)
    @full_project_name
  elsif stack_exists?(@project_name) # allow `cb start STACK_NAME` to work too
    resp = cfn.describe_stack_resources(stack_name: @project_name)
    resource = resp.stack_resources.find do |r|
      r.logical_resource_id == "CodeBuild"
    end
    resource.physical_resource_id # codebuild project name
  else
    puts "ERROR: Unable to find the codebuild project with either full_project_name: #{@full_project_name} or project_name: #{@project_name}".color(:red)
    exit 1
  end
end

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/codebuild/start.rb', line 11

def run
  source_version = @options[:branch] || @options[:source_version] || 'master'
  params = {
    project_name: project_name,
    source_version: source_version
  }
  params[:environment_variables_override] = environment_variables_override if @options[:env_vars]
  resp = codebuild.start_build(params)
  puts "Build started for project: #{project_name}"
  puts "Please check the CodeBuild console for the status."
  puts "Codebuild Log Url:"
  puts codebuild_log_url(resp.build.id)
end