Hexo 四种部署方式

🌐 快速安装
1 2 3 4 5 6 7 8 9 10
|
npm install -g hexo-cli
hexo init <folder> cd <folder> npm install
|
1️⃣ 部署本仓库 GitHub Actions (官方推荐)
在 GitHub Pages 上部署 Hexo
.gitignore 文件中包含一行 public/
- 自定义域名需要在
source/ 文件夹中新增 CNAME
- 建立
.github/workflows/pages.yml
- 本地检查
node --version,替换版本
- 将 main 分支 push 到远程仓库
- 远程仓库
Settings -> Pages -> Source -> GitHub Actions
官方版本
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 42 43 44 45 46 47 48 49 50 51
| .github/workflows/pages.yml name: Pages
on: push: branches: - main
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: token: ${{ secrets.GITHUB_TOKEN }} submodules: recursive - name: Use Node.js 20 uses: actions/setup-node@v4 with: node-version: \"20\" - name: Cache NPM dependencies uses: actions/cache@v4 with: path: node_modules key: ${{ runner.OS }}-npm-cache restore-keys: | ${{ runner.OS }}-npm-cache - name: Install Dependencies run: npm install - name: Build run: npm run build - name: Upload Pages artifact uses: actions/upload-pages-artifact@v3 with: path: ./public deploy: needs: build permissions: pages: write id-token: write environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} runs-on: ubuntu-latest steps: - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@v4
|
简化版

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
| name: simple-hexo-deploy-actions
on: push: branches: [ main ] workflow_dispatch:
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: submodules: recursive - uses: actions/setup-node@v4 with: node-version: \"20\" - uses: actions/cache@v4 with: path: node_modules key: ${{ runner.OS }}-npm-cache - run: npm install - run: npm run build - uses: actions/upload-pages-artifact@v3 with: path: ./public
deploy: needs: build permissions: pages: write id-token: write environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} runs-on: ubuntu-latest steps: - uses: actions/deploy-pages@v4 id: deployment
|
2️⃣ 一键部署
一键部署
1
| npm install hexo-deployer-git --save
|
1 2 3 4 5
| deploy: type: git repo: <repository url> branch: [branch] message: [message]
|
3️⃣ 部署本仓库 gh-pages
使用 Personal Access Token (PAT)
- 步骤1:创建
Personal Access Token
点击 GitHub 右上角头像 → Settings,左侧菜单选择 Developer settings → Personal access tokens → Tokens (classic),点击 Generate new token (classic)
设置:
Note: 填写名称(如 "gh-pages-deploy")
Expiration: 选择合适的时间
Scopes: 勾选 repo(完整权限)
生成并复制 token(只显示一次,务必保存)
1 2 3 4 5 6 7 8
| - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v4 with: personal_token: ${{ secrets.PERSONAL_TOKEN }} publish_dir: ./build publish_branch: gh-pages
|
简化版

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| # .github/workflows/simple-hexo-deploy-pages.yml name: simple-hexo-deploy-pages
on: push: branches: [ main ] workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: submodules: recursive - uses: actions/setup-node@v4 with: node-version: \"20\" - uses: actions/cache@v4 with: path: node_modules key: ${{ runner.OS }}-npm-cache - run: npm install - run: npm run build - uses: actions/upload-artifact@v4 with: name: public path: ${{ github.workspace }}/public retention-days: 1
deploy: needs: build permissions: pages: write id-token: write environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} runs-on: ubuntu-latest steps: - uses: actions/download-artifact@v4 with: name: public path: ./public - uses: peaceiris/actions-gh-pages@v4 with: personal_token: ${{ secrets.PERSONAL_TOKEN }} external_repository: ${{ github.respository }} publish_branch: gh-pages publish_dir: ./public keep_files: false force_orphan: false commit_message: '🚀 Deploy static pages to gh-pages' allow_empty_commit: false cname: ${{ github.respository_owner }}.github.io/${{ github.event.respository.name }} user_name: \"github-actions[bot]\" user_email: \"github-actions[bot]@users.noreply.github.com\"
|
4️⃣ 部署其他仓库 gh-pages
使用 Personal Access Token (PAT) 方法同上
简化版

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| name: simple-hexo-deploy-other
on: push: branches: [ main ] workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: submodules: recursive - uses: actions/setup-node@v4 with: node-version: \"20\" - uses: actions/cache@v4 with: path: node_modules key: ${{ runner.OS }}-npm-cache - run: npm install - run: npm run build - uses: actions/upload-artifact@v4 with: name: public path: ${{ github.workspace }}/public retention-days: 1
deploy: needs: build permissions: pages: write id-token: write environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} runs-on: ubuntu-latest steps: - uses: actions/download-artifact@v4 with: name: public path: ./public - uses: peaceiris/actions-gh-pages@v4 with: personal_token: ${{ secrets.PERSONAL_TOKEN }} external_repository: publish_branch: gh-pages publish_dir: ./public keep_files: false force_orphan: false commit_message: '🚀 Deploy static pages to gh-pages' allow_empty_commit: false cname: user_name: \"github-actions[bot]\" user_email: \"github-actions[bot]@users.noreply.github.com\"
|