variables:
  BUILD_DIR: dist

stages:
  - test
  - build
  - review
  - deploy

tslint:
  stage: test
  tags:
    - nodejs
  allow_failure: false
  dependencies: []
  script:
    - echo "{}" > package.json
    - npm install tslint-angular
    - npm install tslint-eslint-rules
    - npm install @angular/compiler
    - npm install @angular/core
    - npm install typescript
    - npm install rxjs
    - node_modules/tslint/bin/tslint -p ./tsconfig.json -c ./tslint.json ./src/**/*.ts

ngbuild:
  stage: build
  tags:
    - angular
  allow_failure: false
  dependencies: []
  script:
    - npm install -g @angular/cli
    - npm install
    - ng build --prod --aot=false
  artifacts:
    paths:
      - "$BUILD_DIR"

#review:
#  stage: review
#  script:
#    - rsync -av --delete "$BUILD_DIR" /srv/nginx/pages/$CI_BUILD_REF_SLUG
#  environment:
#    name: review/$CI_BUILD_REF_NAME
#    url: http://$CI_BUILD_REF_SLUG.$SERVER_URL
#    on_stop: stop_review
#  only:
#    - branches
#  except:
#    - master
#  tags:
#    - nginx

#stop_review:
#  stage: review
#  script:
#    - rm -rf "$BUILD_DIR" /srv/nginx/pages/$CI_BUILD_REF_SLUG
#  variables:
#    GIT_STRATEGY: none
#  when: manual
#  environment:
#    name: review/$CI_BUILD_REF_NAME
#    action: stop
#  tags:
#    - nginx


deploy:
  stage: deploy
  only:
    - master
  tags:
    - ssh
  dependencies:
    - tslint
    - ngbuild
  script:
    - eval $(ssh-agent -s)
    - mkdir ~/.ssh #&& echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
    - ssh-keyscan "$SERVER_URL" >> ~/.ssh/known_hosts
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - scp -r "$BUILD_DIR"* "$SSH_URL"
