# Official framework image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/node/tags/
image: node:10-stretch

# Only needed when using a docker container to run your tests in.
# Check out: http://docs.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-a-service
services:
  - postgres

variables:
  DOCKER_DRIVER: overlay2
  CI_APPLICATION_REPOSITORY: ${CI_REGISTRY_IMAGE}/${CI_COMMIT_REF_SLUG}
  CI_APPLICATION_TAG: ${CI_COMMIT_SHA}
  CI_CONTAINER_NAME: ci_job_build_${CI_JOB_ID}
  # Nécessaire pour accéder docker depuis le container du runner
  # cf. https://gitlab.com/gitlab-org/gitlab-runner/blob/master/docs/executors/kubernetes.md
  DOCKER_HOST: "tcp://localhost:2375"

# This folder is cached between builds
cache:
  paths:
  - node_modules/
  - ldap_credentials.json

stages:
  - build
  - docker

### Jobs ###

# Install dependencies, compile the bundle.js,
build:transpile:
  stage: build
  before_script:
    - export NODE_ENV=staging
  script:
    - npm ci 
    - npm run build
  artifacts:
    paths:
      - build/

docker:build:
  dependencies: 
    - build:transpile
  stage: docker
  image: docker:latest
  services:
    - docker:dind
  script:
    - echo $LDAP_CONN_CONFIG > ldap_credentials.json
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - echo "Building Dockerfile..."
    - docker build --pull -t ${CI_APPLICATION_REPOSITORY}:${CI_APPLICATION_TAG} -t ${CI_APPLICATION_REPOSITORY}:latest --build-arg DB_PASSWD=${DB_PASSWD} .
    - echo "Pushing to GitLab Container registry $CI_APPLICATION_REPOSITORY with tags '${CI_APPLICATION_TAG}' and 'latest'"
    - docker push ${CI_APPLICATION_REPOSITORY}:${CI_APPLICATION_TAG}
    - docker push ${CI_APPLICATION_REPOSITORY}:latest
  only:
    - master
    - stable