個人站/專案站、自訂網域、部署方式
GitHub Pages 是 GitHub 內建的靜態網站託管服務——把 repo 裡的 HTML/CSS/JS(或 Markdown)直接變成網站,免費、附 HTTPS、綁 github.io 網域。
Pages 有兩種站:
| 類型 | repo 名稱 | 網址 |
|---|---|---|
| 個人/組織站 | 必須是 你.github.io | https://你.github.io/ |
| 專案站 | 任何名稱 | https://你.github.io/repo名/ |
1. repo → Settings → Pages 2. Source: Deploy from a branch 3. Branch: main / (root) 4. Save → 等 1~2 分鐘建置 5. 出現網址:https://你.github.io/repo名/
my-site/
├── index.html
├── about.html
└── assets/
└── site.csshttps://使用者.github.io/my-site/ # 專案網頁:/repo名/ # 個人網頁:/(repo 名必須是 使用者.github.io)
.nojekyll 檔,讓 GitHub 不要用 Jekyll 處理你的 HTML(本站就是這樣做的)。否則有些檔名/路徑可能被改寫。需要先編譯(Astro / Vite / Jekyll / MkDocs 等)的專案,用 workflow 建置後再部署。最簡版本:
on: push: branches: [main] permissions: contents: read pages: write id-token: write jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/configure-pages@v5 - uses: actions/upload-pages-artifact@v3 with: path: . - uses: actions/deploy-pages@v4
細節(on / jobs / permissions)在單元 7學到後就看得懂。這裡先會「複製貼上可用」。
gh repo create my-site --public --source=. --push gh api --method POST repos/USER/my-site/pages -f "source[branch]=main" -f "source[path]=/"
想用自己的網域(如 blog.example.com):
blog.example.com。你.github.io。https://blog.example.com 存取。| 項目 | 限制 |
|---|---|
| 內容 | 靜態檔案(無後端、無資料庫、無伺服器執行) |
| 建置次數 | 每月有配額(免費方案每小時約 10 次) |
| 大小 | repo 建議 1GB 以下、單檔 100MB 內 |
| 用途 | 個人/專案/組織網站 OK;商業「伺服器型」服務禁用 |
1. 建 repo(如 my-site) 2. 放 index.html 與 assets/site.css 3. 加空的 .nojekyll 4. push 到 GitHub 5. Settings → Pages → Deploy from branch → main 6. 等 1–2 分鐘 → https://你.github.io/my-site/
✅ 網址能開
✅ HTTPS 生效(看得到鎖頭)
✅ 更新後 push,網站自動更新
✅ 需要時 CNAME 能指到自訂網域
想更深入,可回顧本站姊妹站的結構當範本——它的原始檔就開源在 GitHub 上。
你有一個 Astro 靜態站,需要: - main 分支:穩定版(已部署) - dev 分支:開發版(可預覽) - 自訂網域:docs.techco.dev - 強制 HTTPS(從 HTTP 跳轉) - 用 GitHub Actions 自動建置部署
# 1. repo 內建立 Pages workflow cat > .github/workflows/pages.yml << 'EOF' name: Deploy Pages on: push: branches: [main, dev] permissions: contents: read pages: write id-token: write jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: { node-version: 20 } - run: npm ci && npm run build - uses: actions/upload-pages-artifact@v3 with: { path: dist } deploy: needs: build runs-on: ubuntu-latest environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} steps: - id: deployment uses: actions/deploy-pages@v4 EOF # 2. 設定自訂網域 echo "docs.techco.dev" > CNAME git add CNAME && git commit -m "chore: add CNAME" # 3. 強制 HTTPS(Settings) gh api --method PUT \ "/repos/TechCo/app/pages" \ -f build_type=workflow \ -f https_enforced=true # 4. 為 dev 分支設定預覽環境 gh api --method POST \ "/repos/TechCo/app/pages/environments" \ -f name="dev-preview" \ -f source='{"branch":"dev"}' # 5. 推送到 main 觸發正式部署 git checkout main && git push # 6. 推送到 dev 觸發預覽部署 git checkout dev && git push # 7. 驗證兩種網址 curl -I https://docs.techco.dev curl -I https://techco.dev/dev/
push 到 main
│
▼
GitHub Pages 服務(內建)
├── 方式一(Deploy from branch):
│ └── 直接複製 static/ 資料夾到 CDN
│
└── 方式二(Actions workflow):
├── runner 執行 build step(如 astro build)
├── 產物上傳為 artifact
└── deploy-pages action 部署 artifact 到 CDN
└── CDN 節點分佈全球(Fastly)
/docs 資料夾或根目錄抓內容——這個分支的歷史會被 GitHub Pages 服務讀取,但你自己看不到「部署結果」(只能看原始碼)。你的帳號.github.io,GitHub 會自動管理 HTTPS 憑證(Let's Encrypt)。網域根(apex)需要改 A record 指向 185.199.108.153 等四個 IP。.nojekyll 檔讓它跳過。否則 _ 開頭的資料夾會被忽略(Jekyll 的约定)。| 症狀 | 可能原因 | 解決方案 |
|---|---|---|
| Pages 網址 404 | 未啟用 Pages / 建置失敗 | 到 Settings → Pages 確認狀態;到 Actions 分頁看建置 log |
| 自訂網域無法訪問 | DNS 未生效 / CNAME 錯誤 | 用 dig docs.techco.dev 確認解析結果 |
| HTTPS 不顯示鎖頭 | 憑證尚未發放 | 等 15-30 分鐘;檢查 DNS 是否指向正確的 CNAME |
| 更新後頁面沒變化 | 瀏覽器快取 | Ctrl+Shift+R 強制重新整理;或在 URL 後加 ?v=2 |
| Actions workflow 沒觸發 | workflow 檔名或觸發條件錯誤 | 確認 .github/workflows/pages.yml 在正確分支;on.push.branches 包含你的分支 |
pr-42.yourapp.pages.dev),PR 合併後自動刪除預覽站。單元 6.8 部署了「一個正式站 + 一個 dev 站」;這裡把文件站放大成產品級 Docs-as-Code:內容與程式碼同 repo、PR 審查才更新、每個 PR 有預覽網址、自動檢查失效連結。做完它,文件品質與程式碼品質同步——這是單元 6.8「怎麼部署」之上的「文件當產品經營」。
techco-docs/ ├── src/ │ ├── content/docs/ │ │ ├── getting-started.md │ │ └── api-reference.md │ └── astro.config.mjs ├── public/ │ ├── favicon.svg │ └── robots.txt ├── .github/workflows/ │ ├── preview.yml # PR → 預覽站(pr-NNN) │ └── deploy.yml # main → 正式站 ├── scripts/ │ └── check-links.mjs # 掃描失效連結 / 圖片 ├── .nojekyll # 跳過 Jekyll(用 Astro 建置) ├── CNAME # docs.techco.dev └── package.json
# .github/workflows/preview.yml(PR 預覽)
name: Preview
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
permissions: { contents: read, pages: write, id-token: write }
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: 20 }
- run: npm ci && npm run build
- name: 檢查失效連結
run: node scripts/check-links.mjs dist
- uses: actions/upload-pages-artifact@v3
with: { path: dist }
- id: deploy
uses: actions/deploy-pages@v4
with: { preview: true }# 1. 正式站回應 curl -sI https://docs.techco.dev | head -1 # 輸出 → HTTP/2 200 # 2. HTTPS 強制 curl -sI http://docs.techco.dev | grep -i location # 輸出 → Location: https://docs.techco.dev/ # 3. PR 預覽網址(在 PR 頁面有 View deployment 連結) gh pr view 45 --json url --jq '.url' # 輸出 → https://github.com/TechCo/techco-docs/pull/45 # 4. 失效連結檢查結果(CI log) # 輸出 → "Checked 42 links, 0 broken"
- 靜態資產走全球 CDN,快取頭設好 - 圖片壓縮 / 轉 WebP,減少頻寬 - 建置用快取 + 增量輸出,PR 預覽更快 - 注意 Pages 每月建置與頻寬限制(100GB/月) - Lighthouse / PageSpeed 量測首屏效能
- 每個 PR 有預覽網址 → 內容先看再上線 - CI 檢查失效連結 / 圖片 / 排版錯誤 - 版本化文件:每一版 docs 對應程式碼版本 - 提供搜尋(pagefind / Algolia)降低找資料成本 - 404 頁 + sitemap 讓使用者與 SEO 都舒服
- HTTPS 強制:所有訪客走加密通道 - 自訂網域確認 DNS 由你控制,避免被劫持 - 建置時不要把 secrets 打包進 dist - robots.txt 控制可被抓取的範圍 - 必要時加 CSP header 防 XSS
| 面向 | github | gitlab | markdown | yaml | json |
|---|---|---|---|---|---|
| 靜態託管 | GitHub Pages(100GB/月) | GitLab Pages | —(不涉及) | —(不涉及) | —(不涉及) |
| 自訂網域 + HTTPS | 內建,Let's Encrypt 自動憑證 | 內建,同樣自動憑證 | — | — | — |
| CI 整合 | GitHub Actions 部署 | GitLab CI 部署 | — | — | — |
| 建置選項 | Jekyll 或任意 SSG(.nojekyll) | 任意(Artifacts → Pages) | 文件內容以 Markdown 撰寫 | SSG 設定檔用 YAML(frontmatter) | SSG 資料 / API 回應用 JSON |
| 與其他欄的關係 | Pages 內容多為 Markdown | 同左 | 頁面內容的主要載體 | frontmatter 與站台設定 | 檢索索引與資料來源 |