name: Deploy Pages
run-name: ${{ gitea.event.head_commit.message }}

on:
  push:
    tags:
      - v*
      - milestone*

jobs:
  Generate-Pages:
    runs-on: ubuntu-latest

    permissions:
      contents: write
      
    steps:
      - name: Send mail
        uses: https://github.com/dawidd6/action-send-mail@v3
        with:
          server_address: smtp.qq.com
          server_port: 465
          secure: true
          username: "${{ secrets.QQ_SMTP_USERNAME }}"
          password: "${{ secrets.QQ_SMTP_TOKEN }}"
          subject: 博客部署启动
          to: 3145078758@qq.com
          from: Gitea Actions
          body: 博客自动化部署开始执行, 前往 ${{ gitea.server_url }}/Linloir/blog/actions/runs/${{ gitea.run_id }} 查看执行日志
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '16'
      - name: Install dependencies
        run: npm install
      - name: Install hexo-cli
        run: npm install -g hexo-cli
      - name: Generate pages
        run: hexo generate
      - name: Copy pages
        run: cp -r public ../pages
      - name: Checkout to `publish` branch
        run: |
          git fetch
          git checkout publish
      - name: Remove old files
        run: rm -rf `ls | grep -v .git`
      - name: Copy new files
        run: cp -r ../pages/* .
      - name: Commit and Push back Changes
        uses: https://github.com/stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: "Auto deploy pages"
          commit_user_name: "Linloir"
          commit_user_email: "3145078758@qq.com"
          commit_author: "Linloir <3145078758@qq.com>"
          branch: publish
          commit_options: "--no-verify"
          token: "${{ secrets.TOKEN }}"
      - name: Call API to Update Caddy
        run: curl https://upd.linloir.cn/update/blog.linloir.gitea.linloir.cn
      - name: Send mail On Success
        if: success()
        uses: https://github.com/dawidd6/action-send-mail@v3
        with:
          server_address: smtp.qq.com
          server_port: 465
          secure: true
          username: "${{ secrets.QQ_SMTP_USERNAME }}"
          password: "${{ secrets.QQ_SMTP_TOKEN }}"
          subject: 博客部署成功
          to: 3145078758@qq.com
          from: Gitea Actions
          body: 博客自动化部署执行成功, 前往 https://blog.linloir.cn 查看
      - name: Send mail On Fail
        if: failure()
        uses: https://github.com/dawidd6/action-send-mail@v3
        with:
          server_address: smtp.qq.com
          server_port: 465
          secure: true
          username: "${{ secrets.QQ_SMTP_USERNAME }}"
          password: "${{ secrets.QQ_SMTP_TOKEN }}"
          subject: 博客部署失败
          to: 3145078758@qq.com
          from: Gitea Actions
          body: 博客自动化部署执行失败, 前往 ${{ gitea.server_url }}/Linloir/blog/actions/runs/${{ gitea.run_id }} 查看执行日志
