|
| 1 | +require 'json' |
| 2 | +require "psych" |
| 3 | +# rubocop:disable Metrics/BlockLength |
| 4 | +namespace :github do |
| 5 | + namespace :actions do |
| 6 | + task :test_template do |t| |
| 7 | + runtimes = [ |
| 8 | + "ruby:3.3", |
| 9 | + "ruby:3.2", |
| 10 | + "ruby:3.1", |
| 11 | + "ruby:3.0", |
| 12 | + ].map do |runtime| |
| 13 | + engine, version = runtime.split(':') |
| 14 | + runtime_alias = "#{engine}-#{version.gsub('.', '')}" |
| 15 | + |
| 16 | + OpenStruct.new( |
| 17 | + "engine" => engine, |
| 18 | + "version" => version, |
| 19 | + "alias" => runtime_alias, |
| 20 | + "image" => "ghcr.io/datadog/images-rb/engines/#{engine}:#{version}" |
| 21 | + ) |
| 22 | + end |
| 23 | + |
| 24 | + test_jobs = runtimes.map do |runtime| |
| 25 | + { |
| 26 | + "test-#{runtime.alias}" => { |
| 27 | + "name" => "Test on #{runtime.engine} #{runtime.version}", |
| 28 | + "needs" => ["compute_tasks"], |
| 29 | + "runs-on" => "ubuntu-22.04", |
| 30 | + "strategy" => { |
| 31 | + "fail-fast" => false, |
| 32 | + "matrix" => { |
| 33 | + "include" => "${{ fromJson(needs.compute_tasks.outputs.#{runtime.alias}-matrix) }}" |
| 34 | + } |
| 35 | + }, |
| 36 | + "container" => { "image" => runtime.image }, |
| 37 | + "steps" => [ |
| 38 | + { "uses" => "actions/checkout@v4" }, |
| 39 | + { "run" => "bundle install" }, |
| 40 | + { "run" => "bundle exec rake test:${{ matrix.task }}" } |
| 41 | + ] |
| 42 | + } |
| 43 | + } |
| 44 | + end |
| 45 | + |
| 46 | + compute_tasks = { |
| 47 | + "runs-on" => "ubuntu-22.04", |
| 48 | + "strategy" => { |
| 49 | + "fail-fast" => false, |
| 50 | + "matrix" => { |
| 51 | + "engine" => runtimes.map do |runtime| |
| 52 | + { "name" => runtime.engine, "version" => runtime.version, "alias" => runtime.alias } |
| 53 | + end |
| 54 | + } |
| 55 | + }, |
| 56 | + "container" =>{ |
| 57 | + "image" => "ghcr.io/datadog/images-rb/engines/${{ matrix.engine.name }}:${{ matrix.engine.version }}" |
| 58 | + }, |
| 59 | + "outputs" => runtimes.each_with_object({}) do |runtime, hash| |
| 60 | + hash["#{runtime.alias}-matrix"] = "${{ steps.set-matrix.outputs.#{runtime.alias} }}" |
| 61 | + end, |
| 62 | + "steps" => [ |
| 63 | + { "uses" => "actions/checkout@v4" }, |
| 64 | + { "run" => "apt update && apt install jq -y" }, |
| 65 | + { "run" => "bundle install" }, |
| 66 | + { |
| 67 | + "id" => "set-matrix", |
| 68 | + "run" => <<~BASH |
| 69 | + matrix_json=$(bundle exec rake github:generate_matrix) |
| 70 | + # Debug output |
| 71 | + echo "Generated JSON:" |
| 72 | + echo "$matrix_json" |
| 73 | + # Set the output |
| 74 | + echo "${{ matrix.engine.alias }}=$(echo "$matrix_json" | jq -c .)" >> $GITHUB_OUTPUT |
| 75 | + BASH |
| 76 | + }, |
| 77 | + ] |
| 78 | + } |
| 79 | + |
| 80 | + base = { |
| 81 | + "name" => 'Test', |
| 82 | + "on" => ['push'], |
| 83 | + "jobs" => { |
| 84 | + "compute_tasks" => compute_tasks, |
| 85 | + **test_jobs.reduce(&:merge) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + string = +"" |
| 90 | + string << <<~EOS |
| 91 | + # Please do NOT manually edit this file. |
| 92 | + # This file is generated by 'bundle exec rake #{t.name}' |
| 93 | + EOS |
| 94 | + string << Psych.dump(base, line_width: 120) |
| 95 | + |
| 96 | + File.binwrite(".github/workflows/test.yml", string) |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + task :generate_matrix do |
| 101 | + matrix = eval(File.read('Matrixfile')).freeze # rubocop:disable Security/Eval |
| 102 | + |
| 103 | + matrix = matrix.slice("stripe") |
| 104 | + |
| 105 | + ruby_version = RUBY_VERSION[0..2] |
| 106 | + major, minor, = Gem::Version.new(RUBY_ENGINE_VERSION).segments |
| 107 | + ruby_runtime = "#{RUBY_ENGINE}-#{major}.#{minor}" |
| 108 | + array = [] |
| 109 | + matrix.each do |key, spec_metadata| |
| 110 | + matched = spec_metadata.any? do |appraisal_group, rubies| |
| 111 | + if RUBY_PLATFORM == 'java' |
| 112 | + rubies.include?("✅ #{ruby_version}") && rubies.include?('✅ jruby') |
| 113 | + else |
| 114 | + rubies.include?("✅ #{ruby_version}") |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + if matched |
| 119 | + array << { task: key } |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + puts JSON.pretty_generate(array) |
| 124 | + end |
| 125 | +end |
| 126 | +# rubocop:enable Metrics/BlockLength |
0 commit comments