於 Github Pages 部署站點

本教程展示如何於 GitHub Pages 部署站點。

爲倉庫啓用 GitHub Pages

  1. 點擊 Settings 標籤。
  2. 打開 Pages.
  3. 選擇 GitHUb Actions 作爲 Source
  4. 可選:設置自定義域名。

創建 GitHub Pages 工作流程

創建以下工作流程,並提交到倉庫。

.github/workflows/gh-pages.yaml
 1name: GitHub Pages
 2
 3on:
 4  # auto deploy when pushing to specified branches.
 5  push:
 6    branches:
 7      - main
 8
 9  # allow deploying manually.
10  workflow_dispatch:
11
12permissions:
13  contents: read
14  pages: write
15  id-token: write
16
17concurrency:
18  group: "pages"
19  cancel-in-progress: false
20
21defaults:
22  run:
23    shell: bash
24
25jobs:
26  build:
27    runs-on: ubuntu-latest
28    steps:
29      - name: Checkout
30        uses: actions/checkout@v3
31        with:
32          submodules: recursive
33
34      - name: Setup Pages
35        id: pages
36        uses: actions/configure-pages@v3
37
38      - name: Setup Node
39        uses: actions/setup-node@v3
40        with:
41          node-version: "18"
42
43      - name: Setup Hugo
44        uses: peaceiris/actions-hugo@v2
45        with:
46          hugo-version: "latest"
47          extended: true
48
49      - name: Install Node.js dependencies
50        run: npm ci
51
52      - name: Build with Hugo
53        env:
54          HUGO_ENVIRONMENT: production
55          HUGO_ENV: production
56        run: |
57          hugo \
58            --gc \
59            --enableGitInfo \
60            --minify \
61            --baseURL "${{ steps.pages.outputs.base_url }}/"          
62
63      - name: Upload artifact
64        uses: actions/upload-pages-artifact@v1
65        with:
66          path: ./public
67
68  deploy:
69    environment:
70      name: github-pages
71      url: ${{ steps.deployment.outputs.page_url }}
72    runs-on: ubuntu-latest
73    needs: build
74    steps:
75      - name: Deploy to GitHub Pages
76        id: deployment
77        uses: actions/deploy-pages@v2