跳至内容

Gradle test sharding

The Tuist Gradle plugin includes built-in support for test sharding. It plans the test suites that Tuist recorded in previous CI runs of your projects' test tasks and uses the Tuist server to create balanced shard plans based on historical timing data.

REQUIREMENTS

How it works#

Test sharding follows a two-phase workflow:

  1. Build phase: Tuist creates a shard plan on the server from the test suites recorded for your projects. The server uses historical test timing data from the last 30 days to distribute tests across shards so each shard takes roughly the same amount of time. The build phase outputs a shard matrix that your CI system uses to spawn parallel runners.
  2. Test phase: Each CI runner receives a shard index and executes only the tests assigned to that shard. The last shard also runs every test suite that is not assigned to another shard.

Build phase#

Prepare test shards using the tuistPrepareTestShards task:

bash
./gradlew tuistPrepareTestShards \
-PtuistShardMax=5

This task:

  1. Finds the projects that have the test task to shard, without compiling them
  2. Creates a shard plan on the Tuist server from the test suites and timings recorded for those projects in the last 30 days
  3. Outputs a shard matrix for your CI system

When no test suites are recorded yet, for example on the first run, the plan has a single shard that runs every test.

Build options#

Configure sharding via Gradle project properties:

PropertyDescription
-PtuistShardMax=<N>Maximum number of shards (default: 2)
-PtuistShardMin=<N>Minimum number of shards
-PtuistShardMaxDuration=<MS>Target maximum duration per shard in milliseconds
-PtuistShardTestTask=<NAME>Test task whose tests are sharded, for example testDebugUnitTest. Defaults to every test task in the build

Android projects with multiple build variants have one unit test task per variant. Set -PtuistShardTestTask to the variant your shards run so only the projects that have it are planned:

bash
./gradlew tuistPrepareTestShards \
-PtuistShardMax=5 \
-PtuistShardTestTask=testDebugUnitTest

The shard reference is automatically derived from CI environment variables (GITHUB_RUN_ID, CI_PIPELINE_ID, etc.) or can be set explicitly via the TUIST_SHARD_REFERENCE environment variable.

Test phase#

Each shard runner executes its assigned tests using the standard test task. When TUIST_SHARD_INDEX is set, the plugin automatically fetches the shard assignment from the server and filters the test execution to include only the assigned test suites. Test tasks in projects with no suites assigned to the shard are skipped.

The last shard runs every test task and excludes the suites assigned to the other shards, so suites that have no recorded runs yet, such as newly added ones, still run.

bash
TUIST_SHARD_INDEX=0 ./gradlew test

If you set -PtuistShardTestTask in the build phase, run that same task in each shard, for example ./gradlew testDebugUnitTest.

Continuous integration#

Tuist automatically detects the following CI providers:

For other providers, refer to the .tuist-shard-matrix.json file to set up parallel jobs.

GitHub Actions#

Use a matrix strategy to run shards in parallel:

