Daily Report & Deploy #231
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 | |
| # Job 4: 发送钉钉通知 | |
| send-dingtalk: | |
| runs-on: ubuntu-latest | |
| needs: [deploy] | |
| steps: | |
| - name: Checkout repository | |
| 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: Find latest report | |
| id: find_report | |
| run: | | |
| DATE=${{ steps.date.outputs.date }} | |
| YEAR=${{ steps.date.outputs.year }} | |
| MONTH=${{ steps.date.outputs.month }} | |
| # 从 data 目录查找今天的 JSON 报告 | |
| report_file="data/$YEAR/$MONTH/$DATE.json" | |
| if [ -f "$report_file" ]; then | |
| echo "report_file=$report_file" >> $GITHUB_OUTPUT | |
| echo "找到今日报告: $report_file" | |
| else | |
| # 如果没有今天的报告,查找最新的报告 | |
| latest_file=$(find data -type f -name "*.json" | sort -r | head -n 1) | |
| if [ -z "$latest_file" ]; then | |
| echo "未找到报告文件" | |
| exit 1 | |
| fi | |
| echo "report_file=$latest_file" >> $GITHUB_OUTPUT | |
| echo "使用最新报告: $latest_file" | |
| fi | |
| - name: Send to DingTalk | |
| env: | |
| DINGDING_TOKEN: ${{ secrets.DINGDING_TOKEN }} | |
| REPORT_FILE: ${{ steps.find_report.outputs.report_file }} | |
| DATE: ${{ steps.date.outputs.date }} | |
| SITE_URL: "https://wayyoungboy.github.io/github-trending-reporter/" | |
| REPO_URL: "https://github.com/${{ github.repository }}" | |
| run: | | |
| # 如果没有配置 token 就安全地跳过(避免在 workflow 校验阶段引用 secrets 导致错误) | |
| if [ -z "$DINGDING_TOKEN" ]; then | |
| echo "DINGDING_TOKEN 未配置,跳过发送" | |
| exit 0 | |
| fi | |
| python3 - <<'PY' | |
| import json, os | |
| report_file = os.environ['REPORT_FILE'] | |
| date = os.environ['DATE'] | |
| site_url = os.environ['SITE_URL'] | |
| repo_url = os.environ['REPO_URL'] | |
| # 读取 JSON 数据 | |
| with open(report_file, 'r', encoding='utf-8') as f: | |
| data = json.load(f) | |
| print(json.dumps(data, ensure_ascii=False, indent=2)) | |
| # 构建简洁的 Markdown 消息 | |
| content = "" | |
| emojis = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟", "1️⃣1️⃣", "1️⃣2️⃣", "1️⃣3️⃣", "1️⃣4️⃣", "1️⃣5️⃣"] | |
| for i, repo in enumerate(data['repositories'], 1): | |
| emoji = emojis[i-1] if i <= len(emojis) else f"#{i}" | |
| content += f"### {emoji} **{repo['full_name']}**\n" | |
| content += f"- **语言**: {repo['language']}\n" | |
| content += f"- **增长**: +{repo['stars_today']} ⭐ (今日)\n" | |
| content += f"- **总计**: {repo['stars']} ⭐\n" | |
| content += f"- **简介**: {repo.get('llm_summary') or repo['description']}\n\n" | |
| # 构建消息(Markdown) | |
| message = { | |
| 'msgtype': 'markdown', | |
| 'markdown': { | |
| 'title': f'每日GitHub趋势报告 - {date}', | |
| 'text': ( | |
| f'### 📈 GitHub趋势每日报告\n\n' | |
| f'**📅 日期**: {date}\n' | |
| f'**📊 项目总数**: {data["total_repos"]}个\n' | |
| f'**🔥 最热门**: {data["repositories"][0]["full_name"]} (+{data["repositories"][0]["stars_today"]}⭐)\n\n' | |
| f'**📱 今日热门项目**: \n{content}\n' | |
| f'**[查看完整报告]({site_url})** | **[项目仓库]({repo_url})**' | |
| ) | |
| } | |
| } | |
| print(json.dumps(message, ensure_ascii=False, indent=2)) | |
| open('dingtalk_message.json', 'w', encoding='utf-8').write(json.dumps(message, ensure_ascii=False)) | |
| PY | |
| curl "https://oapi.dingtalk.com/robot/send?access_token=$DINGDING_TOKEN" \ | |
| -H 'Content-Type: application/json' \ | |
| -d @dingtalk_message.json |