diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml index cf1e8c65..eb1dcd3e 100644 --- a/.github/workflows/gh-pages.yml +++ b/.github/workflows/gh-pages.yml @@ -4,6 +4,10 @@ on: push: branches: - main + workflow_run: + workflows: ["Monthly Script Runner"] + types: + - completed jobs: build-deploy: diff --git a/.github/workflows/sync-ranking-data-by-month.yaml b/.github/workflows/sync-ranking-data-by-month.yaml new file mode 100644 index 00000000..c4f7c14d --- /dev/null +++ b/.github/workflows/sync-ranking-data-by-month.yaml @@ -0,0 +1,67 @@ +name: Monthly Script Runner + +# 触发条件:每月的1号运行 +on: + schedule: + - cron: "0 0 0 * *" # 每月一号的00:00 UTC时间运行 + workflow_dispatch: + +jobs: + run-scripts: + runs-on: ubuntu-latest + + steps: + # 检出代码 + - name: Checkout repository + uses: actions/checkout@v4 + + # 设置 Node.js 环境 + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' # 使用 Node.js 18 + + # 运行第一个脚本 sync_xlab.js,从接口获取最新数据 + - name: Run sync_xlab.js + run: node script/sync_xlab.js + + # 运行第二个脚本 update_year_user.js, 更新 rankingList.json + - name: Run update_year_user.js + run: node script/update_year_user.js + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + # 运行第三个脚本 front-matter.js, 批量创建详情页 + - name: Run front-matter.js + run: node script/front-matter.js + + # 提交并推送更改到 main 分支 + - name: Commit and push changes + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git add . + git commit -m "chore: Automated data update from scripts" + git push origin main + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + # # 创建并推送 PR + # - name: Create Pull Request + # id: create_pr + # uses: peter-evans/create-pull-request@v7 + # with: + # commit-message: "chore: Updating data from xlab" + # title: "chore: Updating data from xlab" + # body: "Updating data from xlab,and create user homepages" + # branch: update-ranking-data + # base: "main" + # delete-branch: true + + # # 启用自动合并 + # - name: Enable auto-merge + # uses: peter-evans/enable-pull-request-automerge@v3 + # with: + # token: ${{ secrets.GITHUB_TOKEN }} + # pull-request-number: ${{ steps.create_pr.outputs.pull-request-number }} + # merge-method: merge \ No newline at end of file diff --git a/script/front-matter.js b/script/front-matter.js index 0cf09d84..568f2e1c 100644 --- a/script/front-matter.js +++ b/script/front-matter.js @@ -67,7 +67,8 @@ async function updateFrontMatter(login, item) { } // 主函数 -async function main(year) { +async function main() { + const year = new Date().getFullYear(); try { const rankingsData = await readRankingData(year); @@ -90,4 +91,4 @@ async function main(year) { } // 执行主程序 -main(2024); +main(); diff --git a/script/sync_xlab.js b/script/sync_xlab.js index 177c9e3c..e1bdb751 100644 --- a/script/sync_xlab.js +++ b/script/sync_xlab.js @@ -40,7 +40,9 @@ async function writeRankingData(data) { } // 主函数 -async function main(year) { +async function main() { + const year = new Date().getFullYear(); + console.log(year); const rankingData = await readRankingData(); const xlabData = await fetchXlab(year); @@ -49,7 +51,7 @@ async function main(year) { return; } - const yearIndex = rankingData.findIndex(v => v.year === year); + const yearIndex = rankingData.findIndex(v => Number(v.year) === year); // 获取当前时间戳 const update = `${new Date().getFullYear()} 年 ${new Date().getMonth() + 1} 月`; const ranking = { @@ -58,7 +60,7 @@ async function main(year) { annualRanking: xlabData, }; - if (yearIndex > 0) { + if (yearIndex > -1) { rankingData[yearIndex] = ranking; } else { // 插入新的年份数据 @@ -71,4 +73,4 @@ async function main(year) { await writeRankingData(rankingData); } -main(2024); +main(); diff --git a/script/update_year_user.js b/script/update_year_user.js index 2e50de04..3c0d51ff 100644 --- a/script/update_year_user.js +++ b/script/update_year_user.js @@ -9,7 +9,7 @@ const fs = require('fs').promises; const path = require('path'); // 运行此脚本 需要配置 github_token 否则会 API 拉取会被 github 限制 -const GITHUB_TOKEN = ''; // 替换为你的 GitHub 个人访问令牌 +const GITHUB_TOKEN = process.env.GITHUB_TOKEN; // 替换为你的 GitHub 个人访问令牌 // 防止速度过快被 github 限制 const TIME_DELAY = 600; @@ -96,7 +96,8 @@ async function enrichRankingData(data) { } // 主函数 -async function main(year) { +async function main() { + const year = new Date().getFullYear(); try { const rankingsData = await readRankingData(); const yearData = rankingsData.find(yearData => yearData.year === year); @@ -114,4 +115,4 @@ async function main(year) { } } -main(2024); +main();