yaml
name: Tests
on: [pull_request]
jobs:
build:
name: Build test shards
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.build.outputs.matrix }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- uses: gradle/actions/setup-gradle@v4
- run: tuist auth login
- id: build
run: ./gradlew tuistPrepareTestShards -PtuistShardMax=5
test:
name: "Shard #${{ matrix.shard }}"
needs: build
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
shard: ${{ fromJson(needs.build.outputs.matrix).shard }}
env:
TUIST_SHARD_INDEX: ${{ matrix.shard }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- uses: gradle/actions/setup-gradle@v4
- run: tuist auth login
- run: ./gradlew test

GitLab CI#

Tuist generates a .tuist-shard-child-pipeline.yml that you trigger as a child pipeline. Define a .tuist-shard template job that the generated shard jobs extend:

yaml
# .gitlab-ci.yml
stages:
- build
- test
build-shards:
stage: build
script:
- tuist auth login
- ./gradlew tuistPrepareTestShards -PtuistShardMax=5
artifacts:
paths:
- .tuist-shard-child-pipeline.yml
test-shards:
stage: test
needs: [build-shards]
trigger:
include:
- artifact: .tuist-shard-child-pipeline.yml
job: build-shards
strategy: depend

The child pipeline needs a .tuist-shard job template:

yaml
# .gitlab/shard-template.yml
.tuist-shard:
script:
- tuist auth login
- ./gradlew test

CircleCI#

Tuist generates a .tuist-shard-continuation.json with parameters for the continuation orb:

yaml
# .circleci/config.yml
version: 2.1
setup: true
orbs:
continuation: circleci/continuation@1
jobs:
build-shards:
docker:
- image: cimg/openjdk:17.0
steps:
- checkout
- run:
name: Build and plan shards
command: |
tuist auth login
./gradlew tuistPrepareTestShards -PtuistShardMax=5
- continuation/continue:
configuration_path: .circleci/continue-config.yml
parameters: .tuist-shard-continuation.json
workflows:
setup:
jobs:
- build-shards
yaml
# .circleci/continue-config.yml
version: 2.1
parameters:
shard-indices:
type: string
default: ""
shard-count:
type: integer
default: 0
jobs:
test-shard:
docker:
- image: cimg/openjdk:17.0
parameters:
shard-index:
type: integer
steps:
- checkout
- run:
name: Run shard
command: |
export TUIST_SHARD_INDEX=<< parameters.shard-index >>
tuist auth login
./gradlew test
workflows:
test:
jobs:
- test-shard:
matrix:
parameters:
shard-index: [<< pipeline.parameters.shard-indices >>]

Buildkite#

Tuist generates a .tuist-shard-pipeline.yml with one step per shard. Upload it with buildkite-agent pipeline upload:

yaml
# pipeline.yml
steps:
- label: "Build test shards"
command: |
tuist auth login
./gradlew tuistPrepareTestShards -PtuistShardMax=5
buildkite-agent pipeline upload .tuist-shard-pipeline.yml

Each generated step has TUIST_SHARD_INDEX set in its environment. Add the test command to each shard step using a shared script:

bash
# .buildkite/shard-step.sh
#!/bin/bash
tuist auth login
./gradlew test

Codemagic#

Codemagic does not support dynamic matrix jobs, so define a separate workflow per shard. Tuist writes TUIST_SHARD_MATRIX and TUIST_SHARD_COUNT to the CM_ENV file for use within each workflow:

yaml
# codemagic.yaml
workflows:
build-shards:
name: Build test shards
instance_type: linux_x2
environment:
java: 17
scripts:
- name: Build and plan shards
script: |
tuist auth login
./gradlew tuistPrepareTestShards -PtuistShardMax=5
test-shard-0: &shard-workflow
name: "Shard #0"
instance_type: linux_x2
environment:
java: 17
vars:
TUIST_SHARD_INDEX: 0
scripts:
- name: Run shard
script: |
tuist auth login
./gradlew test
test-shard-1:
<<: *shard-workflow
name: "Shard #1"
environment:
java: 17
vars:
TUIST_SHARD_INDEX: 1
test-shard-2:
<<: *shard-workflow
name: "Shard #2"
environment:
java: 17
vars:
TUIST_SHARD_INDEX: 2
test-shard-3:
<<: *shard-workflow
name: "Shard #3"
environment:
java: 17
vars:
TUIST_SHARD_INDEX: 3
test-shard-4:
<<: *shard-workflow
name: "Shard #4"
environment:
java: 17
vars:
TUIST_SHARD_INDEX: 4

Bitrise#

On Bitrise, Tuist writes .tuist-shard-matrix.json to the BITRISE_DEPLOY_DIR, making it available as a build artifact for downstream pipeline stages. Use Bitrise Pipelines with pre-defined parallel workflows:

yaml
# bitrise.yml
pipelines:
test-pipeline:
stages:
- build-stage: {}
- test-stage: {}
stages:
build-stage:
workflows:
- build-shards: {}
test-stage:
workflows:
- test-shard-0: {}
- test-shard-1: {}
- test-shard-2: {}
- test-shard-3: {}
- test-shard-4: {}
workflows:
build-shards:
steps:
- script:
title: Build and plan shards
inputs:
- content: |
tuist auth login
./gradlew tuistPrepareTestShards -PtuistShardMax=5
test-shard-0: &shard-workflow
envs:
- TUIST_SHARD_INDEX: 0
steps:
- script:
title: Run shard
inputs:
- content: |
tuist auth login
./gradlew test
test-shard-1:
<<: *shard-workflow
envs:
- TUIST_SHARD_INDEX: 1
test-shard-2:
<<: *shard-workflow
envs:
- TUIST_SHARD_INDEX: 2
test-shard-3:
<<: *shard-workflow
envs:
- TUIST_SHARD_INDEX: 3
test-shard-4:
<<: *shard-workflow
envs:
- TUIST_SHARD_INDEX: 4
Tip

Bitrise does not support dynamic parallel job creation at runtime. Define a fixed number of shard workflows in your pipeline stages — workflows within a stage run in parallel automatically.