name: CI

on:
  push:
    branches: master
  pull_request:
    branches: master
  schedule:
    # workwaround for cannot set timezone, cron will start on Asia/Shanghai 02:00
    - cron: "0 18 * * *"
  workflow_dispatch:
    inputs:
      deploy:
        description: "Whether to deploy to release branch"
        required: true
        default: "0"

env:
  TZ: Asia/Shanghai

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: true

      - name: Build
        run: ./generate.sh

      - name: Git push assets to "release" branch
        run: |
          cd dist
          git init
          git config user.name github-actions[bot]
          git config user.email 41898282+github-actions[bot]@users.noreply.github.com
          git checkout -b release
          git add .
          git commit -m "$(date '+%Y-%m-%d %H:%M:%S')"
          git remote add origin "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}"
          git push -f -u origin release

      - name: Purge jsdelivr CDN
        run: |
          cd dist
          for file in $(ls); do
            curl -i "https://purge.jsdelivr.net/gh/${{ github.repository }}@release/${file}"
          done
