173 lines
6.0 KiB
YAML
173 lines
6.0 KiB
YAML
name: Build and Deploy Site
|
||
|
||
on:
|
||
push:
|
||
branches: [ main ]
|
||
workflow_dispatch:
|
||
|
||
# 设置GITHUB_TOKEN的权限
|
||
permissions:
|
||
contents: write
|
||
pages: write
|
||
id-token: write
|
||
|
||
# 允许一个并发部署
|
||
concurrency:
|
||
group: "pages"
|
||
cancel-in-progress: true
|
||
|
||
jobs:
|
||
build_and_deploy:
|
||
runs-on: ubuntu-latest
|
||
steps:
|
||
- name: Checkout repository
|
||
uses: actions/checkout@v4
|
||
# 使用persist-credentials: false,以便后续步骤可以使用自定义的提交者
|
||
with:
|
||
persist-credentials: false
|
||
fetch-depth: 0 # 获取所有历史记录以进行diff检查
|
||
|
||
- name: Setup Node.js
|
||
uses: actions/setup-node@v4
|
||
with:
|
||
node-version: '16'
|
||
cache: 'npm'
|
||
|
||
- name: Install dependencies
|
||
run: npm install
|
||
|
||
# --- 书签处理步骤开始 ---
|
||
- name: Create bookmarks directory if not exists
|
||
run: mkdir -p bookmarks
|
||
|
||
- name: Check for bookmark files
|
||
id: check_bookmark_files
|
||
run: |
|
||
if [ "$(find bookmarks -type f -name "*.html" 2>/dev/null)" ]; then
|
||
echo "found=true" >> $GITHUB_OUTPUT
|
||
echo "Found bookmark files to process"
|
||
else
|
||
echo "found=false" >> $GITHUB_OUTPUT
|
||
echo "No bookmark files found"
|
||
fi
|
||
|
||
- name: Process bookmark file
|
||
if: steps.check_bookmark_files.outputs.found == 'true'
|
||
run: |
|
||
echo "Processing bookmark files..."
|
||
node src/bookmark-processor.js
|
||
|
||
- name: Debug directory contents
|
||
if: steps.check_bookmark_files.outputs.found == 'true'
|
||
run: |
|
||
echo "Current directory contents:"
|
||
ls -la
|
||
echo "Does bookmarks.user.yml exist?"
|
||
if [ -f bookmarks.user.yml ]; then
|
||
echo "YES - bookmarks.user.yml exists"
|
||
cat bookmarks.user.yml | head -n 10
|
||
else
|
||
echo "NO - bookmarks.user.yml does not exist"
|
||
fi
|
||
|
||
- name: Commit bookmarks.user.yml changes
|
||
if: steps.check_bookmark_files.outputs.found == 'true'
|
||
run: |
|
||
git config --local user.email "action@github.com"
|
||
git config --local user.name "GitHub Action (Bookmarks)"
|
||
|
||
# Check if the file exists
|
||
if [ -f bookmarks.user.yml ]; then
|
||
# Check if file is already tracked by git
|
||
if git ls-files --error-unmatch bookmarks.user.yml 2>/dev/null; then
|
||
echo "bookmarks.user.yml exists and is tracked by git"
|
||
# Check if it has changes
|
||
if ! git diff --quiet bookmarks.user.yml; then
|
||
echo "bookmarks.user.yml has changes, committing..."
|
||
git add bookmarks.user.yml
|
||
git commit -m "Update bookmarks.user.yml from imported bookmarks"
|
||
else
|
||
echo "No changes to bookmarks.user.yml"
|
||
fi
|
||
else
|
||
echo "bookmarks.user.yml exists but is not tracked by git (new file)"
|
||
git add bookmarks.user.yml
|
||
git commit -m "Add bookmarks.user.yml from imported bookmarks"
|
||
fi
|
||
else
|
||
echo "ERROR: bookmarks.user.yml does not exist! Bookmark processing may have failed."
|
||
echo "Current directory contents:"
|
||
ls -la
|
||
fi
|
||
|
||
- name: Clean up processed bookmark files
|
||
if: steps.check_bookmark_files.outputs.found == 'true'
|
||
run: |
|
||
# 检查是否有html文件需要清理
|
||
if [ "$(find bookmarks -type f -name "*.html" 2>/dev/null)" ]; then
|
||
git config --local user.email "action@github.com"
|
||
git config --local user.name "GitHub Action (Cleanup)"
|
||
echo "Cleaning up HTML files..."
|
||
find bookmarks -type f -name "*.html" -delete
|
||
# 检查清理后是否有更改(比如删除了文件)
|
||
if ! git diff --quiet bookmarks/; then
|
||
git add bookmarks/
|
||
git commit -m "Clean up processed bookmark files"
|
||
# 不需要push
|
||
else
|
||
echo "No HTML files needed cleanup commit."
|
||
fi
|
||
else
|
||
echo "No HTML files found to clean up."
|
||
fi
|
||
# --- 书签处理步骤结束 ---
|
||
|
||
- name: Push configuration changes (if any)
|
||
# 只有在书签处理步骤修改了文件时才推送
|
||
# 使用 GITHUB_TOKEN 推送
|
||
if: steps.check_bookmark_files.outputs.found == 'true'
|
||
run: |
|
||
echo "Checking git status before pushing..."
|
||
git status
|
||
|
||
echo "Checking bookmarks.user.yml existence before pushing..."
|
||
if [ -f bookmarks.user.yml ]; then
|
||
echo "bookmarks.user.yml exists with content:"
|
||
ls -la bookmarks.user.yml
|
||
echo "First 5 lines:"
|
||
head -n 5 bookmarks.user.yml
|
||
else
|
||
echo "WARNING: bookmarks.user.yml does not exist before pushing!"
|
||
fi
|
||
|
||
echo "Pushing changes to repository..."
|
||
git push "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" HEAD:${{ github.ref_name }}
|
||
env:
|
||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||
|
||
# --- 网站构建和部署步骤 ---
|
||
- name: Generate site
|
||
run: npm run generate
|
||
|
||
- name: Check favicon
|
||
run: |
|
||
if [ -f dist/favicon.ico ]; then
|
||
echo "Favicon exists"
|
||
ls -l dist/favicon.ico
|
||
else
|
||
echo "Warning: favicon.ico not found in dist directory"
|
||
# 暂时改为警告,避免因为图标问题阻止部署
|
||
# exit 1
|
||
fi
|
||
|
||
- name: Setup Pages
|
||
uses: actions/configure-pages@v4
|
||
|
||
- name: Upload artifact
|
||
uses: actions/upload-pages-artifact@v3
|
||
with:
|
||
path: 'dist'
|
||
|
||
- name: Deploy to GitHub Pages
|
||
id: deployment
|
||
uses: actions/deploy-pages@v4 |