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 (mac-mini built) Nightly builds
    schedule:
        - cron: "0 11 * * *"
  # Trigger after Nightly builds are deployed
    workflow_run:
        workflows: [ "Build and Deploy" ]
        types:
            - completed
    # Manual trigger for rebuilding for releases
    workflow_dispatch: {}
jobs:
    deploy:
        name: "Deploy"
        if: github.event != 'workflow_run' || github.event.workflow_run.conclusion == 'success'
        runs-on: ubuntu-latest
        environment: packages.element.io
        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 }}