name: Node.js CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to SSH
        uses: appleboy/ssh-action@v0.1.4
        with:
          host: ${{ secrets.SSH_HOST }}
          port: ${{ secrets.SSH_PORT }}
          username: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_KEY }}
          passphrase: ${{ secrets.SSH_PASSPHRASE }}
          script: |
            cd ${{ secrets.PATH }}
            git pull origin master -r artapi artapi-new
            cd artapi-new
            git fetch --all
            git reset --hard origin/master
            yarn install --only=master
            node ace build --production
            cp .env build/.env

            cd ..
            mv artapi artapi-old
            mv artapi-new artapi

            # Restart server

            cd artapi
            pm2 kill
            pm2 start build/server.js --name artapi
            rm -rf ../artapi-old
