domain-driven-test-pipelines.md (8725B)
1 --- 2 title: Domain-Driven Test Pipelines 3 description: An effective strategy for optimizing CI pipelines in large, domain-driven projects by leveraging GitHub Actions matrix workflows to run tests per domain (e.g., user, order, shipping) in parallel. 4 --- 5 6 # Domain-Driven Test Pipelines 7 8 2025-09-13 9 10 When building CI pipelines, a [matrix strategy](https://docs.github.com/en/actions/how-tos/write-workflows/choose-what-workflows-do/run-job-variations) is a configuration setup that allows a CI system to run multiple tests or build jobs in parallel with different combinations of parameters. For example, building by operating systems, programming language versions, or environment settings. Typically these are used to run tests against multiple environments and versions of software. For example, running python tests across different versions of python on different operating systems. 11 12 ```yaml 13 jobs: 14 example_matrix: 15 strategy: 16 matrix: 17 python_version: [10, 12, 14] 18 os: [ubuntu-latest, windows-latest] 19 ``` 20 21 In theory, a domain-driven project will have their tests logically divided into these groupings as well. 22 23 ```plaintext 24 com/example/api/ 25 ├── src/ 26 │ ├── user/ 27 │ │ ├── service/ 28 │ │ ├── repository/ 29 │ │ ├── controller/ 30 │ ├── order/ 31 │ │ ├── service/ 32 │ │ ├── repository/ 33 │ │ ├── controller/ 34 │ ├── shipping/ 35 │ │ ├── service/ 36 │ │ ├── repository/ 37 │ │ ├── controller/ 38 ├── test/ 39 │ ├── user/ 40 │ │ ├── service/ 41 │ │ ├── controller/ 42 │ ├── order/ 43 │ │ ├── service/ 44 │ │ ├── controller/ 45 │ ├── shipping/ 46 │ │ ├── service/ 47 │ │ ├── controller/ 48 ``` 49 50 As your codebase grows and your tests grow with it, any tests that require external dependencies may become resource constrained. For example, attempting to run parallel tests against the same instance of postgres. There are two bottlenecks that can occur in this situation: 51 - Limited amounts of tests can run in parallel 52 - Limited database connections can be opened and shared across tests 53 54 What if we leveraged the build matrix mechanism to run these tests in parallel? Each domain would run it's tests with it's own DB instance and in isolation of other domains. 55 56 ## Parameterizing Test Jobs 57 58 If you're using [GitHub Actions](https://docs.github.com/en/actions), it's common to use [reusable workflows](https://docs.github.com/en/actions/how-tos/reuse-automations/reuse-workflows) to reuse existing snippets of steps to run your jobs. This is especially useful when parameterizing the domain of tests that we intend to run. 59 60 ### Gradle Example 61 62 ```yaml 63 # .github/workflows/test.yml 64 65 on: 66 workflow_call: 67 inputs: 68 domain: 69 required: true 70 type: string 71 72 jobs: 73 unit_test: 74 runs-on: depot-ubuntu-22.04 75 steps: 76 - name: Checkout Repository 77 uses: actions/checkout@v4 78 with: 79 ref: ${{ github.head_ref }} 80 81 - name: Setup Java 21 82 uses: actions/setup-java@v4.5.0 83 with: 84 distribution: 'corretto' 85 java-version: '21' 86 87 - name: Setup Gradle 88 uses: gradle/actions/setup-gradle@v4 89 with: 90 gradle-version: '8.14.3' 91 92 - name: Run Gradle Test 93 run: ./gradlew test --tests='com.example.api.${{ inputs.domain }}.*' 94 ``` 95 96 As an example, you may have a gradle project and a CI you want run gradle tests on. Instead of running `./gradlew test` and having your whole suite of tests run asynchronously top-to-bottom, we can leverage a reusable workflow to run only a subset of tests based on domain. 97 98 ## Domain Matrix 99 100 ```yaml 101 # .github/workflows/feature 102 103 jobs: 104 test: 105 uses: .github/workflows/test.yml 106 with: 107 domain: ${{ matrix.domain }} 108 strategy: 109 matrix: 110 domain: 111 - user 112 - order 113 - shipping 114 ``` 115 116 Now that we have a reusable workflow for running our tests, we can use a matrix strategy and run each domains set of tests in parallel of each other. 117 118 ## Enforcing Domain Test Coverage 119 120 ```python 121 #! /usr/bin/env python 122 123 import os 124 import yaml 125 126 def is_domain_covered(domain, test_domains): 127 for test_domain in test_domains: 128 if domain in test_domain: 129 return True 130 return False 131 132 if __name__ == '__main__': 133 with open('.github/workflows/feature.yml', 'r') as workflow_file: 134 workflow = yaml.load(workflow_file, Loader=yaml.FullLoader) 135 136 test_domains = workflow['jobs']['test']['strategy']['matrix']['domain'] 137 138 domains = os.listdir('com/example/src') 139 140 for domain in domains: 141 if not is_domain_covered(domain, test_domains): 142 raise Exception(f'The {domain} domain is not covered in tests') 143 ``` 144 145 What if we introduce a new domain to our project? Surely, we want enforce that all domains are covered when we run our tests via a matrix strategy. Here's a python example of a simple bash script we can use to parse the matrix strategy yaml array and verify it covers each domain directory of our project. 146 147 ```yaml 148 jobs: 149 domain_coverage: 150 steps: 151 - name: Checkout Repository 152 uses: actions/checkout@v4 153 with: 154 ref: ${{ github.head_ref }} 155 156 - name: Verify Domain Test Coverage 157 run: ./scripts/test/domain_test_coverage.sh 158 159 test: 160 needs: [ domain_coverage ] 161 uses: .github/workflows/unit-test.yml 162 with: 163 domain: ${{ matrix.domain }} 164 strategy: 165 matrix: 166 domain: 167 - user 168 - order 169 - shipping 170 ``` 171 172 Now we can run this bash script before the matrix strategy job runs, to verify that all domains are covered with any incoming changes to our project. 173 174 ## Omitting Irrelevant Domains Per Feature 175 176 ```yaml 177 on: 178 workflow_call: 179 inputs: 180 domain: 181 required: true 182 type: string 183 184 jobs: 185 unit_test: 186 runs-on: depot-ubuntu-22.04 187 steps: 188 - name: Checkout Repository 189 uses: actions/checkout@v4 190 with: 191 ref: ${{ github.head_ref }} 192 193 - name: Determine If Test Is Needed 194 id: test_needed 195 run: | 196 git fetch origin main; 197 198 INPUT_DOMAIN=$(echo '${{ inputs.domain }}' | cut -f 1 -d '.' -) 199 200 if [ $(git diff origin/main --name-only | grep $INPUT_DOMAIN | wc | awk '{ print $1 }') -ne 0 ]; then 201 echo "is-test-needed=true" >> "$GITHUB_OUTPUT"; 202 else 203 # Run all tests when merged to main 204 if [ $(git branch --show-current) == 'main' ]; then 205 echo "is-test-needed=true" >> "$GITHUB_OUTPUT"; 206 else 207 echo "is-test-needed=false" >> "$GITHUB_OUTPUT"; 208 fi 209 fi 210 211 - name: Setup Java 21 212 if: steps.test_needed.outputs.is-test-needed == 'true' 213 uses: actions/setup-java@v4.5.0 214 with: 215 distribution: 'corretto' 216 java-version: '21' 217 218 - name: Setup Gradle 219 if: steps.test_needed.outputs.is-test-needed == 'true' 220 uses: gradle/actions/setup-gradle@v4 221 with: 222 gradle-version: '8.14.3' 223 224 - name: Run Gradle Test 225 if: steps.test_needed.outputs.is-test-needed == 'true' 226 run: ./gradlew test --tests='com.example.api.${{ inputs.domain }}.*' 227 ``` 228 229 Now that we're running tests based on domain, we can conditionally run tests for features that might not impact other domains. By checking the files included in a `git diff`, we can check if the `inputs.domain` is included with the files being changed, and only then run tests as needed. 230 231 This is a great way to reduce billable CI minutes. 232 233 **Note:** You still will likely want to run all tests after the feature is merged. 234 235 ## Benefits 236 237 ```bash 238 $ gradle test --parallel 239 ``` 240 241 Given the gradle example earlier, build tools might have parallel testing mechanisms like [`--parallel`](https://docs.gradle.org/current/userguide/performance.html#sec:enable_parallel_execution). These very well may work out of the box for different projects, but some issues I've encountered in the past when leveraging these mechanisms are: 242 - External dependencies be constrained to run the tests in parallel 243 - Threads used for tests constrained by the amount of CPU cores the runners have 244 245 When parallelizing tests by project domain, here are some of the benefits that these changes yield: 246 247 - Physical isolation of resources and domains per set of tests, reducing resource constraints like database connections 248 - Run more tests in parallel, reducing the time it takes for developers to get feedback on their features 249 - Less tests ran on a per feature basis, reducing billable CI minutes