Skip to content
0

构建与部署

📦 构建与部署

本地构建

bash
# 构建生产版本
make build

# 或直接使用 Hugo
./hugo --source=hugo-teek-site --minify

构建产物位于 hugo-teek-site/public/ 目录。

部署到服务器

方式1: 静态文件部署

bash
# 构建
make build

# 上传到服务器
rsync -avz hugo-teek-site/public/ user@server:/var/www/html/

方式2: Nginx 配置

nginx
server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/html;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }

    # 开启 Gzip 压缩
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
}

方式3: GitHub Pages

创建 .github/workflows/deploy.yml

yaml
name: Deploy Hugo site to Pages

on:
  push:
    branches: ["main"]
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.150.0'
          extended: true

      - name: Setup Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.21'

      - name: Build
        run: |
          make install-deps
          make build

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v2
        with:
          path: ./hugo-teek-site/public

  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v2

方式4: Docker 部署

创建 Dockerfile

dockerfile
FROM hugomods/hugo:exts as builder

WORKDIR /src
COPY . .
RUN make build

FROM nginx:alpine
COPY --from=builder /src/hugo-teek-site/public /usr/share/nginx/html
EXPOSE 80

构建并运行:

bash
docker build -t hugo-teek-blog .
docker run -d -p 80:80 hugo-teek-blog
最近更新