Class: Fastlane::Actions::UploadToS3Action

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane-ext/upload_to_s3.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



133
134
135
# File 'lib/fastlane-ext/upload_to_s3.rb', line 133

def self.authors
  ['https://github.com/sroik', 'https://github.com/elfenlaid']
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
# File 'lib/fastlane-ext/upload_to_s3.rb', line 71

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :path,
                                 env_name: 'FL_UPLOAD_S3_PATH',
                                 description: 'Upload local path',
                                 is_string: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("Couldn't find file at path '#{value}'") unless File.exist?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :region,
                                 env_name: 'FL_UPLOAD_S3_REGION',
                                 description: 'Region for S3 or Spaces',
                                 is_string: true,
                                 default_value: 'ams3',
                                 verify_block: proc do |value|
                                   UI.user_error!("No region for UploadToS3Action given, pass using `region: 'region'`") unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :endpoint,
                                 env_name: 'FL_UPLOAD_S3_ENDPOINT',
                                 description: 'Endpoint for S3 or Spaces',
                                 is_string: true,
                                 default_value: 'https://ams3.digitaloceanspaces.com',
                                 verify_block: proc do |value|
                                   UI.user_error!("No Endpoint for UploadToS3Action given, pass using `endpoint: 'endpoint'`") unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :access_key,
                                 env_name: 'FL_UPLOAD_S3_ACCESS_KEY',
                                 description: 'Access Key for S3 or Spaces',
                                 is_string: true,
                                 verify_block: proc do |value|
                                   raise "No Access Key for UploadToS3Action given, pass using `access_key: 'access_key'`".red unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :secret_access_key,
                                 env_name: 'FL_UPLOAD_S3_SECRET_ACCESS_KEY',
                                 description: 'Secret Access Key for S3 or Spaces',
                                 is_string: true,
                                 verify_block: proc do |value|
                                   raise "No Secret Access Key for UploadToS3Action given, pass using `secret_access_key: 'secret_access_key'`".red unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :bucket,
                                 env_name: 'FL_UPLOAD_S3_BUCKET',
                                 description: 'Bucket for S3 or Spaces',
                                 is_string: true,
                                 verify_block: proc do |value|
                                   raise "No Bucket for UploadToS3Action given, pass using `bucket: 'bucket'`".red unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :space,
                                 env_name: 'FL_UPLOAD_S3_SPACE',
                                 description: 'Digital Ocean Space',
                                 is_string: true,
                                 default_value: 'company-files'),
    FastlaneCore::ConfigItem.new(key: :acl,
                                 env_name: 'FL_UPLOAD_S3_ACL',
                                 description: 'Access level for the file',
                                 is_string: true,
                                 default_value: 'public-read',
                                 verify_block: proc do |value|
                                   raise "No Bucket for UploadToS3Action given, pass using `bucket: 'bucket'`".red unless value && !value.empty?
                                 end)
  ]
end

.descriptionObject



67
68
69
# File 'lib/fastlane-ext/upload_to_s3.rb', line 67

def self.description
  'Upload file to S3 or Spaces'
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/fastlane-ext/upload_to_s3.rb', line 141

def self.is_supported?(platform)
  %i[ios android].include?(platform)
end

.outputObject



137
138
139
# File 'lib/fastlane-ext/upload_to_s3.rb', line 137

def self.output
  ['UPLOADED_S3_URL': 'Uploaded file s3 path']
end

.run(params) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fastlane-ext/upload_to_s3.rb', line 10

def self.run(params)
  require 'aws-sdk-s3'

  local_path = params[:path]
  local_name = File.basename(local_path)

  bucket = params[:space] || params[:bucket]
  acl = params[:acl]
  file_key = local_name
  file_path = params[:space] ? params[:bucket] + '/' + file_key : file_key

  client = Aws::S3::Client.new(
    access_key_id: params[:access_key],
    secret_access_key: params[:secret_access_key],
    endpoint: params[:endpoint],
    region: params[:region]
  )

  UI.message "Check whether destination bucket #{bucket} exists..💤"

  begin
    response = client.create_bucket(
      bucket: bucket,
      acl: 'private'
    )
    UI.message "✨ Bucket #{bucket} created! ✨"
  rescue Aws::S3::Errors::BucketAlreadyExists
    UI.message "Bucket #{bucket} alredy exists 👌"
  end

  UI.message 'Going to upload file to s3..💤'

  File.open(local_path, 'r') do |body|
    response = client.put_object(
      acl: acl,
      bucket: bucket,
      key: file_path,
      body: body
    )

    object = Aws::S3::Object.new(
      key: file_path,
      bucket_name: bucket,
      client: client
    )

    url = object.public_url

    UI.message "✨ file uploaded to #{url}"
    ENV[SharedValues::UPLOADED_S3_URL] = url
  end
end