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

# 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

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

stages:
  - build
  - test
  - deploy

### Jobs ###

# Install dependencies, compile the bundle.js,
# make migrations on and seed the db
build:
  stage: build
  script:
    - npm install
    - npm run build
    - knex migrate:latest
    - knex seed:run
  artifacts:
    paths:
      - ldap_connexion_config.json
      - build/
      - node_modules/
    expire_in: 240 min
  tags:
    - database
    - build

# Run ESLint to analyse our JS code
test:lint:
  stage: test
  script:
    - npm run lint
  tags:
    - lint

deploy_staging:
  stage: deploy
  only:
    - master
  before_script:
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$STAGING_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
  script:
    - npm run build
    - ssh -p22 sigma-dev@roued "mkdir -p /opt/sigma-back-dev"
    - ssh -p22 sigma-dev@roued "rm -rf /opt/sigma-back-dev/build"
    - scp -p22 -r build/ sigma-dev@roued:/opt/sigma-back-dev/build
  environment:
    name: staging
    url: http://129.104.210.10:3000
  tags:
    - deployment