deploy-to-server.yml
· 2.0 KiB · YAML
原始文件
# .github/workflows/deploy-to-server.yml
name: Deploy CNB Content to Server (Auto via Event)
on:
repository_dispatch:
types: [deploy-to-server] # 监听来自其他工作流的自定义事件
workflow_dispatch: # 保留手动触发能力(调试用)
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# 🔍 1. 检出当前仓库(用于获取 secrets)
- name: 📥 Checkout repository
uses: actions/checkout@v5
# 🚀 2. 使用 SSH 连接服务器并部署
- name: 🖥️ Deploy to server via SSH
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.SERVER_SSH_KEY }} # 私钥内容(完整 PEM 格式)
port: ${{ secrets.SERVER_PORT || 22 }} # 默认 22,可不填
script: |
# 设置变量
TARGET_DIR="/opt/1panel/www/sites/home.gyhwd.top/index"
TEMP_DIR="/tmp/cnb-deploy"
echo "🧹 Cleaning old temporary directory..."
rm -rf "$TEMP_DIR"
mkdir -p "$TEMP_DIR"
echo "🔗 Cloning CNB repository into temporary directory..."
git clone --depth 1 \
"https://${{ secrets.CNB_USERNAME }}:${{ secrets.CNB_TOKEN }}@cnb.cool/gyhwd.top/AhHui-Homepage.git" \
"$TEMP_DIR"
echo "📂 Clearing target directory..."
rm -rf "$TARGET_DIR"/*
mkdir -p "$TARGET_DIR"
echo "🔄 Copying CNB content to server site directory (flattened)..."
cp -r "$TEMP_DIR"/* "$TARGET_DIR/"
echo "🗑️ Cleaning up temporary directory..."
rm -rf "$TEMP_DIR"
echo "✅ Successfully deployed! Files synced to: $TARGET_DIR"
ls -la "$TARGET_DIR" # Output final structure for debugging
# (可选)重启 Nginx(如果需要)
# systemctl reload nginx
echo "🌐 Website will auto-deploy at: https://home.gyhwd.top"
| 1 | # .github/workflows/deploy-to-server.yml |
| 2 | name: Deploy CNB Content to Server (Auto via Event) |
| 3 | |
| 4 | on: |
| 5 | repository_dispatch: |
| 6 | types: [deploy-to-server] # 监听来自其他工作流的自定义事件 |
| 7 | workflow_dispatch: # 保留手动触发能力(调试用) |
| 8 | |
| 9 | jobs: |
| 10 | deploy: |
| 11 | runs-on: ubuntu-latest |
| 12 | |
| 13 | steps: |
| 14 | # 🔍 1. 检出当前仓库(用于获取 secrets) |
| 15 | - name: 📥 Checkout repository |
| 16 | uses: actions/checkout@v5 |
| 17 | |
| 18 | # 🚀 2. 使用 SSH 连接服务器并部署 |
| 19 | - name: 🖥️ Deploy to server via SSH |
| 20 | uses: appleboy/ssh-action@v1 |
| 21 | with: |
| 22 | host: ${{ secrets.SERVER_IP }} |
| 23 | username: ${{ secrets.SERVER_USERNAME }} |
| 24 | key: ${{ secrets.SERVER_SSH_KEY }} # 私钥内容(完整 PEM 格式) |
| 25 | port: ${{ secrets.SERVER_PORT || 22 }} # 默认 22,可不填 |
| 26 | script: | |
| 27 | # 设置变量 |
| 28 | TARGET_DIR="/opt/1panel/www/sites/home.gyhwd.top/index" |
| 29 | TEMP_DIR="/tmp/cnb-deploy" |
| 30 | |
| 31 | echo "🧹 Cleaning old temporary directory..." |
| 32 | rm -rf "$TEMP_DIR" |
| 33 | mkdir -p "$TEMP_DIR" |
| 34 | |
| 35 | echo "🔗 Cloning CNB repository into temporary directory..." |
| 36 | git clone --depth 1 \ |
| 37 | "https://${{ secrets.CNB_USERNAME }}:${{ secrets.CNB_TOKEN }}@cnb.cool/gyhwd.top/AhHui-Homepage.git" \ |
| 38 | "$TEMP_DIR" |
| 39 | |
| 40 | echo "📂 Clearing target directory..." |
| 41 | rm -rf "$TARGET_DIR"/* |
| 42 | mkdir -p "$TARGET_DIR" |
| 43 | |
| 44 | echo "🔄 Copying CNB content to server site directory (flattened)..." |
| 45 | cp -r "$TEMP_DIR"/* "$TARGET_DIR/" |
| 46 | |
| 47 | echo "🗑️ Cleaning up temporary directory..." |
| 48 | rm -rf "$TEMP_DIR" |
| 49 | |
| 50 | echo "✅ Successfully deployed! Files synced to: $TARGET_DIR" |
| 51 | ls -la "$TARGET_DIR" # Output final structure for debugging |
| 52 | |
| 53 | # (可选)重启 Nginx(如果需要) |
| 54 | # systemctl reload nginx |
| 55 | |
| 56 | echo "🌐 Website will auto-deploy at: https://home.gyhwd.top" |
generate-static-site.yml
· 3.3 KiB · YAML
原始文件
# .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: 🚀 Update page branch with new content
run: |
cd static
git init
git config --global user.name 'GitHub Action'
git config --global user.email 'action@github.com'
git remote add origin "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git"
git fetch origin page --depth=1 || true
git checkout page || git checkout -b page
git add .
git commit -m "[skip ci] 🖼️ Auto-update Bing images and deploy $(date +'%%Y-%%m-%%d %%H:%%M:%%S')" --allow-empty
git push --force origin page
# 👇 新增:主动触发 sync-page-to-cnb.yml
echo "✅ Pushed to page branch. Triggering sync to CNB..."
curl -X POST \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/dispatches \
-d '{"event_type": "sync-to-cnb"}'
| 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: 🚀 Update page branch with new content |
| 69 | run: | |
| 70 | cd static |
| 71 | |
| 72 | git init |
| 73 | git config --global user.name 'GitHub Action' |
| 74 | git config --global user.email 'action@github.com' |
| 75 | |
| 76 | git remote add origin "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" |
| 77 | |
| 78 | git fetch origin page --depth=1 || true |
| 79 | |
| 80 | git checkout page || git checkout -b page |
| 81 | |
| 82 | git add . |
| 83 | git commit -m "[skip ci] 🖼️ Auto-update Bing images and deploy $(date +'%%Y-%%m-%%d %%H:%%M:%%S')" --allow-empty |
| 84 | |
| 85 | git push --force origin page |
| 86 | |
| 87 | # 👇 新增:主动触发 sync-page-to-cnb.yml |
| 88 | echo "✅ Pushed to page branch. Triggering sync to CNB..." |
| 89 | curl -X POST \ |
| 90 | -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 91 | -H "Accept: application/vnd.github.v3+json" \ |
| 92 | https://api.github.com/repos/${{ github.repository }}/dispatches \ |
| 93 | -d '{"event_type": "sync-to-cnb"}' |
sync-page-to-cnb.yml
· 2.6 KiB · YAML
原始文件
# .github/workflows/sync-page-to-cnb.yml
name: Sync Page Branch to CNB (Auto Deploy)
# 避免并发堆积
concurrency:
group: ${{ github.ref }}
cancel-in-progress: true
on:
repository_dispatch:
types: [sync-to-cnb] # 👈 监听来自 generate-static-site.yml 的事件
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: 🔁 Always deploy
id: check
run: |
echo "✅ Page branch updated with new commit. Forcing deployment to CNB."
echo "skip=false" >> $GITHUB_OUTPUT
# 🚀 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..."
git commit -m "[skip ci] 🖼️ Auto-update Bing images and deploy $(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..."
git branch -M main
git push --force --quiet origin main
echo "🎉 Successfully pushed to CNB: wojack/AhHui-Homepage"
# 4.👇 触发服务器部署
- 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 | # 避免并发堆积 |
| 5 | concurrency: |
| 6 | group: ${{ github.ref }} |
| 7 | cancel-in-progress: true |
| 8 | |
| 9 | on: |
| 10 | repository_dispatch: |
| 11 | types: [sync-to-cnb] # 👈 监听来自 generate-static-site.yml 的事件 |
| 12 | workflow_dispatch: |
| 13 | inputs: |
| 14 | reason: |
| 15 | description: '测试部署' |
| 16 | required: false |
| 17 | default: 'Manual run' |
| 18 | |
| 19 | jobs: |
| 20 | sync: |
| 21 | runs-on: ubuntu-latest |
| 22 | |
| 23 | steps: |
| 24 | # 🔍 1. 检出 page 分支内容 |
| 25 | - name: 📥 Checkout page branch |
| 26 | uses: actions/checkout@v5 |
| 27 | with: |
| 28 | ref: page |
| 29 | token: ${{ secrets.GITHUB_TOKEN }} |
| 30 | fetch-depth: 0 # 获取完整历史记录,确保 git diff 能正常工作 |
| 31 | |
| 32 | # 🔍 2. 强制部署(不再检查变更) |
| 33 | - name: 🔁 Always deploy |
| 34 | id: check |
| 35 | run: | |
| 36 | echo "✅ Page branch updated with new commit. Forcing deployment to CNB." |
| 37 | echo "skip=false" >> $GITHUB_OUTPUT |
| 38 | |
| 39 | # 🚀 3. 推送到 CNB |
| 40 | - name: 🚀 Push to CNB & Trigger Deployment |
| 41 | if: steps.check.outputs.skip == 'false' |
| 42 | env: |
| 43 | CNB_USERNAME: ${{ secrets.CNB_USERNAME }} |
| 44 | CNB_TOKEN: ${{ secrets.CNB_TOKEN }} |
| 45 | run: | |
| 46 | echo "🧹 Cleaning old Git metadata..." |
| 47 | rm -rf .git |
| 48 | |
| 49 | echo "🌱 Initializing new Git repository..." |
| 50 | git init |
| 51 | git config --global user.name 'GitHub Action' |
| 52 | git config --global user.email 'action@github.com' |
| 53 | |
| 54 | echo "💾 Adding all static files to staging area..." |
| 55 | git add . |
| 56 | |
| 57 | echo "📝 Committing with timestamp..." |
| 58 | git commit -m "[skip ci] 🖼️ Auto-update Bing images and deploy $(date +'%%Y-%%m-%%d %%H:%%M:%%S')" |
| 59 | |
| 60 | echo "🔗 Setting remote to CNB repository..." |
| 61 | git remote add origin "https://${{ secrets.CNB_USERNAME }}:${{ secrets.CNB_TOKEN }}@cnb.cool/gyhwd.top/AhHui-Homepage.git" |
| 62 | |
| 63 | echo "🚀 Forcing push to main branch on CNB..." |
| 64 | git branch -M main |
| 65 | git push --force --quiet origin main |
| 66 | |
| 67 | echo "🎉 Successfully pushed to CNB: wojack/AhHui-Homepage" |
| 68 | |
| 69 | # 4.👇 触发服务器部署 |
| 70 | - name: 🔔 Trigger server deployment |
| 71 | uses: actions/github-script@v7 |
| 72 | with: |
| 73 | script: | |
| 74 | await github.rest.repos.createDispatchEvent({ |
| 75 | owner: context.repo.owner, |
| 76 | repo: context.repo.repo, |
| 77 | event_type: 'deploy-to-server', |
| 78 | client_payload: { |
| 79 | message: 'CNB updated, triggering server sync', |
| 80 | commit: context.sha |
| 81 | } |
| 82 | }) |