#image: node:20.17

stages:
#  - npm
#  - quality
#  - unit_test
  - deploy

#npm:
#  stage: npm
#  script:
#    - npm install
#  cache:
#    paths:
#      - node_modules/
#  artifacts:
#    expire_in: 2 hours
#    when: on_success
#    paths:
#      - node_modules/
#
#quality:
#  stage: quality
#  dependencies:
#    - npm
#  script:
#    - npm run lint
#
#unit_test:
#  stage: unit_test
#  dependencies:
#    - npm
#  script:
#    - npm run test

deploy:
  stage: deploy
#  dependencies:
#    - unit_test
  rules:
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $MAIN_BRANCH
  before_script:
    - 'command -v ssh-agent >/dev/null || ( apk add --update openssh )'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    - ls -al
    - echo $CI_PROJECT_DIR
    - ls -al
    - ssh $SSH_USER@$SSH_HOST "/bin/bash -s" < deploy.sh $WORK_DIR $MAIN_BRANCH
  after_script:
    - rm -rf ~/.ssh
