Update autobuild.yml #4
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
name: Deploy Home Page | |
on: | |
push: | |
branches: | |
- main # 监听 main 分支的提交 | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 # 确保 git 历史被完整获取,以便用于版本推送 | |
persist-credentials: false # 不在后续步骤中使用内建的 credentials | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: "20" | |
- name: Install dependencies | |
run: npm install | |
- name: Build | |
run: npm run build | |
- name: Commit docs directory | |
run: | | |
git config --local user.email "[email protected]" | |
git config --local user.name "GitHub Action" | |
git add -f docs | |
git commit -m "Deploy new documentation" -a || echo "No changes to commit" | |
- name: Push changes using github-script | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const repo = context.repo; | |
const fs = require('fs'); | |
const path = require('path'); | |
const commitFiles = async (folderPath, base = '') => { | |
const files = fs.readdirSync(folderPath); | |
for (const file of files) { | |
const filePath = path.join(folderPath, file); | |
const gitPath = path.join(base, file); | |
if (fs.statSync(filePath).isDirectory()) { | |
await commitFiles(filePath, gitPath); | |
} else { | |
const content = fs.readFileSync(filePath, 'base64'); | |
await github.rest.repos.createOrUpdateFileContents({ | |
owner: repo.owner, | |
repo: repo.repo, | |
path: gitPath, | |
message: `Update ${gitPath}`, | |
content, | |
branch: 'gh-pages', | |
}); | |
} | |
} | |
}; | |
await commitFiles('docs'); |