name: Deploy to GitHub Pages

on:
  workflow_dispatch: {}
  push:
    branches:
      - main
    paths-ignore:
      - 'README.md'
      - 'LICENSE'
      - '.github/**'
      - '.gitignore'
      - '.git/**'
      - '.idea/**'
      - '.vscode/**'
  pull_request:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    permissions:
      contents: write
      pages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./
          cname: dotfiles.gh.yycc.dev
          publish_branch: gh-pages
          enable_jekyll: true
          keep_files: false
          force_orphan: true
          commit_message: ${{ github.event.head_commit.message }}

      - name: Clear jsdelivr CDN cache
        run: |
          pure_path=(
            ""
            "shells/oh-my-zsh/custom/aliases/"
            "shells/oh-my-zsh/custom/plugins/"
            "shells/oh-my-zsh/custom/themes/"
            "docs"
            "system"
          )
          for path in "${pure_path[@]}"; do
            curl "https://purge.jsdelivr.net/gh/${{ github.repository }}@${{ github.ref_name || github.head_ref }}/$path" \
            && echo "Cache cleared for $path successfully." || echo "Failed to clear cache for $path."
          done

      - name: Send Apprise Notification
        run: |
          curl -X POST \
          -F "tag=p" \
          -F "body=🚀 ${{ github.event.repository.name }} Pages 部署成功 #github

          📎 相关链接
          • [GitHub Pages](https://dotfiles.gh.yycc.dev)
          • [仓库地址](https://github.com/${{ github.repository }})" \
          "${{ secrets.APPRISE_HTTP_URL }}"
