generate-static-site-bk.yml
· 2.6 KiB · YAML
Bruto
# .github/workflows/generate-static-site.yml
name: Update Bing & Deploy to page
# 触发方式:
# - 手动触发(GitHub Actions 界面点击)
# - 每天 01:00 UTC(北京时间 09:00)
on:
workflow_dispatch: # 允许手动运行
schedule:
- cron: "0 1 * * *" # 每天 01:00 UTC
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: write # 必需:允许写入分支(Settings → Actions → Read and write permissions)
steps:
# 1. 检出 main 分支代码
- name: 🛠️ Checkout main branch
uses: actions/checkout@v4
# 2. 设置 Node.js 环境(v20)
- name: 🟩 Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
# 3. 运行 bing.js 获取最新 Bing 壁纸并生成 images.json
- name: 🖼️ Generate Bing images.json
run: node ./assets/js/bing.js
# 4. 准备静态资源目录(只包含要部署的文件)
- name: 📁 Prepare static directory
run: |
# 清理旧的 static 目录
rm -rf static
mkdir -p static/{assets/css,assets/fonts,assets/img,assets/js,assets/json}
# 复制指定文件(不包含 bing.js、其他图片等)
cp index.html static/
cp 404.html static/ || true # 404.html 可选
cp favicon.ico static/
cp apple-touch-icon.png static/
cp assets/css/*.css static/assets/css/
cp assets/fonts/* static/assets/fonts/
cp assets/img/logo.png static/assets/img/
cp assets/js/main.js static/assets/js/
cp assets/js/fireworks.js static/assets/js/
cp assets/json/images.json static/assets/json/
cp assets/readme.md static/
# 防止 GitHub Pages 启用 Jekyll 处理
touch static/.nojekyll
# 输出最终结构,便于调试
echo "📄 Final static structure:"
find static -type f | sort
# 5. 配置 Git 提交者信息
- name: 🧑💻 Configure git
run: |
git config --global user.email "actions@github.com"
git config --global user.name "GitHub Actions"
# 6. 部署到 page 分支
- name: 🚀 Commit and push to page
run: |
cd static
git init
git checkout -b page
git add .
# 使用 [skip ci] 防止 page 推送再次触发 CI
git commit -m "[skip ci] 🖼️ Auto-update Bing images and deploy"
git push --force "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" page
| 1 | # .github/workflows/generate-static-site.yml |
| 2 | name: Update Bing & Deploy to page |
| 3 | |
| 4 | # 触发方式: |
| 5 | # - 手动触发(GitHub Actions 界面点击) |
| 6 | # - 每天 01:00 UTC(北京时间 09:00) |
| 7 | on: |
| 8 | workflow_dispatch: # 允许手动运行 |
| 9 | schedule: |
| 10 | - cron: "0 1 * * *" # 每天 01:00 UTC |
| 11 | |
| 12 | jobs: |
| 13 | deploy: |
| 14 | runs-on: ubuntu-latest |
| 15 | permissions: |
| 16 | contents: write # 必需:允许写入分支(Settings → Actions → Read and write permissions) |
| 17 | |
| 18 | steps: |
| 19 | # 1. 检出 main 分支代码 |
| 20 | - name: 🛠️ Checkout main branch |
| 21 | uses: actions/checkout@v4 |
| 22 | |
| 23 | # 2. 设置 Node.js 环境(v20) |
| 24 | - name: 🟩 Setup Node.js |
| 25 | uses: actions/setup-node@v4 |
| 26 | with: |
| 27 | node-version: '20.x' |
| 28 | |
| 29 | # 3. 运行 bing.js 获取最新 Bing 壁纸并生成 images.json |
| 30 | - name: 🖼️ Generate Bing images.json |
| 31 | run: node ./assets/js/bing.js |
| 32 | |
| 33 | # 4. 准备静态资源目录(只包含要部署的文件) |
| 34 | - name: 📁 Prepare static directory |
| 35 | run: | |
| 36 | # 清理旧的 static 目录 |
| 37 | rm -rf static |
| 38 | mkdir -p static/{assets/css,assets/fonts,assets/img,assets/js,assets/json} |
| 39 | |
| 40 | # 复制指定文件(不包含 bing.js、其他图片等) |
| 41 | cp index.html static/ |
| 42 | cp 404.html static/ || true # 404.html 可选 |
| 43 | cp favicon.ico static/ |
| 44 | cp apple-touch-icon.png static/ |
| 45 | |
| 46 | cp assets/css/*.css static/assets/css/ |
| 47 | cp assets/fonts/* static/assets/fonts/ |
| 48 | cp assets/img/logo.png static/assets/img/ |
| 49 | cp assets/js/main.js static/assets/js/ |
| 50 | cp assets/js/fireworks.js static/assets/js/ |
| 51 | cp assets/json/images.json static/assets/json/ |
| 52 | cp assets/readme.md static/ |
| 53 | |
| 54 | # 防止 GitHub Pages 启用 Jekyll 处理 |
| 55 | touch static/.nojekyll |
| 56 | |
| 57 | # 输出最终结构,便于调试 |
| 58 | echo "📄 Final static structure:" |
| 59 | find static -type f | sort |
| 60 | |
| 61 | # 5. 配置 Git 提交者信息 |
| 62 | - name: 🧑💻 Configure git |
| 63 | run: | |
| 64 | git config --global user.email "actions@github.com" |
| 65 | git config --global user.name "GitHub Actions" |
| 66 | |
| 67 | # 6. 部署到 page 分支 |
| 68 | - name: 🚀 Commit and push to page |
| 69 | run: | |
| 70 | cd static |
| 71 | git init |
| 72 | git checkout -b page |
| 73 | git add . |
| 74 | # 使用 [skip ci] 防止 page 推送再次触发 CI |
| 75 | git commit -m "[skip ci] 🖼️ Auto-update Bing images and deploy" |
| 76 | git push --force "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" page |
sync-page-to-cnb.yml
· 3.3 KiB · YAML
Bruto
# .github/workflows/sync-page-to-cnb.yml
name: Sync Page Branch to CNB (Auto Deploy)
on:
push:
branches:
- page # 当 page 分支有新提交时自动触发(由每日壁纸生成脚本推送)
workflow_dispatch:
inputs:
reason:
description: '手动触发原因(如:测试部署或紧急更新)'
required: false
default: 'Manual run'
jobs:
sync:
runs-on: ubuntu-latest
steps:
# 🔍 1. 检出 page 分支内容
- name: 📥 Checkout page branch
uses: actions/checkout@v5
with:
ref: page
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0 # 获取完整历史记录,确保 git diff 能正常工作
# 🔍 2. 检测是否有实际变更(兼容首次提交)
- name: 🔎 Check for file changes
id: check
run: |
# 判断当前是否为首次提交(无父提交)
if [ -z "$(git rev-parse --verify HEAD^ 2>/dev/null)" ]; then
echo "🆕 First commit detected — assuming changes exist and proceeding."
echo "skip=false" >> $GITHUB_OUTPUT
else
# 获取上一次提交与当前提交之间的文件差异
CHANGES=$(git diff --name-only HEAD^ HEAD)
if [ -z "$CHANGES" ]; then
echo "✅ No changes detected since last commit. Skipping sync."
echo "skip=true" >> $GITHUB_OUTPUT
else
echo "✨ Changes detected:"
echo "$CHANGES"
echo "skip=false" >> $GITHUB_OUTPUT
fi
fi
# 🚀 3. 推送到 CNB(仅当有变更时执行)
- name: 🚀 Push to CNB & Trigger Deployment
if: steps.check.outputs.skip == 'false'
env:
CNB_USERNAME: ${{ secrets.CNB_USERNAME }}
CNB_TOKEN: ${{ secrets.CNB_TOKEN }}
run: |
echo "🧹 Cleaning old Git metadata..."
rm -rf .git
echo "🌱 Initializing new Git repository..."
git init
git config --global user.name 'GitHub Action'
git config --global user.email 'action@github.com'
echo "💾 Adding all static files to staging area..."
git add .
echo "📝 Committing with timestamp..."
# 使用 %% 转义 %,因为 YAML 中 % 是特殊字符
git commit -m "⚡ Sync from GitHub page branch: $(date +'%%Y-%%m-%%d %%H:%%M:%%S')"
echo "🔗 Setting remote to CNB repository..."
git remote add origin "https://${{ secrets.CNB_USERNAME }}:${{ secrets.CNB_TOKEN }}@cnb.cool/gyhwd.top/AhHui-Homepage.git"
echo "🚀 Forcing push to main branch on CNB (default deployment branch)..."
git branch -M main
git push --force --quiet origin main
echo "🎉 Successfully pushed to CNB: wojack/AhHui-Homepage"
# 👇 新增:发送事件触发服务器部署
- name: 🔔 Trigger server deployment
uses: actions/github-script@v7
with:
script: |
await github.rest.repos.createDispatchEvent({
owner: context.repo.owner,
repo: context.repo.repo,
event_type: 'deploy-to-server',
client_payload: {
message: 'CNB updated, triggering server sync',
commit: context.sha
}
})
| 1 | # .github/workflows/sync-page-to-cnb.yml |
| 2 | name: Sync Page Branch to CNB (Auto Deploy) |
| 3 | |
| 4 | on: |
| 5 | push: |
| 6 | branches: |
| 7 | - page # 当 page 分支有新提交时自动触发(由每日壁纸生成脚本推送) |
| 8 | workflow_dispatch: |
| 9 | inputs: |
| 10 | reason: |
| 11 | description: '手动触发原因(如:测试部署或紧急更新)' |
| 12 | required: false |
| 13 | default: 'Manual run' |
| 14 | |
| 15 | jobs: |
| 16 | sync: |
| 17 | runs-on: ubuntu-latest |
| 18 | |
| 19 | steps: |
| 20 | # 🔍 1. 检出 page 分支内容 |
| 21 | - name: 📥 Checkout page branch |
| 22 | uses: actions/checkout@v5 |
| 23 | with: |
| 24 | ref: page |
| 25 | token: ${{ secrets.GITHUB_TOKEN }} |
| 26 | fetch-depth: 0 # 获取完整历史记录,确保 git diff 能正常工作 |
| 27 | |
| 28 | # 🔍 2. 检测是否有实际变更(兼容首次提交) |
| 29 | - name: 🔎 Check for file changes |
| 30 | id: check |
| 31 | run: | |
| 32 | # 判断当前是否为首次提交(无父提交) |
| 33 | if [ -z "$(git rev-parse --verify HEAD^ 2>/dev/null)" ]; then |
| 34 | echo "🆕 First commit detected — assuming changes exist and proceeding." |
| 35 | echo "skip=false" >> $GITHUB_OUTPUT |
| 36 | else |
| 37 | # 获取上一次提交与当前提交之间的文件差异 |
| 38 | CHANGES=$(git diff --name-only HEAD^ HEAD) |
| 39 | if [ -z "$CHANGES" ]; then |
| 40 | echo "✅ No changes detected since last commit. Skipping sync." |
| 41 | echo "skip=true" >> $GITHUB_OUTPUT |
| 42 | else |
| 43 | echo "✨ Changes detected:" |
| 44 | echo "$CHANGES" |
| 45 | echo "skip=false" >> $GITHUB_OUTPUT |
| 46 | fi |
| 47 | fi |
| 48 | |
| 49 | # 🚀 3. 推送到 CNB(仅当有变更时执行) |
| 50 | - name: 🚀 Push to CNB & Trigger Deployment |
| 51 | if: steps.check.outputs.skip == 'false' |
| 52 | env: |
| 53 | CNB_USERNAME: ${{ secrets.CNB_USERNAME }} |
| 54 | CNB_TOKEN: ${{ secrets.CNB_TOKEN }} |
| 55 | run: | |
| 56 | echo "🧹 Cleaning old Git metadata..." |
| 57 | rm -rf .git |
| 58 | |
| 59 | echo "🌱 Initializing new Git repository..." |
| 60 | git init |
| 61 | git config --global user.name 'GitHub Action' |
| 62 | git config --global user.email 'action@github.com' |
| 63 | |
| 64 | echo "💾 Adding all static files to staging area..." |
| 65 | git add . |
| 66 | |
| 67 | echo "📝 Committing with timestamp..." |
| 68 | # 使用 %% 转义 %,因为 YAML 中 % 是特殊字符 |
| 69 | git commit -m "⚡ Sync from GitHub page branch: $(date +'%%Y-%%m-%%d %%H:%%M:%%S')" |
| 70 | |
| 71 | echo "🔗 Setting remote to CNB repository..." |
| 72 | git remote add origin "https://${{ secrets.CNB_USERNAME }}:${{ secrets.CNB_TOKEN }}@cnb.cool/gyhwd.top/AhHui-Homepage.git" |
| 73 | |
| 74 | echo "🚀 Forcing push to main branch on CNB (default deployment branch)..." |
| 75 | git branch -M main |
| 76 | git push --force --quiet origin main |
| 77 | |
| 78 | echo "🎉 Successfully pushed to CNB: wojack/AhHui-Homepage" |
| 79 | |
| 80 | # 👇 新增:发送事件触发服务器部署 |
| 81 | - name: 🔔 Trigger server deployment |
| 82 | uses: actions/github-script@v7 |
| 83 | with: |
| 84 | script: | |
| 85 | await github.rest.repos.createDispatchEvent({ |
| 86 | owner: context.repo.owner, |
| 87 | repo: context.repo.repo, |
| 88 | event_type: 'deploy-to-server', |
| 89 | client_payload: { |
| 90 | message: 'CNB updated, triggering server sync', |
| 91 | commit: context.sha |
| 92 | } |
| 93 | }) |