Skip to content

Commit

Permalink
Update with latest_major condition and ensure gem presence
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed May 14, 2024
1 parent b7955f3 commit be92864
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ prepare_image_destinations:
- when: manual
allow_failure: true
script:
- ./.gitlab/check_gem_presence.rb $CI_COMMIT_TAG
- echo "IMG_DESTINATIONS=$(./.gitlab/prepare_image_destinations.rb dd-lib-ruby-init $CI_COMMIT_TAG)" >> build.env
artifacts:
reports:
Expand Down
37 changes: 37 additions & 0 deletions .gitlab/check_gem_presence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env ruby

require 'bundler/inline'

gemfile { gem 'gems', source: 'https://rubygems.org' }

require 'rubygems'
require 'gems'

version = ARGV[0].chomp
version = version.delete_prefix('v') if version.start_with?('v')

candidate = Gem::Version.new(version)

retry_count = 0
max_retries = 60
interval = 60

loop do
versions = Gems.versions('datadog').map { |h| Gem::Version.new(h['number']) }

if versions.include?(candidate)
puts "Gem version #{candidate} found!"
exit 0
else
retry_count += 1
puts "Attempt(#{retry_count}): Gem 'ddtrace' version '#{candidate}' not found."

if retry_count >= max_retries
puts "Max retries(#{max_retries}) reached, stopping..."
exit 1
else
puts "Retrying in #{interval} seconds..."
sleep interval
end
end
end
24 changes: 14 additions & 10 deletions .gitlab/prepare_image_destinations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,31 @@
version = version.delete_prefix('v') if version.start_with?('v')

candidate = Gem::Version.new(version)
versions = Gems.versions('datadog').map { |h| Gem::Version.new(h['number']) }

# Make sure candidate has already been published to 'https://rubygems.org'
unless versions.include?(candidate)
warn "Version #{candidate} not found in RubyGems"
exit 1
end

# Skip pre-releases
if candidate.prerelease?
warn 'No tags for pre-releases'
exit 1
end

major, minor, = candidate.to_s.split('.')

latest_major_tag = "v#{major}" # contains major
latest_minor_tag = "v#{major}.#{minor}" # contains major, minor
current_major_versions = versions.select { |v| v.to_s.start_with?("#{major}.") }

tags = []

gem_name = 'datadog'

# Check if the candidate is larger than public latest version
tags << 'latest' if candidate > Gem::Version.new(Gems.latest_version(gem_name).fetch('version'))
tags << latest_major_tag
tags << latest_minor_tag
tags << 'latest' if versions.all? { |v| candidate >= v }
tags << "v#{major}" if current_major_versions.all? { |v| candidate >= v }
tags << "v#{major}.#{minor}"
tags << "v#{candidate}"

destinations = tags.map { |tag| "#{image_name}:#{tag}" }
# $stdout.puts "tags: #{tags}" # Uncomment for debugging

destinations = tags.map { |tag| "#{image_name}:#{tag}" }
$stdout.puts destinations.join(',')

0 comments on commit be92864

Please sign in to comment.