部署

如何部署使用 Lume 构建的站点。

使用 rsync 手动部署

这是部署站点最简单的方式:只需构建站点,然后使用 rsync 将其上传到你的服务器。一个简单的方法是在 deno.json 文件中创建一个 deno 任务:

{
  "importMap": "import_map.json",
  "tasks": {
    "build": "deno task lume",
    "serve": "deno task lume -s",
    "lume": "echo \"import 'lume/cli.ts'\" | deno run -A -",
    "deploy": "deno task build && rsync -r _site/ user@my-site.com:~/www"
  }
}

除了常规的 Lume 任务之外,我们还添加了一个名为 deploy 的新任务,它执行两个命令:构建站点并将其上传到服务器。现在,要构建和部署你的站点,只需运行:

deno task deploy

GitHub Pages

要使用 GitHub Pages 部署 Lume 站点,请转到你的仓库中的 Settings > Pages,配置 source 以使用 GitHub Actions,并创建以下 workflow:

name: Publish on GitHub Pages

on:
  push:
    branches: [main]

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

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Clone repository
        uses: actions/checkout@v4

      - name: Setup Deno environment
        uses: denoland/setup-deno@v2

      - name: Build site
        run: deno task build

      - name: Setup Pages
        uses: actions/configure-pages@v5

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: "_site"

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

GitLab Pages

要使用 GitLab Pages 部署 Lume 站点,请设置一个包含以下代码的 CI/CD 配置:

image: denoland/deno

stages:
  - pages

pages:
  stage: pages
  script: deno task build --dest=public

  artifacts:
    paths:
      - public

build 命令中的 --dest=public 参数将目标文件夹设置为 ./public。这是 GitLab 用来发布站点的文件夹。如果你已经在 配置文件 中定义了 dest folder,则不需要此参数。

Deno Deploy

Deno Deploy 是由 Deno 提供的分布式部署系统,支持静态文件。它要求你的仓库在 GitHub 上。

  • 在 Deno Deploy 中注册并创建一个新项目。
  • 配置 Git integration 以使用 GitHub Actions 部署模式。
  • 在你的仓库中,你需要一个入口点文件来 serving 文件。创建文件 serve.ts,代码如下:
import Server from "lume/core/server.ts";

const server = new Server({
  port: 8000,
  root: `${Deno.cwd()}/_site`,
});

server.start();

console.log("Listening on http://localhost:8000");
  • 创建以下 GitHub workflow,将 project-name 替换为你在 Deno Deploy 中的项目名称。
name: Publish on Deno Deploy

on:
  push:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read

    steps:
      - name: Clone repository
        uses: actions/checkout@v4

      - name: Setup Deno environment
        uses: denoland/setup-deno@v2

      - name: Build site
        run: deno task build

      - name: Deploy to Deno Deploy
        uses: denoland/deployctl@v1
        with:
          project: project-name
          import-map: "./deno.json"
          entrypoint: serve.ts

Netlify

根据 Netlify 文档网站上的 "Available software at build time" 页面,Deno 是构建时支持的几种运行时之一。为了构建你的项目,你需要告诉 Netlify 在构建时运行哪个命令,在本例中是 deno task build

在你的仓库中创建 netlify.toml 文件,代码如下:

[build]
  publish = "_site"
  command = "deno task build"
[build]
  publish = "_site"
  command = """
  curl -fsSL https://deno.land/install.sh | sh && \
  /opt/buildhome/.deno/bin/deno task build \
"""

Vercel

Vercel 默认情况下没有 Deno,因此构建命令必须安装它。

curl -fsSL https://deno.land/x/install/install.sh | sh && /vercel/.deno/bin/deno task build

还要记住将输出目录配置为 _site

Fleek

要使用 Fleek 部署你的 Lume 站点,请在你的仓库中创建一个 .fleek.json 文件,代码如下:

{
  "build": {
    "image": "denoland/deno",
    "command": "deno task build",
    "publicDir": "_site"
  }
}

Cloudflare Pages

要使用 Cloudflare Pages 部署你的 Lume 站点,请按如下方式配置构建命令:

curl -fsSL https://deno.land/x/install/install.sh | sh && /opt/buildhome/.deno/bin/deno task build

记住将输出目录配置为 _site

Render

要使用 Render 部署你的 Lume 站点,请创建一个新的 Static Site 项目,并按如下方式配置构建命令:

curl -fsSL https://deno.land/x/install/install.sh | sh && /opt/render/.deno/bin/deno task build

将输出目录配置为 _site

AWS Amplify

要使用 AWS Amplify 部署你的 Lume 站点,请创建一个 amplify.yml 文件,代码如下:

version: 1
frontend:
  phases:
    build:
      commands:
        - curl -fsSL https://deno.land/x/install/install.sh | sh
        - /root/.deno/bin/deno task build
  artifacts:
    baseDirectory: /_site
    files:
      - "**/*"
  cache:
    paths: []

记住在 Lume _config.ts 文件中忽略 amplify.yml 文件。如果你不想在你的仓库中创建此文件,你可以在 AWS 控制面板中配置它。

Kinsta

Kinsta 是一项主机服务,允许 免费托管多达 100 个静态站点。由于 Kinsta 仅支持 Node,要托管 Lume 站点,你需要创建以下 package.json 文件:

{
  "scripts": {
    "build": "deno task lume"
  },
  "devDependencies": {
    "deno-bin": "^1.37.2"
  }
}

在项目设置中,将构建命令配置为 npm run build,并将发布目录配置为 _site

Kinsta 提供了 这个不错的模板,你可以使用它。

Surge

Surge 是一个基于 CLI 的静态 Web 发布主机,具有无限站点和自定义域名。要将你的站点上传到 Surge,你需要首先使用 npm install --global surge 安装 CLI 工具。然后,在 _dest 目录中,运行 surge 以登录/注册并上传站点。