|
| 1 | +require 'json' |
| 2 | +require "psych" |
| 3 | +require 'ostruct' |
| 4 | +require_relative 'appraisal_conversion' |
| 5 | + |
| 6 | +# rubocop:disable Metrics/BlockLength |
| 7 | +namespace :github do |
| 8 | + namespace :actions do |
| 9 | + task :test_template do |t| |
| 10 | + ubuntu = "ubuntu-22.04" |
| 11 | + |
| 12 | + runtimes = [ |
| 13 | + "ruby:3.3", |
| 14 | + "ruby:3.2", |
| 15 | + # "ruby:3.1", |
| 16 | + # "ruby:3.0", |
| 17 | + # "ruby:2.7", |
| 18 | + # "ruby:2.6", |
| 19 | + # "ruby:2.5", |
| 20 | + # "jruby:9.4", |
| 21 | + # "jruby:9.3", |
| 22 | + # "jruby:9.2", |
| 23 | + ].map do |runtime| |
| 24 | + engine, version = runtime.split(':') |
| 25 | + runtime_alias = "#{engine}-#{version.gsub('.', '')}" |
| 26 | + |
| 27 | + OpenStruct.new( |
| 28 | + "engine" => engine, |
| 29 | + "version" => version, |
| 30 | + "alias" => runtime_alias, |
| 31 | + "image" => "ghcr.io/datadog/images-rb/engines/#{engine}:#{version}" |
| 32 | + ) |
| 33 | + end |
| 34 | + |
| 35 | + test_jobs = runtimes.map do |runtime| |
| 36 | + { |
| 37 | + "test-#{runtime.alias}" => { |
| 38 | + "name" => "#{runtime.engine}-#{runtime.version}: ${{ matrix.task }} (${{ matrix.group }})", |
| 39 | + "needs" => ["compute_tasks"], |
| 40 | + "runs-on" => ubuntu, |
| 41 | + "strategy" => { |
| 42 | + "fail-fast" => false, |
| 43 | + "matrix" => { |
| 44 | + "include" => "${{ fromJson(needs.compute_tasks.outputs.#{runtime.alias}-matrix) }}" |
| 45 | + } |
| 46 | + }, |
| 47 | + "steps" => [ |
| 48 | + { "uses" => "actions/checkout@v4" }, |
| 49 | + { |
| 50 | + "name" => "Configure Git", |
| 51 | + "run" => 'git config --global --add safe.directory "$GITHUB_WORKSPACE"' |
| 52 | + }, |
| 53 | + { |
| 54 | + "uses" => "actions/download-artifact@v4", |
| 55 | + "with" => { |
| 56 | + "name" => "bundled-dependencies-${{ github.run_id }}-#{runtime.alias}", |
| 57 | + } |
| 58 | + }, |
| 59 | + { "run" => "bundle install --local" }, |
| 60 | + { |
| 61 | + "name" => "Test ${{ matrix.task }} with ${{ matrix.gemfile }}", |
| 62 | + "env" => { "BUNDLE_GEMFILE" => "${{ matrix.gemfile }}" }, |
| 63 | + "run" => "bundle install && bundle exec rake spec:${{ matrix.task }}" |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + } |
| 68 | + end |
| 69 | + |
| 70 | + compute_tasks = { |
| 71 | + "runs-on" => ubuntu, |
| 72 | + "strategy" => { |
| 73 | + "fail-fast" => false, |
| 74 | + "matrix" => { |
| 75 | + "engine" => runtimes.map do |runtime| |
| 76 | + { "name" => runtime.engine, "version" => runtime.version, "alias" => runtime.alias } |
| 77 | + end |
| 78 | + } |
| 79 | + }, |
| 80 | + "container" =>{ |
| 81 | + "image" => "ghcr.io/datadog/images-rb/engines/${{ matrix.engine.name }}:${{ matrix.engine.version }}" |
| 82 | + }, |
| 83 | + "outputs" => runtimes.each_with_object({}) do |runtime, hash| |
| 84 | + hash["#{runtime.alias}-matrix"] = "${{ steps.set-matrix.outputs.#{runtime.alias} }}" |
| 85 | + end, |
| 86 | + "steps" => [ |
| 87 | + { "uses" => "actions/checkout@v4" }, |
| 88 | + { "run" => "bundle install" }, |
| 89 | + { |
| 90 | + "id" => "set-matrix", |
| 91 | + "run" => <<~BASH |
| 92 | + matrix_json=$(bundle exec rake github:generate_matrix) |
| 93 | + # Debug output |
| 94 | + echo "Generated JSON:" |
| 95 | + echo "$matrix_json" |
| 96 | + # Set the output |
| 97 | + echo "${{ matrix.engine.alias }}=$(echo "$matrix_json")" >> $GITHUB_OUTPUT |
| 98 | + BASH |
| 99 | + }, |
| 100 | + { "run" => "bundle cache" }, |
| 101 | + { |
| 102 | + "uses" => "actions/upload-artifact@v4", |
| 103 | + "with" => { |
| 104 | + "name" => "bundled-dependencies-${{ github.run_id }}-${{ matrix.engine.alias }}", |
| 105 | + "retention-days" => 1, |
| 106 | + "path" => <<~STRING |
| 107 | + Gemfile.lock |
| 108 | + vendor/ |
| 109 | + STRING |
| 110 | + } |
| 111 | + }, |
| 112 | + ] |
| 113 | + } |
| 114 | + |
| 115 | + base = { |
| 116 | + "name" => 'Test', |
| 117 | + "on" => ['push'], |
| 118 | + "concurrency" => { |
| 119 | + "group" => '${{ github.workflow }}-${{ github.ref }}', |
| 120 | + "cancel-in-progress" => '${{ github.ref != \'refs/heads/master\' }}' |
| 121 | + }, |
| 122 | + "jobs" => { |
| 123 | + "compute_tasks" => compute_tasks, |
| 124 | + **test_jobs.reduce(&:merge) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + # `Psych.dump` directly creates anchors, but Github Actions does not support anchors for YAML, |
| 129 | + # convert to JSON first to avoid anchors |
| 130 | + json = JSON.dump(base) |
| 131 | + yaml = Psych.safe_load(json) |
| 132 | + |
| 133 | + string = +"" |
| 134 | + string << <<~EOS |
| 135 | + # Please do NOT manually edit this file. |
| 136 | + # This file is generated by 'bundle exec rake #{t.name}' |
| 137 | + EOS |
| 138 | + string << Psych.dump(yaml, line_width: 120) |
| 139 | + File.binwrite(".github/workflows/test.yml", string) |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + task :generate_matrix do |
| 144 | + matrix = TEST_METADATA |
| 145 | + |
| 146 | + ruby_version = RUBY_VERSION[0..2] |
| 147 | + major, minor, = Gem::Version.new(RUBY_ENGINE_VERSION).segments |
| 148 | + ruby_runtime = "#{RUBY_ENGINE}-#{major}.#{minor}" |
| 149 | + array = [] |
| 150 | + matrix.each do |key, spec_metadata| |
| 151 | + spec_metadata.each do |group, rubies| |
| 152 | + matched = if RUBY_PLATFORM == 'java' |
| 153 | + rubies.include?("✅ #{ruby_version}") && rubies.include?('✅ jruby') |
| 154 | + else |
| 155 | + rubies.include?("✅ #{ruby_version}") |
| 156 | + end |
| 157 | + |
| 158 | + if matched |
| 159 | + gemfile = AppraisalConversion.to_bundle_gemfile(group) rescue "Gemfile" |
| 160 | + |
| 161 | + array << { |
| 162 | + group: group, |
| 163 | + gemfile: gemfile, |
| 164 | + task: key |
| 165 | + } |
| 166 | + end |
| 167 | + end |
| 168 | + end |
| 169 | + |
| 170 | + puts JSON.dump(array) |
| 171 | + end |
| 172 | +end |
| 173 | +# rubocop:enable Metrics/BlockLength |
0 commit comments