自动部署
自动部署
使用 GitHub Actions 实现自动部署,每次推送代码自动构建并发布博客。
GitHub Actions 概述
GitHub Actions 是 GitHub 提供的 CI/CD 服务,可以自动化构建、测试和部署工作流。
配置工作流
在博客根目录创建 .github/workflows/deploy.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| name: Deploy Hexo Blog
on: push: branches: - main
jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 with: submodules: true - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: '20' - name: Cache dependencies uses: actions/cache@v4 with: path: ~/.npm key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.os }}-node- - name: Install dependencies run: npm install - name: Build run: hexo generate - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./public
|
启用 GitHub Pages
- 进入仓库 Settings → Pages
- Source 选择 Deploy from a branch
- Branch 选择 gh-pages,目录选择 / (root)
- 保存
使用 Vercel 自动部署
如果使用 Vercel,不需要配置 GitHub Actions。Vercel 会自动检测推送并部署:
1 2 3 4 5
| git add . git commit -m "new post" git push origin main
|
使用 Netlify 自动部署
- 登录 netlify.com
- 导入 GitHub 仓库
- 配置:
- Build command:
hexo generate - Publish directory:
public
- 保存
每次推送代码,Netlify 会自动构建和部署。
写作工作流
配置好自动部署后,写作流程变得非常简单:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| hexo new "我的新文章"
hexo server
git add . git commit -m "post: 我的新文章" git push origin main
|
多分支部署
可以为不同分支部署不同版本:
1 2 3 4 5
| on: push: branches: - main - dev
|
定时部署
使用 cron 表达式定时触发部署:
1 2 3 4 5 6
| on: schedule: - cron: '0 0 * * 1' push: branches: - main
|
💡 提示:推荐使用 Vercel 或 Netlify 自动部署,比 GitHub Actions 更简单,配置更少。