name: Generate packages.element.io directory indexes
on:
  # Trigger a rebuild of all indexes if the template gets updated
  push:
    branches: [ develop ]
    paths:
      - 'packages.element.io/**'
  # Trigger a daily rebuild for nightlies
  schedule:
    - cron: '0 11 * * *'
  # Manual trigger for rebuilding for releases
  workflow_dispatch: { }
jobs:
  deploy:
    name: "Deploy"
    runs-on: ubuntu-latest
    environment: develop
    env:
      R2_BUCKET: 'packages-element-io'
      R2_URL: ${{ secrets.CF_R2_S3_API }}
    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          cache: "yarn"

      - name: Install Deps
        run: "yarn install --pure-lockfile"

      - name: Copy static files
        if: github.event_name == 'push'
        run: aws s3 cp --recursive packages.element.io/ s3://$R2_BUCKET/ --endpoint-url $R2_URL --region auto
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.CF_R2_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.CF_R2_TOKEN }}

      - name: Generate directory indexes
        run: scripts/generate-packages-index.ts
        env:
          CF_R2_S3_API: ${{ secrets.CF_R2_S3_API }}
          CF_R2_ACCESS_KEY_ID: ${{ secrets.CF_R2_ACCESS_KEY_ID }}
          CF_R2_TOKEN: ${{ secrets.CF_R2_TOKEN }}
