Class: Fastlane::Actions::BuildNumberSyncAction

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

Documentation collapse

Class Method Summary collapse

Class Method Details

.add_user_config_if_needed(pwd_git_username:, pwd_git_email:) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/fastlane/plugin/yalantis_ci/actions/build_number_sync.rb', line 66

def self.add_user_config_if_needed(pwd_git_username:, pwd_git_email:)
  variables = {'email' => pwd_git_email, 'name' => pwd_git_username }
  variables.each do |key, value|
    if `git config --local --get user.#{key}`.strip.empty?
      `git config --local user.#{key} #{value}` unless value.empty?
    end
  end
end

.authorsObject



127
128
129
# File 'lib/fastlane/plugin/yalantis_ci/actions/build_number_sync.rb', line 127

def self.authors
  ["Dima Vorona", "Yalantis"]
end

.available_optionsObject



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
# File 'lib/fastlane/plugin/yalantis_ci/actions/build_number_sync.rb', line 83

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :git_repo_url,
      env_name: "BUILD_NUMBER_GIT_REPO_URL",
      description: "Git repo URL where Build Number is being stored",
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :git_repo_branch,
      env_name: "BUILD_NUMBER_GIT_REPO_BRANCH",
      description: "Git repo branch where Build Number is being stored",
      type: String,
      default_value: 'main'
    ),
    FastlaneCore::ConfigItem.new(
      key: :increment,
      description: 'Whether value from remote should be incremented or not',
      default_value: true,
      verify_block: proc do |value|
        UI.user_error!("Only `true` and `false` are allowed") unless value == true || value == false
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :git_filepath,
      env_name: "BUILD_NUMBER_GIT_FILEPATH",
      description: "Relative filepath to the file that contains build number",
      type: String,
      default_value: 'payload.json'
    ),
    FastlaneCore::ConfigItem.new(
      key: :mutation_callback,
      description: 'A callback invoked with the build number from remote and expected build number to be returned',
      optional: true,
      type: Proc,
      default_value: nil
    )
  ]
end

.descriptionObject



79
80
81
# File 'lib/fastlane/plugin/yalantis_ci/actions/build_number_sync.rb', line 79

def self.description
  "Read and increment version stored in a specified repo"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/fastlane/plugin/yalantis_ci/actions/build_number_sync.rb', line 131

def self.is_supported?(platform)
  [:ios, :mac].include?(platform)
end

.outputObject



123
124
125
# File 'lib/fastlane/plugin/yalantis_ci/actions/build_number_sync.rb', line 123

def self.output
  ['BUILD_NUMBER', 'The new build number']
end

.read_from_remote(git_repo_url:, git_repo_branch:, payload_filepath:, mutation_callback: nil) ⇒ Object



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
62
63
64
# File 'lib/fastlane/plugin/yalantis_ci/actions/build_number_sync.rb', line 20

def self.read_from_remote(git_repo_url:, git_repo_branch:, payload_filepath:, mutation_callback: nil)
  build_number = nil
  attempts = 0
  total_attempts = 5

  pwd = ENV['PWD']
  pwd_managed_by_git = system("git -C \"#{pwd}\" rev-parse") == true
  pwd_git_username = pwd_managed_by_git ? `git config --local --get user.name`.strip : nil
  pwd_git_email = pwd_managed_by_git ? `git config --local --get user.email`.strip : nil

  while build_number.nil? && attempts < total_attempts
    Helper::GitHelper.clone_repo_in_tmp(repo_url: git_repo_url, branch: git_repo_branch, create_branch: true) do |dir|
      attempts += 1

      UI.message("Attempt #{attempts} out of #{total_attempts} to get build number")

      filepath = File.join(dir, payload_filepath)
      if File.exist?(filepath)
        begin
          remote_build_number = JSON.parse(File.read(filepath))['build_number']
        rescue
          remote_build_number = 1
        end
      else
        remote_build_number = 1
      end

      build_number = mutation_callback != nil ? mutation_callback.call(remote_build_number) : remote_build_number

      if build_number.kind_of?(Integer) && build_number > remote_build_number
        payload_contents = JSON.pretty_generate({ 'build_number' => build_number, 'generation' => SecureRandom.uuid })
        File.open(filepath, 'w+') { |file| file.write(payload_contents) }

        add_user_config_if_needed(pwd_git_username: pwd_git_username, pwd_git_email: pwd_git_email)

        Actions.sh("git add #{filepath} && git commit -m '[Build Number] Update Build Number to: #{build_number}'")
        Actions.sh("git push #{git_repo_url} #{git_repo_branch}", error_callback: ->(result) { build_number = nil })
      elsif build_number.kind_of?(Integer) && build_number < remote_build_number
        UI.user_error!("It is unexpected to return build number less than a proposed one: #{build_number} vs #{remote_build_number}")
      end
    end
  end

  build_number
end

.run(options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/fastlane/plugin/yalantis_ci/actions/build_number_sync.rb', line 9

def self.run(options)
  build_number = read_from_remote(
    git_repo_url: options[:git_repo_url],
    git_repo_branch: options[:git_repo_branch],
    payload_filepath: options[:git_filepath],
    mutation_callback: options[:mutation_callback]
  )
  Actions.lane_context[SharedValues::BUILD_NUMBER] = build_number unless build_number.nil?
  return build_number
end