Skip to content

Commit

Permalink
Parallelize Ruby tests in CI build
Browse files Browse the repository at this point in the history
This is closely based on alphagov/whitehall#8364.

I've put `minitest-ci` in the `scripts` directory rather than the `bin`
directory, because I think the latter is usually reserved for built-in
scripts.

I've made `minitest-ci` executable in the git repo rather than having a
separate step in the workflow job.

I've run rubocop auto-correct against `minitest-ci` to fix some
violations.

I haven't bothered to split the Ruby tests out into a separate workflow
file for now.
  • Loading branch information
floehopper committed Oct 25, 2023
1 parent 2ddf478 commit b98ab99
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ jobs:
with:
useWithRails: true

test-ruby:
name: Test Ruby
test-ruby-matrix:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ci_node_total: [4]
ci_node_index: [0, 1, 2, 3]
steps:
- name: Setup MySQL
id: setup-mysql
Expand Down Expand Up @@ -72,5 +76,13 @@ jobs:
env:
RAILS_ENV: test
TEST_DATABASE_URL: ${{ steps.setup-mysql.outputs.db-url }}
run: bundle exec rake test
CI_NODE_TOTAL: ${{ matrix.ci_node_total }}
CI_NODE_INDEX: ${{ matrix.ci_node_index }}
run: ./script/minitest-ci

test-ruby:
name: Test Ruby
needs: test-ruby-matrix
runs-on: ubuntu-latest
steps:
- run: echo "All Minitest tests have passed 🚀"
16 changes: 16 additions & 0 deletions script/minitest-ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby

# This script is used during the GitHub Actions workflow
# defined in .github/workflows/ci.yml.
# It splits the MiniTest suite into randomly-allocated groups
# which are executed across multiple GitHub Actions 'matrix' nodes.

tests = Dir["test/**/*_test.rb"]
.sort
.shuffle(random: Random.new(ENV["GITHUB_SHA"].to_i(16)))
.select
.with_index do |_el, i|
i % ENV["CI_NODE_TOTAL"].to_i == ENV["CI_NODE_INDEX"].to_i
end

exec "bundle exec rails test #{tests.join(' ')}"

0 comments on commit b98ab99

Please sign in to comment.