name: 🚀 Deploy
on:
  push:
    branches:
      - main
  pull_request: {}

jobs:
  deploy:
    name: 🚀 Deploy
    runs-on: self-hosted
    # only build/deploy main branch on pushes
    if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }}

    steps:
      - name: ⬇️  Checkout repo
        uses: actions/checkout@v2

      - name: 🔐 Apply secrets env
        run: |
          echo 'PORT=5001' > .env
          echo 'DATABASE_URL="file:./prod.db"' >> .env
          echo 'SESSION_SECRET="${{secrets.SESSION_SECRET}}"' >> .env

      - name: ⚒ Install and build
        run: |
          yarn install
          PORT=5001 yarn build
          yarn prisma db push

      - name: 🚀 Deploy
        run: |
          chown -R plone:plone .
          su plone && pm2 startOrRestart ecosystem.config.js
