Skip to content

Commit

Permalink
Parallelise MiniTest workflow
Browse files Browse the repository at this point in the history
This configures the MiniTest workflow to run
using a matrix strategy. It run separate parts
of the test suite on different nodes and hence
speeds up total run time of the CI/CD pipeline.
  • Loading branch information
Jonathan Young committed Oct 12, 2023
1 parent d1610ab commit 07cd144
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ jobs:
test-ruby:
name: Test Ruby
uses: ./.github/workflows/minitest.yml

test-ruby-parallel:
name: Test Ruby
uses: ./.github/workflows/minitest-parallel.yml

integration-tests-parallel:
name: Integration tests
Expand Down
79 changes: 79 additions & 0 deletions .github/workflows/minitest-parallel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Run Minitest

on:
workflow_call:
inputs:
ref:
description: 'The branch, tag or SHA to checkout'
required: false
type: string
publishingApiRef:
description: 'The branch, tag or SHA to checkout Publishing API'
required: false
default: 'main'
type: string

jobs:
run-minitest:
name: Run Minitest
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
uses: alphagov/govuk-infrastructure/.github/actions/setup-mysql@main

- name: Setup Redis
uses: alphagov/govuk-infrastructure/.github/actions/setup-redis@main

- name: Install additional system dependencies
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends ghostscript
- name: Checkout repository
uses: actions/checkout@v4
with:
repository: alphagov/whitehall
ref: ${{ inputs.ref || github.ref }}

- name: Checkout Publishing API (for Content Schemas)
uses: actions/checkout@v4
with:
repository: alphagov/publishing-api
ref: ${{ inputs.publishingApiRef }}
path: vendor/publishing-api

- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true

- name: Setup Node
uses: alphagov/govuk-infrastructure/.github/actions/setup-node@main

- name: Precompile assets
uses: alphagov/govuk-infrastructure/.github/actions/precompile-rails-assets@main

- name: Initialize database
env:
RAILS_ENV: test
TEST_DATABASE_URL: ${{ steps.setup-mysql.outputs.db-url }}
run: bundle exec rails db:setup

- name: Make bin/minitest-ci executable
run: chmod +x ./bin/minitest-ci

- name: Run Minitest
env:
RAILS_ENV: test
GOVUK_CONTENT_SCHEMAS_PATH: vendor/publishing-api/content_schemas
TEST_DATABASE_URL: ${{ steps.setup-mysql.outputs.db-url }}
CI_NODE_TOTAL: ${{ matrix.ci_node_total }}
CI_NODE_INDEX: ${{ matrix.ci_node_index }}
run: |
./bin/minitest-ci
11 changes: 11 additions & 0 deletions bin/minitest-ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby

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 07cd144

Please sign in to comment.