Class: Fastlane::Actions::RunDockerizedTaskAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/mango/actions/run_dockerized_task_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



54
55
56
# File 'lib/fastlane/plugin/mango/actions/run_dockerized_task_action.rb', line 54

def self.authors
  ['Serghei Moret', 'Daniel Hartwich']
end

.available_optionsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/fastlane/plugin/mango/actions/run_dockerized_task_action.rb', line 71

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :container_name,
                                 env_name: 'CONTAINER_NAME',
                                 description: 'Name of the docker container. Will be generated randomly if not defined',
                                 optional: true,
                                 type: String),

    FastlaneCore::ConfigItem.new(key: :no_vnc_port,
                                 env_name: 'NO_VNC_PORT',
                                 description: 'Port to redirect noVNC. 6080 by default',
                                 default_value: 6080,
                                 optional: false,
                                 type: Integer),

    FastlaneCore::ConfigItem.new(key: :device_name,
                                 env_name: 'DEVICE_NAME',
                                 description: 'Name of the Android device. Nexus 5X by default',
                                 default_value: 'Nexus 5X',
                                 optional: false,
                                 type: String),

    FastlaneCore::ConfigItem.new(key: :emulator_name,
                                 env_name: 'EMULATOR_NAME',
                                 description: 'Name of the Android emulator. emulator-5554 by default',
                                 default_value: 'emulator-5554',
                                 optional: false,
                                 type: String),

    FastlaneCore::ConfigItem.new(key: :docker_image,
                                 env_name: 'DOCKER_IMAGE',
                                 description: 'Name of the Docker image. butomo1989/docker-android-x86-5.1.1 by default',
                                 default_value: 'butomo1989/docker-android-x86-5.1.1',
                                 optional: false,
                                 type: String),

    FastlaneCore::ConfigItem.new(key: :container_timeout,
                                 env_name: 'CONTAINER_TIMEOUT',
                                 description: 'Timeout (in seconds) to get the healthy docker container. 450 (7.5 minutes) by default',
                                 default_value: 450,
                                 optional: false,
                                 type: Integer),

    FastlaneCore::ConfigItem.new(key: :android_task,
                                 env_name: 'ANDROID TASK',
                                 description: 'A generic Android task you want to execute',
                                 is_string: true,
                                 optional: false),

    FastlaneCore::ConfigItem.new(key: :sdk_path,
                                 env_name: 'SDK_PATH',
                                 description: 'The path to your Android sdk directory (root). ANDROID_HOME by default',
                                 default_value: ENV['ANDROID_HOME'],
                                 is_string: true,
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :port_factor,
                                 env_name: 'PORT_FACTOR',
                                 description: 'Base for calculating a unique port for noVNC. You can pass EXECUTOR_NUMBER from Jenkins for example, this will be unique and not clash in case of several instances running on the same machine',
                                 optional: true,
                                 type: String),

    FastlaneCore::ConfigItem.new(key: :core_amount,
                                 env_name: 'CORE_AMOUNT',
                                 default_value: 0,
                                 description: 'Define if we want to start docker container with the limitation',
                                 optional: true,
                                 type: Integer),

    FastlaneCore::ConfigItem.new(key: :workspace_dir,
                                 env_name: 'WORKSPACE_DIR',
                                 default_value: '/root/tests/',
                                 description: 'Path to the workspace to execute commands. If you want to execute your `android_task` from a different directory you have to specify `workspace_dir`',
                                 optional: true,
                                 type: String),

    FastlaneCore::ConfigItem.new(key: :maximal_run_time,
                                 env_name: 'MAXIMAL_RUN_TIME',
                                 default_value: 60,
                                 description: 'Defines the maximal time of your test run. Defaults to 60 minutes',
                                 optional: true,
                                 type: Integer),

    FastlaneCore::ConfigItem.new(key: :bundle_install,
                                 env_name: 'BUNDLE_INSTALL',
                                 default_value: false,
                                 description: 'Defines if the Android task must execute bundle install before running a build',
                                 optional: true,
                                 type: Boolean),

    FastlaneCore::ConfigItem.new(key: :is_running_on_emulator,
                                 env_name: 'IS_RUNNING_ON_EMULATOR',
                                 default_value: true,
                                 description: 'Define if we want to run the emulator in the container',
                                 optional: true,
                                 type: Boolean),

    FastlaneCore::ConfigItem.new(key: :post_actions,
                                 env_name: 'POST_ACTIONS',
                                 description: 'Actions that will be executed after the main command has been executed',
                                 is_string: true,
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :pre_action,
                                 env_name: 'PRE_ACTION',
                                 description: 'Actions that will be executed before the docker image gets pulled',
                                 is_string: true,
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :docker_registry_login,
                                 env_name: 'DOCKER_REGISTRY_LOGIN',
                                 description: 'Authenticating yourself to a custom Docker registry',
                                 type: String,
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :pull_latest_image,
                                 env_name: 'PULL_LATEST_IMAGE',
                                 description: 'Define if you want to pull the latest image',
                                 type: Boolean,
                                 default_value: false,
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :environment_variables,
                                 env_name: 'ENVIRONMENT_VARIABLES',
                                 description: 'Comma seperated list of environment variables which are passed into the docker container',
                                 type: Array,
                                 default_value: [],
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :vnc_enabled,
                                 env_name: 'VNC_ENABLED',
                                 description: 'A bool. True for vnc_enabled False for vnc_disabled',
                                 type: Boolean,
                                 default_value: true,
                                 optional: true)
  ]
