name: Deploy to production
concurrency: production

on:
  push:
    branches: [master]
  workflow_dispatch:

jobs:
  deployment:
    runs-on: ubuntu-latest
    environment: production
    timeout-minutes: 30
  
    steps:
    - name: SSH Remote Commands
      uses: appleboy/ssh-action@v1.1.0
      with:
        # Proxy
        proxy_host : ${{secrets.PROXY_HOST}}
        proxy_port : ${{secrets.PROXY_PORT}}
        proxy_username : ${{secrets.PROXY_USER}}
        proxy_passphrase: ${{secrets.PROXY_PASSPHRASE}}
        proxy_key: ${{secrets.PROXY_KEY}}
        
        # Serveur web
        host: ${{secrets.HOST}}
        port : ${{secrets.PORT}}
        username : ${{secrets.USER}}
        key: ${{secrets.KEY}}

        script_stop: true

        # See https://github.com/ae-utbm/sith/wiki/GitHub-Actions#deployment-action
        script: |
          cd ${{secrets.SITH_PATH}}

          git fetch
          git reset --hard origin/master
          poetry install --with prod --without docs,tests
          npm install
          poetry run ./manage.py install_xapian
          poetry run ./manage.py migrate
          poetry run ./manage.py collectstatic --clear --noinput
          poetry run ./manage.py compilemessages

          sudo systemctl restart uwsgi
  
  sentry:
    runs-on: ubuntu-latest
    environment: production
    timeout-minutes: 30
    needs: deployment
    steps:
      - uses: actions/checkout@v4

      - name: Sentry Release
        uses: getsentry/action-release@v1.7.0
        env:
          SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
          SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
          SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
          SENTRY_URL: ${{ secrets.SENTRY_URL }}
        with:
          environment: production