GitHub Actions の matrix strategy でテストを並列実行する
GitHub Actions
インフラ
知識
運用
テストの速度改善のため、Github Actionsでdocker composeを使いつつテストを並列で実行する。
yml
jobs:
test:
runs-on: ubuntu-22.04
timeout-minutes: 15
strategy:
matrix:
shard: [1, 2, 3, 4]
fail-fast: false
steps:
~ 略 ~
- name: Setup database
run: docker compose up mysql -d
- name: Run tests (Shard ${{ matrix.shard }})
working-directory: ./hoge
env:
DATABASE_NAME: hoge
DATABASE_PORT: 3501
SHARD_ID: ${{ matrix.shard }}
SHARD_TOTAL: ${{ strategy.job-total }}
run: npm run test:shardpackage.jsonには以下を定義しておく
json
"test:shard": "jest --shard=$SHARD_ID/$SHARD_TOTAL",速度は1/4ほどに改善された。