Skip to content

Commit

Permalink
Improve update gemfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Sep 18, 2024
1 parent f6ec8b7 commit 4dd912a
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 137 deletions.
93 changes: 93 additions & 0 deletions .github/workflows/lock-dependency.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: Lock Dependency

on:
workflow_dispatch:
inputs:
branch:
description: 'Branch to be lock dependency'
required: true
# Testing purpose, to be removed before merge.
push:
branches:
- tonycthsu/automate-update-gemfiles

# Ensure obsolete job is cancelled if another commit is pushed to the same branch.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
# TODO: In order to fully automate this workflow for each PR,
# have a reliable way to precheck job to understand whether it need to be updated
lock:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
engine:
# ADD NEW RUBIES HERE
- name: ruby
version: '3.4'
- name: ruby
version: '3.3'
- name: ruby
version: '3.2'
- name: ruby
version: '3.1'
- name: ruby
version: '3.0'
- name: ruby
version: '2.7'
- name: ruby
version: '2.6'
- name: ruby
version: '2.5'
- name: jruby
version: '9.4'
- name: jruby
version: '9.3'
- name: jruby
version: '9.2'
container:
image: ghcr.io/datadog/images-rb/engines/${{ matrix.engine.name }}:${{ matrix.engine.version }}
env:
BUNDLE_WITHOUT: check
steps:
- uses: actions/checkout@v4
- run: ls -al
- run: |
ruby -v
gem -v
bundler -v
- run: bundle install

# TODO: Migrate away from `appraisal`
- run: bundle exec appraisal generate
- run: bundle exec rake dependency:lock

- uses: actions/upload-artifact@v4
with:
name: lock-dependency-${{ github.run_id }}-${{ matrix.engine.name }}-${{ matrix.engine.version }}
path: gemfiles/${{ matrix.engine.name }}_${{ matrix.engine.version }}*

commit:
needs: lock
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: actions/download-artifact@v4
with:
path: gemfiles
pattern: lock-dependency-${{ github.run_id }}-*
merge-multiple: true

- run: git diff --color

- uses: stefanzweifel/git-auto-commit-action@v5
with:
file_pattern: 'gemfiles/*'
commit_message: "[🤖] Lock Dependency: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
83 changes: 0 additions & 83 deletions .github/workflows/update-gemfiles.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def build_coverage_matrix(integration, range, gem: nil, min: nil, meta: {})

if min
appraise "#{integration}-min" do
gem gem, "= #{n}"
gem gem, "= #{min}"
meta.each { |k, v| gem k, v }
end
end
Expand Down
2 changes: 1 addition & 1 deletion appraisal/jruby-9.2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
gem 'typhoeus'
end

build_coverage_matrix('stripe', 7..12)
build_coverage_matrix('stripe', 7..12, min: '5.15.0')
build_coverage_matrix('opensearch', 2..3, gem: 'opensearch-ruby')
build_coverage_matrix('elasticsearch', 7..8)

Expand Down
2 changes: 1 addition & 1 deletion appraisal/jruby-9.3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
gem 'typhoeus'
end

build_coverage_matrix('stripe', 7..12)
build_coverage_matrix('stripe', 7..12, min: '5.15.0')
build_coverage_matrix('opensearch', 2..3, gem: 'opensearch-ruby')
build_coverage_matrix('elasticsearch', 7..8)

Expand Down
2 changes: 1 addition & 1 deletion appraisal/jruby-9.4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
gem 'typhoeus'
end

build_coverage_matrix('stripe', 7..12)
build_coverage_matrix('stripe', 7..12, min: '5.15.0')
build_coverage_matrix('opensearch', 2..3, gem: 'opensearch-ruby')
build_coverage_matrix('elasticsearch', 7..8)

Expand Down
2 changes: 1 addition & 1 deletion appraisal/ruby-2.5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
gem 'typhoeus'
end

build_coverage_matrix('stripe', 7..12)
build_coverage_matrix('stripe', 7..12, min: '5.15.0')
build_coverage_matrix('opensearch', 2..3, gem: 'opensearch-ruby')
build_coverage_matrix('elasticsearch', 7..8)

Expand Down
2 changes: 1 addition & 1 deletion appraisal/ruby-2.6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
gem 'typhoeus'
end

build_coverage_matrix('stripe', 7..12)
build_coverage_matrix('stripe', 7..12, min: '5.15.0')
build_coverage_matrix('opensearch', 2..3, gem: 'opensearch-ruby')
build_coverage_matrix('elasticsearch', 7..8)

Expand Down
2 changes: 1 addition & 1 deletion appraisal/ruby-2.7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
gem 'typhoeus'
end

build_coverage_matrix('stripe', 7..12)
build_coverage_matrix('stripe', 7..12, min: '5.15.0')
build_coverage_matrix('opensearch', 2..3, gem: 'opensearch-ruby')
build_coverage_matrix('elasticsearch', 7..8)

Expand Down
2 changes: 1 addition & 1 deletion appraisal/ruby-3.0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
gem 'typhoeus'
end

