Skip to content

Commit d865382

Browse files
authored
[NO-TICKET] Add seed to CI unit tests (#4953)
1 parent 0a32491 commit d865382

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

.github/workflows/_unit_test.yml

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Unit Test Template
1+
name: Unit Tests (Ruby version specific)
22

33
on: # yamllint disable-line rule:truthy
44
workflow_call:
@@ -13,6 +13,9 @@ on: # yamllint disable-line rule:truthy
1313
alias:
1414
required: true
1515
type: string
16+
seed:
17+
required: false
18+
type: number
1619
outputs:
1720
lockfile:
1821
description: "The lockfile artifact"
@@ -21,11 +24,33 @@ on: # yamllint disable-line rule:truthy
2124
description: "The cache key for bundle"
2225
value: ${{ jobs.batch.outputs.cache-key }}
2326

27+
workflow_dispatch:
28+
inputs:
29+
engine:
30+
description: "`ruby` or `jruby`"
31+
required: true
32+
type: string
33+
version:
34+
description: "The version of the engine (2.x or 3.x for Ruby, 9.x or 10.x for JRuby)"
35+
required: true
36+
type: string
37+
alias:
38+
description: "The alias of the engine (e.g. `ruby-34` for Ruby 3.4)"
39+
required: true
40+
type: string
41+
seed:
42+
description: "The seed to reproduce a CI run"
43+
required: false
44+
type: number
45+
46+
permissions: {}
47+
2448
jobs:
2549
batch:
2650
runs-on: ubuntu-24.04
2751
name: batch
2852
outputs:
53+
seed: "${{ steps.set-batches.outputs.seed }}"
2954
batches: "${{ steps.set-batches.outputs.batches }}"
3055
misc: "${{ steps.set-batches.outputs.misc }}"
3156
cache-key: "${{ steps.bundle-cache.outputs.cache-key }}"
@@ -43,20 +68,23 @@ jobs:
4368

4469
- id: set-batches
4570
name: Distribute tasks into batches
71+
env:
72+
CI_TEST_SEED: "${{ inputs.seed || '' }}"
4673
run: |
4774
data=$(bundle exec rake github:generate_batches)
4875
echo "$data" | ruby -rjson -e 'puts JSON.pretty_generate(JSON.parse(STDIN.read))'
4976
5077
# Extract each key and set it as a separate output
78+
seed_data=$(echo "$data" | ruby -rjson -e 'puts JSON.parse(STDIN.read)["seed"]')
5179
batches_data=$(echo "$data" | ruby -rjson -e 'puts JSON.parse(STDIN.read)["batches"].to_json')
5280
misc_data=$(echo "$data" | ruby -rjson -e 'puts JSON.parse(STDIN.read)["misc"].to_json')
5381
54-
echo "batches=$batches_data" >> "$GITHUB_OUTPUT"
55-
echo "misc=$misc_data" >> "$GITHUB_OUTPUT"
82+
{ echo "seed=$seed_data"; echo "batches=$batches_data"; echo "misc=$misc_data"; } >> "$GITHUB_OUTPUT"
5683
- name: Generate batch summary
57-
run: bundle exec rake github:generate_batch_summary
5884
env:
5985
batches_json: "${{ steps.set-batches.outputs.batches }}"
86+
CI_TEST_SEED: "${{ steps.set-batches.outputs.seed }}"
87+
run: bundle exec rake github:generate_batch_summary
6088

6189
# `Initialize containers` step becomes quite heavily when many services are used.
6290
#
@@ -88,6 +116,7 @@ jobs:
88116
timeout-minutes: 30
89117
env:
90118
BATCHED_TASKS: "${{ toJSON(matrix.tasks) }}"
119+
CI_TEST_SEED: "${{ needs.batch.outputs.seed }}"
91120
strategy:
92121
fail-fast: false
93122
matrix:
@@ -152,6 +181,7 @@ jobs:
152181
timeout-minutes: 30
153182
env:
154183
BATCHED_TASKS: "${{ toJSON(matrix.tasks) }}"
184+
CI_TEST_SEED: "${{ needs.batch.outputs.seed }}"
155185
strategy:
156186
fail-fast: false
157187
matrix:

tasks/github.rake

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ namespace :github do
4646
end
4747
end
4848

49-
# Random!
50-
matching_tasks.shuffle!
49+
# Seed
50+
rng = (ENV['CI_TEST_SEED'] && ENV['CI_TEST_SEED'] != '') ? Random.new(ENV['CI_TEST_SEED'].to_i) : Random.new
51+
matching_tasks.shuffle!(random: rng)
5152

5253
batch_count = 7
5354
batch_count *= 2 if RUBY_PLATFORM == 'java'
@@ -61,6 +62,7 @@ namespace :github do
6162
end
6263

6364
data = {
65+
seed: rng.seed,
6466
batches: batched_matrix,
6567
misc: {'include' => [{'batch' => "0", 'tasks' => misc_tasks}]}
6668
}
@@ -77,6 +79,7 @@ namespace :github do
7779
summary = ENV['GITHUB_STEP_SUMMARY']
7880

7981
File.open(summary, 'a') do |f|
82+
f.puts "*__Seed__: #{ENV["CI_TEST_SEED"]}*"
8083
data['include'].each do |batch|
8184
rows = batch['tasks'].map do |t|
8285
"* #{t["task"]} (#{t["group"]})"
@@ -118,9 +121,11 @@ namespace :github do
118121
task :run_batch_tests do
119122
tasks = JSON.parse(ENV['BATCHED_TASKS'] || {})
120123

124+
rng = Random.new(ENV['CI_TEST_SEED'].to_i)
125+
121126
tasks.each do |task|
122127
env = {'BUNDLE_GEMFILE' => task['gemfile']}
123-
cmd = "bundle exec rake spec:#{task["task"]}"
128+
cmd = "bundle exec rake spec:#{task["task"]}['--seed #{rng.rand(0xFFFF)}']"
124129

125130
Bundler.with_unbundled_env { sh(env, cmd) }
126131
end

0 commit comments

Comments
 (0)