end

.descriptionObject



50
51
52
# File 'lib/fastlane/plugin/mango/actions/run_dockerized_task_action.rb', line 50

def self.description
  'Action that runs Android tasks on a specified Docker image'
end

.detailsObject



66
67
68
69
# File 'lib/fastlane/plugin/mango/actions/run_dockerized_task_action.rb', line 66

def self.details
  # Optional:
  ''
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/fastlane/plugin/mango/actions/run_dockerized_task_action.rb', line 62

def self.is_supported?(platform)
  platform == :android
end

.return_valueObject



58
59
60
# File 'lib/fastlane/plugin/mango/actions/run_dockerized_task_action.rb', line 58

def self.return_value
  # If your method provides a return value, you can describe here what it does
end

.run(params) ⇒ Object



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
39
40
41
42
43
44
45
46
47
48
# File 'lib/fastlane/plugin/mango/actions/run_dockerized_task_action.rb', line 6

def self.run(params)
  UI.important('The mango plugin is working!')
  workspace_dir = params[:workspace_dir]
  ENV['DOCKER_CONFIG'] = "#{ENV['WORKSPACE']}/.docker"
  mango_helper = Fastlane::Helper::MangoHelper.new(params)
  mango_helper.setup_container

  docker_commander = Helper::DockerCommander.new(mango_helper.container_name)

  failure_buffer_timeout = 5
  timeout_command = "timeout #{params[:maximal_run_time] - failure_buffer_timeout}m"

  android_task = params[:android_task]
  if android_task
    UI.success('Starting Android Task.')
    bundle_install = params[:bundle_install] ? '&& bundle install ' : ''

    docker_commander.exec(command: "cd #{workspace_dir} #{bundle_install}&& #{timeout_command} #{android_task} || exit 1")
  end
rescue StandardError => e
  begin
    Actions.sh("docker logs #{mango_helper.container_name} --tail 200")
  rescue StandardError
    # do nothing
  end
  docker_commander.exec(command:
                            'cat /var/log/supervisor/docker-android.stderr.log', raise_when_fail: false)
  docker_commander.exec(command: 'cat /var/log/supervisor/supervisord.log',
                        raise_when_fail: false)
  raise e
ensure
  begin
    post_actions = params[:post_actions]
    if post_actions && !mango_helper.kvm_disabled?
      docker_commander&.exec(command: "cd #{workspace_dir} && #{post_actions}")
    end

    UI.important("Cleaning up #{params[:emulator_name]} container")
    docker_commander.delete_container if mango_helper&.instance_variable_get('@container')
  rescue StandardError => e
    puts e
  end
end