ข้ามไปยังเนื้อหา

Deploy

คุณรู้วิธี deploy แอปพลิเคชัน Next.js อยู่แล้ว: push ไป Vercel, เชื่อมต่อ repo กับ Netlify หรือรัน next build && next start บน server Astro ใช้ topology เดียวกัน — static output ไปยัง CDN ใดก็ได้, SSR output ผ่าน adapter — แต่ build artefact เรียบง่ายกว่าและ deployment surface กว้างกว่ามาก เพราะ static Astro build คือโฟลเดอร์ HTML, CSS และ JS ธรรมดา

Terminal window
# Build โปรเจกต์
npx astro build
# Output อยู่ใน dist/ เสมอ
# Static build: dist/ มี .html, .css, .js, images
# SSR build: dist/ มี server entry point + static assets

โฟลเดอร์ dist/ คือ deployment artefact สำหรับ static sites อัปโหลดตรงๆ; สำหรับ SSR รัน server entry point ผ่าน adapter

.github/workflows/deploy.yml
name: Deploy to GitHub Pages
on:
push:
branches: [main]
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
cache: npm
- run: npm ci
- run: 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

ตั้งค่า site และ base ใน astro.config.mjs ด้วยหาก site อยู่ที่ sub-path (เช่น https://user.github.io/repo/):

astro.config.mjs
export default defineConfig({
site: 'https://yourname.github.io',
base: '/your-repo-name',
});

Netlify ตรวจจับ Astro อัตโนมัติ เพิ่ม netlify.toml สำหรับ config ชัดเจน:

[build]
command = "npm run build"
publish = "dist"

Vercel ตรวจจับ Astro อัตโนมัติเช่นกัน ไม่ต้องมีไฟล์ config สำหรับ static build เส้นทาง zero-config: เชื่อมต่อ repo ใน Vercel dashboard แล้ว push

สำหรับ SSR output ต้องใช้ adapter ติดตั้งด้วย npx astro add <adapter> และตั้ง output: 'server':

Terminal window
npx astro add vercel
astro.config.mjs
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel/serverless';
export default defineConfig({
output: 'server',
adapter: vercel(),
});

Push ไป Vercel — adapter จะ wrap server entry point ใน Vercel Serverless Functions อัตโนมัติ

Terminal window
npx astro add node
astro.config.mjs
import { defineConfig } from 'astro/config';
import node from '@astrojs/node';
export default defineConfig({
output: 'server',
adapter: node({ mode: 'standalone' }),
});
Terminal window
# หลัง build:
node dist/server/entry.mjs

standalone mode สร้าง Node.js HTTP server ที่รันตรงๆ หรือ wrap ใน Docker container ได้

Next.js deploy
// Next.js deployment options
// 1. Vercel (zero-config)
// Push ไป GitHub → เชื่อมต่อกับ Vercel → เสร็จ
// 2. Static export (ไม่มี SSR)
// next.config.js
const nextConfig = { output: 'export' };
// → โฟลเดอร์ out/ deploy ไปยัง static host ใดก็ได้
// 3. Self-hosted Node.js
// next build → next start (port 3000)
// หรือ: node .next/standalone/server.js
// 4. Docker
// FROM node:20-alpine
// COPY .next/standalone ./
// CMD ["node", "server.js"]
Astro deploy
// Astro deployment options
// 1. Static (default) — CDN / static host ใดก็ได้
// astro build → โฟลเดอร์ dist/
// ใช้ได้กับ GitHub Pages, Netlify, Vercel, S3 + CloudFront, ฯลฯ
// 2. Vercel SSR
// npx astro add vercel
// output: 'server' → push → Vercel จัดการทั้งหมด
// 3. Self-hosted Node.js
// npx astro add node
// output: 'server', mode: 'standalone'
// astro build → node dist/server/entry.mjs
// 4. Docker (Node standalone)
// FROM node:20-alpine
// COPY dist ./dist
// CMD ["node", "./dist/server/entry.mjs"]
.github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run check # astro check — type errors ทำให้ build ล้มเหลว
- run: npm run build # astro build — compilation errors ทำให้ build ล้มเหลว

นี่สะท้อน CI pipeline ทั่วไปของ Next.js (tsc --noEmit + next build) — เพียงแต่ชื่อ script ต่างกัน

`astro build` วาง production artefact ไว้ที่ไหน?
ต้องทำอะไรก่อน deploy Astro site ที่มี `output: 'server'` ไปยัง Vercel?
คำสั่งใดใน GitHub Actions CI step จับ type errors ก่อน build?
สำหรับ Node.js standalone SSR deployment ต้องรันไฟล์ใดหลัง `astro build`?