Class: Fastlane::Actions::GithubActionAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



286
287
288
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 286

def self.authors
  ["josdholtz"]
end

.available_optionsObject



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 294

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :server_url,
                                 env_name: "FL_GITHUB_API_SERVER_URL",
                                 description: "The server url. e.g. 'https://your.internal.github.host/api/v3' (Default: 'https://api.github.com')",
                                 default_value: "https://api.github.com",
                                 optional: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("Please include the protocol in the server url, e.g. https://your.github.server/api/v3") unless value.include?("//")
                                 end),
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: "FL_GITHUB_API_TOKEN",
                                 description: "Personal API Token for GitHub - generate one at https://github.com/settings/tokens",
                                 sensitive: true,
                                 code_gen_sensitive: true,
                                 is_string: true,
                                 default_value: ENV["GITHUB_API_TOKEN"],
                                 default_value_dynamic: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :org,
                                 env_name: "FL_GITHUB_ACTIONS_ORG",
                                 description: "Name of organization of the repository for GitHub Actions"),
    FastlaneCore::ConfigItem.new(key: :repo,
                                 env_name: "FL_GITHUB_ACTIONS_REPO",
                                 description: "Name of repository for GitHub Actions"),
    FastlaneCore::ConfigItem.new(key: :match_org,
                                 env_name: "FL_GITHUB_ACTIONS_MATCH_ORG",
                                 description: "Name of organization of the match repository",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :match_repo,
                                 env_name: "FL_GITHUB_ACTIONS_MATCH_REPO",
                                 description: "Name of match repository",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :dotenv_paths,
                                 env_name: "FL_GITHUB_ACTINOS_DOTENV_PATHS",
                                 description: "Paths of .env files to parse",
                                 optional: true,
                                 type: Array,
                                 verify_block: proc do |values|
                                   values.each do |value|
                                     UI.user_error!("Path #{value} doesn't exist") unless File.exist?(value)
                                   end 
                                 end),
  ]
end

.check_for_setup_ci_in_fastfileObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 25

def self.check_for_setup_ci_in_fastfile
  fastfiles = Dir.glob("./*/Fastfile").map do |path|
    File.absolute_path(path)
  end
 
  fastfiles.each do |path|
    content = File.read(path)

    if !content.include?("setup_ci")
      UI.confirm("`setup_ci` is not detected for '#{path}'. Do you still want to continue on?")
    end
  end

end

.deploy_key_titleObject



21
22
23
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 21

def self.deploy_key_title
  "Match Deploy Key (created by fastalne-plugin-github_actions)"
end

.descriptionObject



282
283
284
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 282

def self.description
  "Helper to setup GitHub actions for fastlane and match"
end

.detailsObject



290
291
292
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 290

def self.details
  "Helper to setup GitHub actions for fastlane and match"
end

.encrypt_secret(key64, secret) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 269

def self.encrypt_secret(key64, secret)
  require "rbnacl"
  require "base64"

  key = Base64.decode64(key64)
  public_key = RbNaCl::PublicKey.new(key)

  box = RbNaCl::Boxes::Sealed.from_public_key(public_key)
  encrypted_secret = box.encrypt(secret)

  return Base64.strict_encode64(encrypted_secret)
end

.generate_deploy_key(params) ⇒ Object



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
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 125

def self.generate_deploy_key(params)
  if params[:match_org].to_s == "" && params[:match_repo].to_s == ""
    UI.message("Skipping Deploy Key generation...")
    return {}
  elsif params[:match_org].to_s == ""
    UI.user_error!("`match_org` also needs to be specified")
  elsif params[:match_repo].to_s == ""
    UI.user_error!("`match_repo` also needs to be specified")
  end

  get_deploy_keys_resp = self.match_repo_get(params, "/keys")

  sleep(1)

  deploy_keys = get_deploy_keys_resp[:json] || []
  deploy_keys.each do |deploy_key|
    if deploy_key["title"] == deploy_key_title
      if UI.confirm("Deploy Key for the match repo already exists... Delete it?")
        self.match_repo_delete(params, "/keys/#{deploy_key["id"]}")
        UI.message("Deleted existing Deploy Key")
        sleep(1)
      else
        return {}
      end
    end
  end

  require 'sshkey'
  k = SSHKey.generate()

  body = {
    title: deploy_key_title,
    key: k.ssh_public_key,
    read_only: true
  }
  post_deploy_key_resp = self.match_repo_post(params, "/keys", body)
  UI.message("Created Deploy Key")
  
  sleep(3)
 
  secrets = {}
  secrets[match_deploy_key] = k.private_key  
  return secrets
end

.generate_workflow_template(params, secret_names) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 40