build_coverage_matrix('stripe', 7..12)
build_coverage_matrix('stripe', 7..12, min: '5.15.0')
build_coverage_matrix('opensearch', 2..3, gem: 'opensearch-ruby')
build_coverage_matrix('elasticsearch', 7..8)

Expand Down
2 changes: 1 addition & 1 deletion appraisal/ruby-3.1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
gem 'typhoeus'
end

build_coverage_matrix('stripe', 7..12)
build_coverage_matrix('stripe', 7..12, min: '5.15.0')
build_coverage_matrix('opensearch', 2..3, gem: 'opensearch-ruby')
build_coverage_matrix('elasticsearch', 7..8)

Expand Down
2 changes: 1 addition & 1 deletion appraisal/ruby-3.2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
gem 'typhoeus'
end

build_coverage_matrix('stripe', 7..12)
build_coverage_matrix('stripe', 7..12, min: '5.15.0')
build_coverage_matrix('opensearch', 2..3, gem: 'opensearch-ruby')
build_coverage_matrix('elasticsearch', 7..8)

Expand Down
2 changes: 1 addition & 1 deletion appraisal/ruby-3.3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
gem 'typhoeus'
end

build_coverage_matrix('stripe', 7..12)
build_coverage_matrix('stripe', 7..12, min: '5.15.0')
build_coverage_matrix('opensearch', 2..3, gem: 'opensearch-ruby')
build_coverage_matrix('elasticsearch', 7..8)

Expand Down
2 changes: 1 addition & 1 deletion appraisal/ruby-3.4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
gem 'typhoeus'
end

build_coverage_matrix('stripe', 7..12)
build_coverage_matrix('stripe', 7..12, min: '5.15.0')
build_coverage_matrix('opensearch', 2..3, gem: 'opensearch-ruby')
build_coverage_matrix('elasticsearch', 7..8)

Expand Down
49 changes: 49 additions & 0 deletions tasks/appraisal_conversion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'pathname'

# This module translates our custom mapping between appraisal and bundler.
#
# It cannot be included into `Appraisal` file, because it was invoked via `instance_eval`.
module AppraisalConversion
module_function

@gemfile_dir = 'gemfiles'
@definition_dir = 'appraisal'

def to_bundle_gemfile(group)
gemfile = "#{runtime_identifier}_#{group}.gemfile".tr('-', '_')
path = root_path.join(gemfile_dir, gemfile)

if path.exist?
path.to_s
else
raise "Gemfile not found at #{path}"
end
end

def definition
path = root_path.join(@definition_dir, "#{runtime_identifier}.rb")

if path.exist?
path.to_s
else
raise "Definition not found at #{path}"
end
end

def runtime_identifier
major, minor, = Gem::Version.new(RUBY_ENGINE_VERSION).segments
"#{RUBY_ENGINE}-#{major}.#{minor}"
end

def gemfile_pattern
root_path + gemfile_dir + "#{runtime_identifier.tr('-', '_')}_*.gemfile"
end

def gemfile_dir
@gemfile_dir
end

def root_path
Pathname.pwd
end
end
49 changes: 49 additions & 0 deletions tasks/dependency.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'open3'

require_relative 'appraisal_conversion'

task :dep => :dependency
task :dependency => %w[dependency:lock]
namespace :dependency do
# rubocop:disable Style/MultilineBlockChain
Dir.glob(AppraisalConversion.gemfile_pattern).each do |gemfile|
# desc "Lock the dependencies for #{gemfile}"
task gemfile do
Bundler.with_unbundled_env do
command = +'bundle lock'
command << ' --add-platform x86_64-linux aarch64-linux' unless RUBY_PLATFORM == 'java'
output, = Open3.capture2e({ 'BUNDLE_GEMFILE' => gemfile.to_s }, command)

puts output
end
end
end.tap do |gemfiles|
desc "Lock the dependencies for #{AppraisalConversion.runtime_identifier}"
# WHY can't we use `multitask :lock => gemfiles` here?
#
# Running bundler in parallel has various race conditions
#
# Race condition with the file system, particularly worse with JRuby.
# For instance, `Errno::ENOENT: No such file or directory - bundle` is raised with JRuby 9.2

# Even with CRuby, `simplcov` declaration with `github` in Gemfile causes
# race condition for the local gem cache with the following error:

# ```
# [/usr/local/bundle/bundler/gems/simplecov-3bb6b7ee58bf/simplecov.gemspec] isn't a Gem::Specification (NilClass instead).
# ```

# and

# ```
# fatal: Unable to create '/usr/local/bundle/bundler/gems/simplecov-3bb6b7ee58bf/.git/index.lock': File exists.
# Another git process seems to be running in this repository, e.g.
# an editor opened by 'git commit'. Please make sure all processes
# are terminated then try again. If it still fails, a git process
# may have crashed in this repository earlier:
# remove the file manually to continue.
# ```
task :lock => gemfiles
end
# rubocop:enable Style/MultilineBlockChain
end
Loading

0 comments on commit 4dd912a

Please sign in to comment.