name: Deploy to production
on:
  release:
    types: [released]

  # to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  update-env:
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: "20"
          cache: "npm"
      - run: npm install ci
      - name: "Update DOTENV_LOCAL in prod"
        env:
          HF_TOKEN: ${{ secrets.HF_TOKEN }}
          SERPER_API_KEY: ${{ secrets.SERPER_API_KEY }}
          OPENID_CONFIG: ${{ secrets.OPENID_CONFIG }}
          MONGODB_URL: ${{ secrets.MONGODB_URL }}
          HF_DEPLOYMENT_TOKEN: ${{ secrets.HF_DEPLOYMENT_TOKEN }}
        run: npm run updateProdEnv
  sync-to-hub:
    runs-on: ubuntu-latest
    steps:
      - name: Check large files
        uses: ActionsDesk/lfs-warning@v2.0
        with:
          filesizelimit: 10485760 # this is 10MB so we can sync to HF Spaces
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
          lfs: true
      - name: Push to hub
        env:
          HF_DEPLOYMENT_TOKEN: ${{ secrets.HF_DEPLOYMENT_TOKEN }}
        run: git push https://nsarrazin:$HF_DEPLOYMENT_TOKEN@huggingface.co/spaces/huggingchat/chat-ui main