Postgres and Redis container services for e2e tests in Github actions
September 8, 2021For e2e testing we need database connection, provisioning container service for Postgres database can be automated using Github actions. Environment variable for connection string for newly created database can be set in the step for running e2e tests. The same goes for Redis instance.
# ...jobs:build:# Container must run in Linux-based operating systemsruns-on: ubuntu-latest# Image from Docker hubcontainer: node:16.3.0-alpine3.13# ...strategy:matrix:# ...database-name:- e2e-testing-dbdatabase-user:- usernamedatabase-password:- passworddatabase-host:- postgresdatabase-port:- 5432redis-host:- redisredis-port:- 6379services:postgres:image: postgres:latestenv:POSTGRES_DB: ${{ matrix.database-name }}POSTGRES_USER: ${{ matrix.database-user }}POSTGRES_PASSWORD: ${{ matrix.database-password }}ports:- ${{ matrix.database-port }}:${{ matrix.database-port }}# Set health checks to wait until postgres has startedoptions:--health-cmd pg_isready--health-interval 10s--health-timeout 5s--health-retries 5redis:image: redis# Set health checks to wait until redis has startedoptions: >---health-cmd "redis-cli ping"--health-interval 10s--health-timeout 5s--health-retries 5steps:# ...- run: npm run test:e2eenv:DATABASE_URL: postgres://${{ matrix.database-user }}:${{ matrix.database-password }}@${{ matrix.database-host }}:${{ matrix.database-port }}/${{ matrix.database-name }}REDIS_URL: redis://${{ matrix.redis-host }}:${{ matrix.redis-port }}# ...