使用 Deno 部署你的 Astro 站点
你可以使用 Deno 部署静态或按需渲染的 Astro 站点,无论是在你自己的服务器上,还是部署到 Deno 部署,这是一个在边缘运行 JavaScript、TypeScript 和 WebAssembly 的分布式系统,遍布全球。
¥You can deploy a static or on-demand rendered Astro site using Deno, either on your own server, or to Deno Deploy, a distributed system that runs JavaScript, TypeScript, and WebAssembly at the edge, worldwide.
本指南包括使用 Deno 在你自己的服务器上运行 Astro 站点以及通过 GitHub Actions 或 Deno Deploy CLI 部署到 Deno Deploy 的说明。
¥This guide includes instructions for running your Astro site on your own server with Deno, and deploying to Deno Deploy through GitHub Actions or the Deno Deploy CLI.
要求
标题部分 要求¥Requirements
本指南假设你已经安装了 Deno。
¥This guide assumes you already have Deno installed.
项目配置
标题部分 项目配置¥Project Configuration
你的 Astro 项目可以部署为静态站点,也可以部署为按需渲染的站点。
¥Your Astro project can be deployed as a static site, or as an on-demand rendered site.
静态站点
标题部分 静态站点¥Static Site
默认情况下,你的 Astro 项目是静态站点。你不需要任何额外的配置来使用 Deno 部署静态 Astro 站点或 Deno Deploy。
¥Your Astro project is a static site by default. You don’t need any extra configuration to deploy a static Astro site with Deno, or to Deno Deploy.
按需渲染适配器
标题部分 按需渲染适配器¥Adapter for on-demand rendering
要使用 Deno 在你的 Astro 项目中启用按需渲染,并在 Deno Deploy 上部署:
¥To enable on-demand rendering in your Astro project using Deno, and to deploy on Deno Deploy:
-
Install the
@deno/astro-adapter
adapter to your project’s dependencies using your preferred package manager:终端窗口 npm install @deno/astro-adapter终端窗口 pnpm install @deno/astro-adapter终端窗口 yarn add @deno/astro-adapter -
Update your
astro.config.mjs
project configuration file with the changes below.astro.config.mjs import { defineConfig } from 'astro/config';import deno from '@deno/astro-adapter';export default defineConfig({output: 'server',adapter: deno(),}); -
Update your
preview
script inpackage.json
with the change below.package.json {// ..."scripts": {"dev": "astro dev","start": "astro dev","build": "astro build","preview": "astro preview""preview": "deno run --allow-net --allow-read --allow-env ./dist/server/entry.mjs"}}You can now use this command to preview your production Astro site locally with Deno.
终端窗口 npm run preview终端窗口 pnpm run preview终端窗口 yarn run preview
如何部署
标题部分 如何部署¥How to deploy
你可以在自己的服务器上运行 Astro 站点,也可以通过 GitHub Actions 或使用 Deno Deploy 的 CLI(命令行接口)部署到 Deno Deploy。
¥You can run your Astro site on your own server, or deploy to Deno Deploy through GitHub Actions or using Deno Deploy’s CLI (command line interface).
在你自己的服务器上
标题部分 在你自己的服务器上¥On your own server
-
Copy your project onto your server.
-
Install the project dependencies using your preferred package manager:
终端窗口 npm install终端窗口 pnpm install终端窗口 yarn -
Build your Astro site with your preferred package manager:
终端窗口 npm run build终端窗口 pnpm run build终端窗口 yarn run build -
Start your application with the following command:
终端窗口 deno run -A jsr:@std/http/file-server dist终端窗口 deno run -A ./dist/server/entry.mjs
GitHub Actions 部署
标题部分 GitHub Actions 部署¥GitHub Actions Deployment
如果你的项目存储在 GitHub 上,Deno 部署网站 将指导你设置 GitHub Actions 来部署你的 Astro 站点。
¥If your project is stored on GitHub, the Deno Deploy website will guide you through setting up GitHub Actions to deploy your Astro site.
-
Push your code to a public or private GitHub repository.
-
Sign in on Deno Deploy with your GitHub account, and click on New Project.
-
Select your repository, the branch you want to deploy from, and select GitHub Action mode. (Your Astro site requires a build step, and cannot use Automatic mode.)
-
In your Astro project, create a new file at
.github/workflows/deploy.yml
and paste in the YAML below. This is similar to the YAML given by Deno Deploy, with the additional steps needed for your Astro site..github/workflows/deploy.yml name: Deployon: [push]jobs:deploy:name: Deployruns-on: ubuntu-latestpermissions:id-token: write # Needed for auth with Deno Deploycontents: read # Needed to clone the repositorysteps:- name: Clone repositoryuses: actions/checkout@v4# Not using npm? Change `npm ci` to `yarn install` or `pnpm i`- name: Install dependenciesrun: npm ci# Not using npm? Change `npm run build` to `yarn build` or `pnpm run build`- name: Build Astrorun: npm run build- name: Upload to Deno Deployuses: denoland/deployctl@v1with:project: my-deno-project # TODO: replace with Deno Deploy project nameentrypoint: jsr:@std/http/file-serverroot: dist.github/workflows/deploy.yml name: Deployon: [push]jobs:deploy:name: Deployruns-on: ubuntu-latestpermissions:id-token: write # Needed for auth with Deno Deploycontents: read # Needed to clone the repositorysteps:- name: Clone repositoryuses: actions/checkout@v4# Not using npm? Change `npm ci` to `yarn install` or `pnpm i`- name: Install dependenciesrun: npm ci# Not using npm? Change `npm run build` to `yarn build` or `pnpm run build`- name: Build Astrorun: npm run build- name: Upload to Deno Deployuses: denoland/deployctl@v1with:project: my-deno-project # TODO: replace with Deno Deploy project nameentrypoint: dist/server/entry.mjs -
After committing this YAML file, and pushing to GitHub on your configured deploy branch, the deploy should begin automatically!
You can track the progress using the “Actions” tab on your GitHub repository page, or on Deno Deploy.
CLI 部署
标题部分 CLI 部署¥CLI Deployment
-
Install the Deno Deploy CLI.
终端窗口 deno install -gArf jsr:@deno/deployctl -
Build your Astro site with your preferred package manager:
终端窗口 npm run build终端窗口 pnpm run build终端窗口 yarn run build -
Run
deployctl
to deploy!终端窗口 cd dist && deployctl deploy jsr:@std/http/file-server终端窗口 deployctl deploy ./dist/server/entry.mjsYou can track all your deploys on Deno Deploy.
-
(Optional) To simplify the build and deploy into one command, add a
deploy-deno
script inpackage.json
.package.json {// ..."scripts": {"dev": "astro dev","start": "astro dev","build": "astro build","preview": "astro preview","deno-deploy": "npm run build && cd dist && deployctl deploy jsr:@std/http/file-server"}}package.json {// ..."scripts": {"dev": "astro dev","start": "astro dev","build": "astro build","preview": "deno run --allow-net --allow-read --allow-env ./dist/server/entry.mjs","deno-deploy": "npm run build && deployctl deploy ./dist/server/entry.mjs"}}Then you can use this command to build and deploy your Astro site in one step.
终端窗口 npm run deno-deploy