image: node:12.16.3

stages:
  - test
  - build
  - deploy

variables:
  MONGODB_VERSION: '4.2.6'
  MONGODB_URL: 'mongodb://mongodb:27017/test'
  PORT: '3002'
  JWT_SECRET: 'test'

# http://docs.gitlab.com/ee/ci/yaml/README.html#cache
cache:
  paths:
    - app/client/node_modules/
    - app/server/node_modules/

test_client:
  stage: test
  script:
    - cd app/client
    - npm install
    - npm run test
  except:
    - master # only for now to speed up dev
    
test_server:
  stage: test
  # Info: http://docs.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-a-service
  services:
    - name: mongo:$MONGODB_VERSION
      alias: mongodb
  script:
    - cd app/server
    - npm install
    - npm run test
  except:
    - master # only for now to speed up dev

build_client:
  before_script: docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  variables: 
    CLIENT_IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
    CLIENT_RELEASE_IMAGE_TAG: $CI_REGISTRY_IMAGE:latest

  image: docker:19.03.12
  services:
    - docker:19.03.12-dind
  stage: build
  script:
    - docker build -f app/client/Dockerfile -t $IMAGE_TAG .
    - docker tag $CLIENT_IMAGE_TAG $CLIENT_RELEASE_IMAGE_TAG
    - docker push $CLIENT_RELEASE_IMAGE_TAG
  only: 
    - master
