Skip to content

Update badge

Update badge #1205

Workflow file for this run

name: Deploy《LeetCode-Cookbook》
on:
push:
branches:
- master # 只在master上push触发部署
- add_hugo
paths-ignore: # 下列文件的变更不触发部署,可以自行添加
- README.md
- LICENSE
jobs:
deploy:
runs-on: ubuntu-latest # 使用ubuntu系统镜像运行自动化脚本
steps: # 自动化步骤
- uses: actions/checkout@v5 # 第一步,下载代码仓库
with:
fetch-depth: 0 # config.toml 里 enableGitInfo = true,需要完整 git 历史,否则取不到每页“最后修改”信息
- name: Setup Hugo # 第二步,安装 hugo
uses: peaceiris/actions-hugo@v3
with:
hugo-version: '0.74.3'
extended: true # 关键修复:主题用 resources.ToCSS 编译 book.scss,必须用 extended 版,否则 build 直接报错
- name: Build # 第三步,编译 hugo
run: |
cd ./website
hugo -D --minify
- name: Deploy to Server # 第四步:在 runner 上用 rsync 把构建产物推到服务器
# 关键修复:之前用 appleboy/ssh-action,它会把 script 放到“服务器上”执行,
# 于是 rsync 在服务器本机找不到 ./website/public/,还要再 SSH 回自己 → Permission denied。
# 正确做法是直接在 runner 上跑 rsync,用 DEPLOY_KEY 推到服务器。
env:
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
SERVER_PORT: ${{ secrets.SERVER_PORT }}
SERVER_DESTINATION: ${{ secrets.SERVER_DESTINATION }}
run: |
mkdir -p ~/.ssh
printf '%s\n' "$DEPLOY_KEY" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
# 服务器较旧(OpenSSH 5.x),只认旧的 ssh-rsa(SHA-1) 签名,
# 而新版 OpenSSH 默认禁用了它,所以要显式加回 PubkeyAcceptedAlgorithms=+ssh-rsa,
# 否则会报 "sign_and_send_pubkey: no mutual signature supported"。
rsync -avz --delete --exclude='*.pyc' \
-e "ssh -i ~/.ssh/deploy_key -p ${SERVER_PORT:-22} -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o PubkeyAcceptedAlgorithms=+ssh-rsa" \
./website/public/ \
"${SSH_USERNAME}@${SSH_HOST}:${SERVER_DESTINATION:-/var/www/books/leetcode/}"
word-count: # 重新统计总字数并把 README 里的徽章数字刷新、自动提交回仓库
# 只在 master 上跑(add_hugo 等分支不自动改 README)。
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
permissions:
contents: write # 需要写权限才能 commit & push 回仓库
steps:
- uses: actions/checkout@v5
- name: Compute total word count # 跑仓库自带脚本(标准库即可,ubuntu-latest 自带 python3)
id: wc
run: |
COUNT=$(python3 website/content/ChapterFour/pytool/WordCount.py | awk '/total word count/{print $NF}')
# 防御:脚本异常时不要用空值/非数字去覆盖徽章
if ! echo "$COUNT" | grep -qE '^[0-9]+$'; then
echo "WordCount.py 未返回有效数字:'$COUNT'"; exit 1
fi
echo "total word count = $COUNT"
echo "count=$COUNT" >> "$GITHUB_OUTPUT"
- name: Update badge & commit if changed
run: |
# 只替换 README 里 Total Word Count 徽章的数字部分
COUNT="${{ steps.wc.outputs.count }}"
sed -i -E "s#(Total%20Word%20Count-)[0-9]+(-success)#\1${COUNT}\2#" README.md
if git diff --quiet -- README.md; then
echo "徽章已是最新(${COUNT}),无需提交。"
else
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add README.md
# [skip ci]:这次只改 README 的自动提交不必再触发任何 workflow(deploy 已 paths-ignore,
# 但 test.yml 没有,所以统一用 skip ci 防止多余构建,也彻底杜绝循环触发)。
git commit -m "chore: update Total Word Count badge to ${COUNT} [skip ci]"
git push
fi