def self.generate_workflow_template(params, secret_names)
  workflows_dir = File.absolute_path(".github/workflows")
  workflow_path = File.join(workflows_dir, 'fastlane.yml')

  if File.exist?(workflow_path)
    if UI.confirm("File already exists at #{workflow_path}. Do you want to overwrite?")
      UI.message("Overwriting #{workflow_path}")
    else
      UI.message("Cancelled workflow template generation...")
      return
    end
  end

  require 'fastlane/erb_template_helper'
  include ERB::Util
    
  spaces = " " * 10

  use_match = secret_names.include?(match_deploy_key)
    
  #
  # Clone test secrets and commands
  #
  if use_match
    clone_test_secrets = [
      'GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=no"',
      "#{match_deploy_key}: ${{ secrets.#{match_deploy_key} }}"
    ].map do |secret|
      "#{spaces}#{secret}"
    end.join("\n")

    clone_test_commands = [
      'eval "$(ssh-agent -s)"',
      "ssh-add - <<< \"${#{match_deploy_key}}\"",
      "git clone [email protected]:#{params[:match_org]}/#{params[:match_repo]}.git",
      "ls #{params[:match_repo]}"
    ].map do |command|
      "#{spaces}#{command}"
    end.join("\n")
  end

  #
  # Secrets and commands
  #
  secrets = secret_names.map do |secret_name|
    "#{secret_name}: ${{ secrets.#{secret_name}  }}"
  end

  if use_match
    secrets << 'GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=no"'
    secrets << 'MATCH_READONLY: true'
  end

  secrets = secrets.map do |secret|
    "#{spaces}#{secret}"
  end.join("\n")

  commands = []
  if use_match
    commands = [
      'eval "$(ssh-agent -s)"',
      "ssh-add - <<< \"${#{match_deploy_key}}\"",
    ]
  end

  commands << 'bundle exec fastlane test'
  commands = commands.map do |command|
    "#{spaces}#{command}"
  end.join("\n")

  puts "secret size: #{secrets.size}"

  workflow_template = Helper::GithubActionHelper.load("workflow_template")
  workflow_render = Helper::GithubActionHelper.render(workflow_template, {
    use_match: use_match,
    clone_test_secrets: clone_test_secrets,
    clone_test_commands: clone_test_commands,
    secrets: secrets,
    commands: commands
  })

  FileUtils.mkdir_p(workflows_dir)
  File.write(workflow_path, workflow_render)
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


340
341
342
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 340

def self.is_supported?(platform)
  true
end

.match_deploy_keyObject



17
18
19
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 17

def self.match_deploy_key
  "MATCH_DEPLOY_KEY"  
end

.match_repo_delete(params, path) ⇒ Object



259
260
261
262
263
264
265
266
267
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 259

def self.match_repo_delete(params, path)
  return other_action.github_api(
    server_url: params[:server_url],
    api_token: params[:api_token],
    http_method: "DELETE",
    path: "/repos/#{params[:match_org]}/#{params[:match_repo]}#{path}",
    body: {},
  )
end

.match_repo_get(params, path) ⇒ Object



239
240
241
242
243
244
245
246
247
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 239

def self.match_repo_get(params, path)
  return other_action.github_api(
    server_url: params[:server_url],
    api_token: params[:api_token],
    http_method: "GET",
    path: "/repos/#{params[:match_org]}/#{params[:match_repo]}#{path}",
    body: {},
  )
end

.match_repo_post(params, path, body) ⇒ Object



249
250
251
252
253
254
255
256
257
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 249

def self.match_repo_post(params, path, body)
  return other_action.github_api(
    server_url: params[:server_url],
    api_token: params[:api_token],
    http_method: "POST",
    path: "/repos/#{params[:match_org]}/#{params[:match_repo]}#{path}",
    body: body,
  )
end

.parse_dotenvs(params) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 207

def self.parse_dotenvs(params)
  dotenv_paths = (params[:dotenv_paths] || [])

  if dotenv_paths.empty?
    UI.message "No dotenv paths to parse..."
    return {}
  end

  require "dotenv"
  return Dotenv.parse(*dotenv_paths)
end

.post_secrets(params, additional_secrets) ⇒ Object



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
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 170

def self.post_secrets(params, additional_secrets)
  public_key_resp = self.repo_get(params, "/actions/secrets/public-key")
  key_id = public_key_resp[:json]["key_id"]
  key64 = public_key_resp[:json]["key"]

  secrets = self.parse_dotenvs(params)
  secrets = secrets.merge(additional_secrets || {})

  encrypted_secrets = {}
  secrets.each do |k,v|
    encrypted_value = self.encrypt_secret(key64, v)
    encrypted_secrets[k] = encrypted_value
  end

  existing_secrets_resp = self.repo_get(params, "/actions/secrets")
  existing_secret_names = existing_secrets_resp[:json]["secrets"].map do |secret|
    secret["name"].to_s
  end

  encrypted_secrets.reject! do |k,v|
    if existing_secret_names.include?(k.to_s)
      !UI.confirm("Overwrite #{k}?")
    end
  end

  encrypted_secrets.each do |k,v|
    body = {
      key_id: key_id,
      encrypted_value: v
    }
    self.repo_put(params, "/actions/secrets/#{k}", body)
    UI.message("Saving secret #{k}")
  end

  return secrets.keys
end

.repo_get(params, path) ⇒ Object



219
220
221
222
223
224
225
226
227
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 219

def self.repo_get(params, path)
  return other_action.github_api(
    server_url: params[:server_url],
    api_token: params[:api_token],
    http_method: "GET",
    path: "/repos/#{params[:org]}/#{params[:repo]}#{path}",
    body: {},
  )
end

.repo_put(params, path, body) ⇒ Object



229
230
231
232
233
234
235
236
237
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 229

def self.repo_put(params, path, body)
  return other_action.github_api(
    server_url: params[:server_url],
    api_token: params[:api_token],
    http_method: "PUT",
    path: "/repos/#{params[:org]}/#{params[:repo]}#{path}",
    body: body,
  )
end

.run(params) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/fastlane/plugin/github_action/actions/github_action_action.rb', line 7

def self.run(params)
  UI.message("The github_actions plugin is working!")

  self.check_for_setup_ci_in_fastfile

  additional_secrets = self.generate_deploy_key(params)
  secret_names = self.post_secrets(params, additional_secrets)
  self.generate_workflow_template(params, secret_names)
end