Class: Jets::CLI::Ci::Start

Inherits:
Base show all
Defined in:
lib/jets/cli/ci/start.rb

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#build_id, #check_build_id!, #find_build, #run_with_exception_handling, #show_console_log_url, #stack_name, #stop_build

Methods inherited from Base

#initialize, #paginate, #paging_params, rescue_api_error

Methods included from Util::Logging

#log

Methods included from AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from AwsServices::StackStatus

#output_value, #stack_exists?

Methods included from AwsServices::GlobalMemoist

included, #reset_cache!

Methods included from Api

#api, #api_key

Constructor Details

This class inherits a constructor from Jets::CLI::Base

Instance Method Details

#are_you_sure?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/jets/cli/ci/start.rb', line 85

def are_you_sure?
  message = "Will start build and deploy #{project_name.color(:green)}\n"
  message << "#{git_dirty_message}\n" if git_dirty?
  sure? message
end

#branch_infoObject



64
65
66
67
68
69
70
71
# File 'lib/jets/cli/ci/start.rb', line 64

def branch_info
  if @options[:branch]
    log.info "Branch: #{@options[:branch]}"
  end
  if @options[:secondary_branches]
    log.info "Branches: #{@options[:secondary_branches]}"
  end
end

#buildspec_overrideObject



38
39
40
41
# File 'lib/jets/cli/ci/start.rb', line 38

def buildspec_override
  return unless @options[:buildspec_override]
  IO.read(@options[:buildspec_override])
end

#environment_variables_overrideObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jets/cli/ci/start.rb', line 47

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

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

#git_changes_pushed?Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
# File 'lib/jets/cli/ci/start.rb', line 105

def git_changes_pushed?
  current_branch = `git branch --show-current`.strip
  local_commit = `git rev-parse #{current_branch}`.strip
  remote_commit = `git rev-parse origin/#{current_branch}`.strip
  local_commit == remote_commit
end

#git_dirty?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/jets/cli/ci/start.rb', line 116

def git_dirty?
  `git status --porcelain`.strip != ""
end

#git_dirty_messageObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jets/cli/ci/start.rb', line 91

def git_dirty_message
  if git_dirty?
    <<~EOL.strip
      Warning: Git is dirty. The CI build will not include the latest changes.
      If you want to include the latest changes, please commit and push them first.
    EOL
  elsif !git_changes_pushed?
    <<~EOL.strip
      Warning: The latest changes have not been pushed to the remote repository.
      If you want to include the latest changes, please push them first.
    EOL
  end
end

#git_dirty_noteObject



112
113
114
# File 'lib/jets/cli/ci/start.rb', line 112

def git_dirty_note
  "(git is dirty)" if git_dirty?
end

#runObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jets/cli/ci/start.rb', line 3

def run
  are_you_sure?

  params = {project_name: project_name}
  source_version = @options[:branch]
  params[:source_version] = source_version if source_version # when nil. uses branch configured on CodeBuild project settings
  params[:secondary_sources_version_override] = secondary_sources_version_override if secondary_sources_version_override # when nil. uses branch configured on CodeBuild project settings
  params[:environment_variables_override] = environment_variables_override if @options[:env_vars]
  params.merge!(@options[:overrides]) if @options[:overrides]
  branch_info
  params[:buildspec_override] = buildspec_override
  log.debug("params: #{params}")
  resp = start_build(params)

  log.info "Build started for #{project_name}"
  show_console_log_url(resp.build.id)
  tail_logs(resp.build.id)

  resp = codebuild.batch_get_builds(ids: [resp.build.id])
  success = resp.builds.first.build_status == "SUCCEEDED"
  exit 1 unless success
  success
end

#secondary_sources_version_overrideObject

docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/CodeBuild/Client.html#start_build-instance_method Map Hash to secondary_sources_version_override Structure. IE: From: “feature1” To: [“Main”, source_version: “feature”]



77
78
79
80
81
82
83
# File 'lib/jets/cli/ci/start.rb', line 77

def secondary_sources_version_override
  secondary_branches = @options[:secondary_branches]
  return unless secondary_branches
  secondary_branches.map do |id, version|
    {source_identifier: id, source_version: version}
  end
end

#start_build(params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/jets/cli/ci/start.rb', line 27

def start_build(params)
  codebuild.start_build(params)
rescue Aws::CodeBuild::Errors::ResourceNotFoundException => e
  log.error "Error: #{e.message}".color(:red)
  log.error <<~EOL
    AWS CodeBuild Project #{project_name} not found. Are you sure it exists?
    Maybe double-check your AWS config settings
  EOL
  exit 1
end

#tail_logs(build_id) ⇒ Object



43
44
45
# File 'lib/jets/cli/ci/start.rb', line 43

def tail_logs(build_id)
  Tailer.new(@options, build_id).run
end