Daily Report & Deploy #58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Daily Report & Deploy | |
| on: | |
| schedule: | |
| - cron: '0 0 * * *' # 每天 UTC 00:00 | |
| workflow_dispatch: | |
| push: | |
| branches: [main] | |
| permissions: | |
| contents: write | |
| pages: write | |
| id-token: write | |
| concurrency: | |
| group: pages | |
| cancel-in-progress: false | |
| env: | |
| PYTHON_VERSION: '3.11' | |
| NODE_VERSION: '20' | |
| jobs: | |
| # Job 1: 生成报告(仅定时或手动触发) | |
| generate-report: | |
| if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: 📅 Get date | |
| id: date | |
| run: | | |
| echo "date=$(date +%Y-%m-%d)" >> $GITHUB_OUTPUT | |
| echo "year=$(date +%Y)" >> $GITHUB_OUTPUT | |
| echo "month=$(date +%m)" >> $GITHUB_OUTPUT | |
| - name: 🐍 Setup Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ env.PYTHON_VERSION }} | |
| - name: 📦 Install Python dependencies | |
| run: pip install -r requirements.txt | |
| - name: 🚀 Generate report | |
| env: | |
| LLM_API_KEY: ${{ secrets.LLM_API_KEY }} | |
| LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }} | |
| LLM_MODEL: ${{ secrets.LLM_MODEL }} | |
| GITHUB_API_TOKEN: ${{ secrets.GITHUB_API_TOKEN }} | |
| run: python main.py --local --no-push | |
| - name: 📁 Move output to reports | |
| run: | | |
| DATE=${{ steps.date.outputs.date }} | |
| YEAR=${{ steps.date.outputs.year }} | |
| MONTH=${{ steps.date.outputs.month }} | |
| mkdir -p reports/$YEAR/$MONTH data/$YEAR/$MONTH | |
| cp output/$DATE.md reports/$YEAR/$MONTH/ 2>/dev/null || true | |
| cp output/$DATE.json data/$YEAR/$MONTH/ 2>/dev/null || true | |
| - name: 📤 Commit and push | |
| run: | | |
| git config user.name "wayyoungboy" | |
| # git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git config user.email "1017761807@qq.com" | |
| git add reports/ data/ | |
| git diff --staged --quiet || git commit -m "📈 Add report for ${{ steps.date.outputs.date }}" | |
| git push | |
| # Job 2: 构建部署网站 | |
| build-deploy: | |
| runs-on: ubuntu-latest | |
| needs: [generate-report] | |
| if: always() && (needs.generate-report.result == 'success' || needs.generate-report.result == 'skipped') | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| ref: main | |
| fetch-depth: 0 | |
| - name: 🔄 Pull latest changes | |
| run: | | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| git fetch origin main | |
| git pull origin main --rebase | |
| echo "✅ Latest changes pulled" | |
| echo "" | |
| echo "Latest 5 commits:" | |
| git log --oneline -5 | |
| - name: 🟢 Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: ${{ env.NODE_VERSION }} | |
| - name: 📦 Install dependencies | |
| run: npm install | |
| - name: 🔧 Generate sidebar | |
| run: npm run generate-sidebar | |
| - name: 📅 Find latest report date | |
| id: latest-report | |
| run: | | |
| echo "🔍 Searching for latest report..." | |
| echo "" | |
| echo "Available years:" | |
| ls reports/ | |
| echo "" | |
| # 查找 reports 目录下最新的报告日期(按文件名排序,因为文件名就是日期) | |
| LATEST_YEAR=$(ls reports/ | sort -r | head -1) | |
| echo "Latest year: $LATEST_YEAR" | |
| echo "" | |
| echo "Available months in $LATEST_YEAR:" | |
| ls reports/$LATEST_YEAR/ | |
| echo "" | |
| LATEST_MONTH=$(ls reports/$LATEST_YEAR/ | sort -r | head -1) | |
| echo "Latest month: $LATEST_MONTH" | |
| echo "" | |
| echo "Available reports in $LATEST_YEAR/$LATEST_MONTH:" | |
| ls reports/$LATEST_YEAR/$LATEST_MONTH/ | |
| echo "" | |
| # 按文件名(日期)降序排序,取第一个 | |
| LATEST_DATE=$(ls reports/$LATEST_YEAR/$LATEST_MONTH/ | sort -r | head -1 | sed 's/.md$//') | |
| echo "Latest date: $LATEST_DATE" | |
| echo "" | |
| echo "year=$LATEST_YEAR" >> $GITHUB_OUTPUT | |
| echo "month=$LATEST_MONTH" >> $GITHUB_OUTPUT | |
| echo "date=$LATEST_DATE" >> $GITHUB_OUTPUT | |
| echo "✅ Latest report found: $LATEST_YEAR/$LATEST_MONTH/$LATEST_DATE" | |
| - name: 🔧 Update index.js with latest report link | |
| run: | | |
| echo "🔧 Updating index.js with latest report link..." | |
| echo "" | |
| echo "Current link in index.js:" | |
| grep -A 1 "primaryButton" src/pages/index.js | grep "to=" | |
| echo "" | |
| LATEST_REPORT_PATH="/reports/${{ steps.latest-report.outputs.year }}/${{ steps.latest-report.outputs.month }}/${{ steps.latest-report.outputs.date }}" | |
| echo "📌 New link: $LATEST_REPORT_PATH" | |
| echo "" | |
| echo "Updating link..." | |
| # 替换 index.js 中的链接 | |
| sed -i "s|to=\"/reports/[^\"]*\"|to=\"$LATEST_REPORT_PATH\"|g" src/pages/index.js | |
| echo "✅ Link updated successfully!" | |
| echo "" | |
| echo "Updated link in index.js:" | |
| grep -A 1 "primaryButton" src/pages/index.js | grep "to=" | |
| echo "" | |
| - name: 🏗️ Build | |
| run: npm run build | |
| - name: 📤 Upload artifact | |
| uses: actions/upload-pages-artifact@v3 | |
| with: | |
| path: build | |
| # Job 3: 部署到 GitHub Pages | |
| deploy: | |
| environment: | |
| name: github-pages | |
| url: ${{ steps.deployment.outputs.page_url }} | |
| runs-on: ubuntu-latest | |
| needs: build-deploy | |
| steps: | |
| - name: 🚀 Deploy to GitHub Pages | |
| id: deployment | |
| uses: actions/deploy-pages@v4 |