class ContainerRunner

Run a client in Docker container

Public Instance Methods

run(client) click to toggle source

start a container, copy the sources there and run “rake run” @param client [String,nil] the client name, nil or empty string = find

the client automatically
# File lib/tasks/container_runner.rb, line 30
def run(client)
  container = find_container
  container.pull
  container.start
  container.copy_current_dir

  cmd = client ? "rake run[#{client}]" : "rake run"
  container.run(cmd)

  container.stop
end

Private Instance Methods

find_container() click to toggle source

find the Docker image to use in the container @return [String] the image name

# File lib/tasks/container_runner.rb, line 46
def find_container
  # explicitly requested image
  image = ENV["DOCKER_IMAGE"]
  return GithubActions::Container.new(image) if image && !image.empty?

  # scan the Docker images used in the GitHub Actions
  containers = workflow_containers
  return containers.first if containers.size == 1

  if containers.empty?
    error("No Docker image was found in the GitHub Actions")
    puts "Use DOCKER_IMAGE=<name> option for specifying the image name"
    abort
  end

  # multiple images found
  error("Found multiple Docker images in the GitHub Actions:")
  error(containers.map { |c| [c.image, c.options] })
  puts "Use DOCKER_IMAGE=<name> option for specifying the image name"
  puts "and DOCKER_OPTIONS=<options> option for specifying the extra Docker"
  puts "command line parameters."
  abort
end
workflow_containers() click to toggle source

extract the Docker images from the GitHub Actions, the duplicates are removed @return [Array<GithubActions::Container>] image names

# File lib/tasks/container_runner.rb, line 73
def workflow_containers
  ret = GithubActions::Workflow.read.each_with_object([]) do |workflow, containers|
    workflow.jobs.each do |job|
      container_data = job.container

      case container_data
      when String
        containers << GithubActions::Container.new(container_data)
      when Hash
        # to_s converts missing options (nil) to empty options ("")
        # to treat these as equal in comparison
        containers << GithubActions::Container.new(
          container_data["image"],
          container_data["options"].to_s
        )
      else
        abort "Unsupported container definition: #{container_data.inspect}"
      end
    end
  end

  ret.uniq { |c| [c.image, c.options] }
end