-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
166 additions
and
140 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
name: Deploy | ||
on: | ||
push: | ||
branches: | ||
- main | ||
schedule: | ||
- cron: "0 4,16 * * *" | ||
workflow_dispatch: | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
@@ -40,9 +38,29 @@ jobs: | |
path: workers-site/node_modules | ||
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('workers-site/package-lock.json') }} | ||
- run: npm i # 执行 Blogroll 的依赖安装 | ||
- run: npm run gen # 相当于 node index.js,生成 opml.xml,opml.json 和 data.json | ||
- run: npm run update # 相当于 node update_files.js,从 Seatable 更新数据到 README.md | ||
env: | ||
SEATABLE_API_TOKEN: ${{ secrets.SEATABLE_API_TOKEN }} | ||
- name: Commit and push if README.md changed | ||
env: | ||
DEPLOY_REPO: [email protected]:nju-lug/blogroll.git | ||
DEPLOY_BRANCH: main | ||
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | ||
run: | | ||
if [ -n "$(git status --porcelain README.md)" ]; then | ||
git config --local user.email "github-actions[bot]@users.noreply.github.com" | ||
git config --local user.name "github-actions[bot]" | ||
git add README.md | ||
git commit -m "update README.md from github actions" | ||
mkdir -p ~/.ssh | ||
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa | ||
chmod 600 ~/.ssh/id_rsa | ||
ssh-keyscan github.com >> ~/.ssh/known_hosts | ||
git push $DEPLOY_REPO HEAD:$DEPLOY_BRANCH | ||
else | ||
echo "No changes detected" | ||
fi | ||
- run: npm run gen # 相当于 node index.js,生成 opml.xml,opml.json 和 data.json | ||
- run: npm run build | ||
- name: Deploy to Cloudflare Workers | ||
uses: cloudflare/wrangler-action@v3 | ||
|
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 文件读取包 | ||
const fs = require('fs'); | ||
// 引入 SeaTableAPI | ||
const { Base } = require('seatable-api'); | ||
// exit函数 | ||
const { exit } = require('process'); | ||
|
||
// 相关配置 | ||
const seatableToken = process.env.SEATABLE_API_TOKEN; | ||
const readmeMdPath = './README.md'; | ||
// 读取 README.md | ||
const readmeMdContent = fs.readFileSync(readmeMdPath, { encoding: 'utf-8' }); | ||
|
||
// 解析 SeaTable 中的表格,转为 JSON | ||
async function parseSeaTableToJson() { | ||
const seatableBase = new Base({ | ||
server: "https://table.nju.edu.cn", | ||
APIToken: seatableToken | ||
}); | ||
try { | ||
await seatableBase.auth(); | ||
} catch (err) { | ||
console.log('Seatable API Token 无效,请检查环境变量 SEATABLE_API_TOKEN 是否正确设置。'); | ||
exit(1); | ||
} | ||
const tables = await seatableBase.getTables(); | ||
const rows = await seatableBase.listRows(tables[0]['name']); | ||
const rows_reverse = rows.reverse(); | ||
var opmlJson = []; | ||
rows_reverse.forEach(row => { | ||
// 根据Name去重并保留最后一个 | ||
if (row['Name'] && !(opmlJson.find((item) => item.title === row['Name']))) { | ||
if (opmlJson.find((item) => item.htmlUrl === row['HTML'])) { | ||
return; | ||
} | ||
opmlJson.push({ | ||
title: row['Name'], | ||
xmlUrl: row['RSS'], | ||
htmlUrl: row['HTML'] | ||
}); | ||
} | ||
}); | ||
return opmlJson.reverse(); | ||
} | ||
|
||
|
||
(async () => { | ||
// 从 SeaTable 中读取数据 | ||
const opmlJson = await parseSeaTableToJson(); | ||
|
||
// 更新 README.md 中的表格内容 | ||
const tableStart = readmeMdContent.indexOf('| -- | -- | -- |') + 22; | ||
const tableEnd = readmeMdContent.indexOf('## OPML') - 2; | ||
const tableContent = opmlJson.map((lineJson) => `| ${lineJson.title} | ${lineJson.xmlUrl} | ${lineJson.htmlUrl} |`).join('\n') + '\n'; | ||
const newReadmeMdContent = readmeMdContent.slice(0, tableStart) + tableContent + readmeMdContent.slice(tableEnd); | ||
fs.writeFileSync(readmeMdPath, newReadmeMdContent, { encoding: 'utf-8' }); | ||
})(); |