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 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
# frozen_string_literal: true
module Satorix
module CI
module Deploy
module Rubygems
require 'fileutils'
include Satorix::Shared::Console
extend self
def go
log_bench('Generating rubygems.org configuration_file...') { generate_rubygems_configuration_file }
log_bench('Preparing gem build directory...') { prepare_gem_build_directory }
log_bench('Building gem...') { build_gem }
built_gems.each { |gem| log_bench("Publishing #{ File.basename gem }...") { publish_gem gem } }
end
private
def build_gem
Dir.chdir(Satorix.app_dir) do
run_command 'bundle exec rake build'
end
end
def built_gems
Dir.glob(File.join(gem_build_directory, '*.gem')).select { |e| File.file? e }
end
def gem_build_directory
File.join Satorix.app_dir, 'pkg'
end
def generate_rubygems_configuration_file
path = File.join(Dir.home, '.gem')
FileUtils.mkdir_p(path) unless File.exist?(path)
file = File.join(path, 'credentials')
File.open(file, 'w') { |f| f.write rubygems_configuration_file_contents }
FileUtils.chmod 0o600, file
end
def prepare_gem_build_directory
run_command "rm -rf #{ gem_build_directory }"
FileUtils.mkdir_p gem_build_directory
end
def publish_gem(gem)
run_command "gem push #{ gem } --config-file #{ File.join(Dir.home, '.gem', 'credentials') }"
rescue RuntimeError
# To prevent the display of an ugly stacktrace.
abort "\nGem was not published!"
end
def rubygems_api_key
ENV['SATORIX_CI_RUBYGEMS_API_KEY']
end
def rubygems_configuration_file_contents
"---\n:rubygems_api_key: #{ rubygems_api_key }"
end
end
end
end
end
|