image: node:latest

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/

stages:
  - build
  - deploy

build:
  stage: build
  script:
    - npm install
    - npm run build
  artifacts:
    paths:
      - out/
  only:
    - main

pages:
  stage: deploy
  environment: production
  before_script:
    - apt-get update -y
    - apt-get -y install rsync
    - rsync --version
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_HOST_KEY" > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    - mkdir -p public
    - mv out public
    - |
      rsync --verbose --recursive --times --delete --progress --human-readable \
      -e 'ssh -p "'"$SSH_PORT"'"' \
      public/ \
      $SSH_USER@$SSH_HOST_IP:$SSH_TARGET_DIR
  artifacts:
    paths:
      - public