# 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:
  - install
  - test
  - build
  - deploy

### Jobs ###

# Install dependencies, compile the bundle.js,
# make migrations on and seed the db
install-dependencies:
  stage: install
  script:
    - npm install -q
  artifacts:
    paths:
      - node_modules/
  tags:
    - build

build:
  stage: build
  script:
    - npm run build
    - knex migrate:latest
    - knex seed:run
  artifacts:
    paths:
      - build/
  tags:
    - build
    - database

deploy-staging:
  stage: deploy
  only:
    - stable
  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:
    - ssh -p22 sigma-dev@roued "mkdir -p /opt/sigma-back-dev"
    - ssh -p22 sigma-dev@roued "rm -rf /opt/sigma-back-dev/build"
    - ssh -p22 sigma-dev@roued "rm -rf /opt/sigma-back-dev/assets"
    - scp -p22 -r build/ sigma-dev@roued:/opt/sigma-back-dev/build
    - scp -p22 -r assets/ sigma-dev@roued:/opt/sigma-back-dev/assets
    - scp -p22 package.json "package-lock.json" ldap_config.json ldap_connexion_config.json sigma-back-dev.service sigma-dev@roued:/opt/sigma-back-dev/
  environment:
    name: staging
    url: http://129.104.210.10:3000
  tags:
    - deployment