Skip to content

Test API against Postgres Service Container #118

Test API against Postgres Service Container

Test API against Postgres Service Container #118

name: Test API against Postgres Service Container
on:
push:
branches:
- '*'
schedule:
- cron: '30 5 * * 2,4'
jobs:
install-test:
name: Node application tests
strategy:
fail-fast: false
matrix:
node: ['18', '20']
runson: ['ubuntu-20.04', 'ubuntu-22.04']
postgresversion: ['14.2-alpine']
runs-on: ${{ matrix.runson }}
# These run alongside the job.
# See:
# - https://docs.github.com/en/actions/using-containerized-services/about-service-containers
# - https://docs.github.com/en/actions/using-containerized-services/creating-postgresql-service-containers
services:
# label for service - since this job doesn't run in a container, this service is accessible at localhost
database:
image: postgres:${{ matrix.postgresversion }}
ports:
- 5432:5432
env:
POSTGRES_USER: acronymapi
POSTGRES_PASSWORD: password
POSTGRES_DB: defaultdb
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
# https://github.com/actions/checkout
- name: Checkout the commit triggering this job
uses: actions/checkout@v4
#
- name: Install Postgres Client
run: |
sudo apt-get update
sudo apt-get install --yes --no-install-recommends postgresql-client
#
- name: Initialize database with initdb
run: |
PGPASSWORD=password psql -h localhost -U acronymapi --dbname=defaultdb --file=initdb.sql
# https://github.com/actions/setup-node
# https://github.com/actions/cache is no longer needed with setup-node v2
- name: Setup Node with npm cache
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: 'npm'
#
- name: Install dependencies from package.json
run: npm install
- name: Run tests with mocks
run: npm run test || echo '::error file=package.json,title=npm::default target failed, ${{matrix.node}}'
- name: Run tests against live Postgres server
run: npm --test_live_server=true run test || echo '::error file=package.json,title=npm::test_live_server failed, ${{matrix.node}}'
env:
PGUSER: acronymapi
PGPASSWORD: password
PGDATABASE: defaultdb
PGHOST: localhost
PGPORT: 5432