variables:
  LOCAL_DIR: public

stages: 
 - build
 - test_deployment
 - production_deployment

.sftp: &sftp_template
  image: ${CI_REGISTRY_IMAGE}/ubuntu_sftp
  tags:
    - wasenweiler.sit-servers.net
  script:
    - mkdir -p ~/.ssh
    - echo "${SFTP_KEY}" > ~/.ssh/id_rsa
    - echo "${SFTP_HOSTCERT}" > ~/.ssh/known_hosts
    - chmod 600 ~/.ssh/*
    - mv ${LOCAL_DIR} ${REMOTE_DIR}
    - sftp ${SFTP_USERNAME}@${SFTP_HOST} <<< "put -r ${REMOTE_DIR}"


brunch_build:
  stage: build
  image: node
  before_script:
    - npm i -g brunch
  script:
    - brunch b -p
  artifacts:
    paths:
      - public/*
    expire_in: 3 days

test_deploy: 
  stage: test_deployment
  dependencies:
    - brunch_build
  variables:
    SFTP_USERNAME: ${TEST_USERNAME}
    SFTP_KEY: ${TEST_KEY}
    SFTP_HOST: ${TEST_HOST}
    SFTP_HOSTCERT: ${TEST_HOSTCERT}
    REMOTE_DIR: ${TEST_DIR}
    GIT_STRATEGY: none

  environment:
    name: Preview
    url: http://tls-tracking.preview.sauerburger.com/

  <<: *sftp_template

production_deploy: 
  stage: production_deployment
  dependencies:
    - brunch_build
  variables:
    SFTP_USERNAME: ${PRODUCTION_USERNAME}
    SFTP_KEY: ${PRODUCTION_KEY}
    SFTP_HOST: ${PRODUCTION_HOST}
    SFTP_HOSTCERT: ${PRODUCTION_HOSTCERT}
    REMOTE_DIR: ${PRODUCTION_DIR}
    GIT_STRATEGY: none

  only:
    - master
  when: manual

  environment:
    name: Production
    url: http://tls-tracking.sauerburger.com/

  <<: *sftp_template

