1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# frozen_string_literal: true
class Docker
DEFAULT_ENV = {
'CI_DEBUG_TRACE' => 'true',
'CI_PROJECT_DIR' => '/tmp/app',
'SECURE_LOG_LEVEL' => 'debug'
}.freeze
attr_reader :pwd
def initialize(pwd: Pathname.pwd)
@pwd = pwd
end
def build(tag:)
Dir.chdir pwd do
system("docker build --network=host -t #{tag} .")
end
end
def run(image:, project_path: Pathname.pwd, env: {}, debug: ENV.fetch('DEBUG', 'false') == 'true')
env_options = DEFAULT_ENV.merge(env).map { |(key, value)| "--env #{key}='#{value}'" }
Dir.chdir pwd do
command = if debug
expand([:docker, :run, '-it', "--entrypoint=''", '--rm', "--volume=#{project_path}:/tmp/app", '--add-host=maven.test:127.0.0.1', '--network=host', env_options, image, '/bin/bash -l'])
else
expand([:docker, :run, '--rm', "--volume=#{project_path}:/tmp/app", '--network=host', '--add-host=maven.test:127.0.0.1', env_options, image, '/analyzer run'])
end
system(command, exception: true)
end
end
private
def expand(command)
command.flatten.map(&:to_s).join(' ')
end
end
|