image: node:lts-alpine

services:
  - postgres:latest

variables:
  TEST_DB_NAME: test
  POSTGRES_DB: fahrtenbuch
  POSTGRES_HOST: postgres
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: postgres
  POSTGRES_HOST_AUTH_METHOD: trust
  POSTGRES_DRIVER: postgres
  SKIP_PREFLIGHT_CHECK: "true"
  INITIAL_COORDINATOR_EMAIL: "initial@fahrtenbuch.example"
  INITIAL_COORDINATOR_PASSWORD: "password"
  JWT_SECRET: "RANDOM_SECRET_HARD_TO_GUESS"


# These folders and files are cached between builds
cache:
  paths:
    - client/node_modules/
    - server/node_modules/
    - server/dist/

# Stages for better overview
stages:
  - prepare
  - build
  - test
  - cleanup

mirror_dockerfile_server:
  stage: prepare
  script:
    - cd server && npm install

mirror_dockerfile_client:
  stage: prepare
  script:
    - cd client && npm install

build_server:
  stage: build
  script:
    - cd server && npm run build

test_server:
  stage: test
  # I only run the server, since server tests are (almost) always against the API
  before_script:
    - cd server && npm run start &
  script:
    - cd server && npm run test

test_client:
  stage: test
  # If specific content needs to be tested, both the server and client need to be up and running
  before_script:
    - cd client && npm start &
    - cd server && npm start &
  script:
    - cd client && npm run test

drop_database:
  stage: cleanup
  image: postgres:latest
  script:
    - export PGPASSWORD=$POSTGRES_PASSWORD
    - psql -h postgres -U "$POSTGRES_USER" -c "DROP DATABASE IF EXISTS $POSTGRES_DB;"