Class: Command::BuildImage

Inherits:
Base
  • Object
show all
Defined in:
lib/command/build_image.rb

Constant Summary collapse

NAME =
"build-image"
OPTIONS =
[
  app_option(required: true),
  commit_option,
  docker_context_option
].freeze
ACCEPTS_EXTRA_OPTIONS =
true
DESCRIPTION =
"Builds and pushes the image to Control Plane"
LONG_DESCRIPTION =
<<~DESC
  - Builds and pushes the image to Control Plane
  - Automatically assigns image numbers, e.g., `app:1`, `app:2`, etc.
  - Uses `.controlplane/Dockerfile` or a different Dockerfile specified through `dockerfile` in the `.controlplane/controlplane.yml` file
  - If a commit is provided through `--commit` or `-c`, it will be set as the runtime env var `GIT_COMMIT`
  - Accepts extra options that are passed to `docker build`
DESC

Constants inherited from Base

Command::Base::ALL_VALIDATIONS, Command::Base::DEFAULT_ARGS, Command::Base::EXAMPLES, Command::Base::HIDE, Command::Base::REQUIRES_ARGS, Command::Base::USAGE, Command::Base::VALIDATIONS, Command::Base::VALIDATIONS_WITHOUT_ADDITIONAL_OPTIONS, Command::Base::VALIDATIONS_WITH_ADDITIONAL_OPTIONS, Command::Base::WITH_INFO_HEADER

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

add_app_identity_option, all_commands, all_options, all_options_by_key_name, app_option, #args_join, commit_option, common_options, #cp, cpu_option, detached_option, docker_context_option, domain_option, #ensure_docker_running!, entrypoint_option, image_option, #initialize, interactive_option, location_option, log_method_option, logs_limit_option, logs_since_option, memory_option, org_option, #progress, replica_option, #run_command_in_latest_image, #run_cpflow_command, run_release_phase_option, skip_confirm_option, skip_post_creation_hook_option, skip_pre_deletion_hook_option, skip_secret_access_binding_option, skip_secrets_setup_option, #step, #step_finish, terminal_size_option, trace_option, upstream_token_option, use_local_token_option, validations_option, verbose_option, version_option, wait_option, #with_retry, workload_option

Methods included from Helpers

normalize_command_name, normalize_option_name, random_four_digits, strip_str_and_validate

Constructor Details

This class inherits a constructor from Command::Base

Instance Method Details

#callObject

rubocop:disable Metrics/MethodLength



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/command/build_image.rb', line 21

def call # rubocop:disable Metrics/MethodLength
  ensure_docker_running!

  dockerfile = config.current[:dockerfile] || "Dockerfile"
  dockerfile = "#{config.app_cpln_dir}/#{dockerfile}"

  raise "Can't find Dockerfile at '#{dockerfile}'." unless File.exist?(dockerfile)

  progress.puts("Building image from Dockerfile '#{dockerfile}'...\n\n")

  image_name = cp.latest_image_next
  image_url = "#{config.org}.registry.cpln.io/#{image_name}"

  commit = config.options[:commit]
  build_args = []
  build_args.push("GIT_COMMIT=#{commit}") if commit

  docker_context = config.options[:docker_context] || config.app_dir

  cp.image_build(image_url, dockerfile: dockerfile,
                            docker_args: config.args,
                            build_args: build_args,
                            docker_context: docker_context)

  push_path = "/org/#{config.org}/image/#{image_name}"

  progress.puts("\nPushing image to '#{push_path}'...\n\n")
  cp.image_push(image_url)
  progress.puts("\nPushed image to '#{push_path}'.\n\n")

  step("Waiting for image to be available", retry_on_failure: true) do
    images = cp.query_images["items"]
    images.find { |image| image["name"] == image_name }
  end
end