avatar

sunday

Sunday's Blog

  • 首页
Home github actions 自动部署 standalone模式的nextjs
文章

github actions 自动部署 standalone模式的nextjs

Posted 2024-11-8 Updated 2024-11- 8
By sunday
6~8 min read

1.在next.config.js中开启standalone

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: 'standalone',
}

module.exports = nextConfig

2.编写github actions 的deploy.yml

创建.github/workflows/deploy.yml 文件(.github前面有个点)

name: 部署

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: 检出代码
        uses: actions/checkout@main

      - name: 设置node版本
        uses: actions/setup-node@v4
        with:
          node-version: "22"

      - name: 安装pnpm
        run: npm install pnpm -g

      - name: 安装依赖
        run: pnpm install
      
      - name: 生成prisma client
        run: npx prisma generate  

      - name: Build 打包
        run: npm run build

      - name: cp to Server 拷贝standalone里的所有文件
        uses: appleboy/[email protected]
        with:
          host: "126.11.31.123"
          username: root
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          port: 22
          source: ".next/standalone/"
          target: "/root/test/"
          overwrite: true
          strip_components: 2  #这里填写2是去掉前缀不然会把 .next/standalone/ 这两个文件夹一起拷贝过去的

      - name: cp to Server 因为上一步会把standalone里的.next文件夹拷贝过来,而项目的static文件夹需要放在.next文件里
        uses: appleboy/[email protected]
        with:
          host: "126.11.31.123"
          username: root
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          port: 22
          source: ".next/static"
          target: "/root/test/.next/"
          overwrite: true
          strip_components: 1 #这里填写1会把static(包含文件夹)放在 /root/test/.next/  里

      - name: cp to Server 把项目根目录的public和pm2.json拷贝到/root/test里
        uses: appleboy/[email protected]
        with:
          host: "126.11.31.123"
          username: root
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          port: 22
          source: "public,pm2.json"
          target: "/root/test/"
          overwrite: true

      - name: 部署脚本
        uses: appleboy/ssh-action@master
        with:
          host: "126.11.31.123"
          username: root
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          port: 22
          #注意要带--env production不然pm2.json中定义的环境变量不生效
          script: "cd /root/test && pm2 restart pm2.json --env production" 

3.编写pm2启动文件pm2.json

{
  "apps": [
    {
      "name": "app",
      "script": "./server.js",
      "exec_mode": "cluster_mode",
      "max_memory_restart": "1000M",
      "instances": 2,
      "env_production": {
        "NODE_ENV": "production",
        "PORT": 3000, # 监听的端口
        "HOSTNAME": "0.0.0.0"
      }
    }
  ]
}

nextjs, 软件使用
nextjs 软件使用
License:  CC BY 4.0
Share

Further Reading

May 3, 2025

nextjs15使用better-sqlite3的连接报错问题

1.出现如下错误 ⨯ Error: Could not locate the bindings file. Tried: 解决方法 我是使用pnpm包管理器,执行以下操作 首先安装node-gyp pnpm i node-gyp -D 然后执行 pnpm approve-builds 执行后会

Apr 6, 2025

nextjs + clerk + supabase + realtime 实时监听数据库更改

1.开启realtime.messages策略 在supabase的SQL Editor执行以下命令 create policy "Enable all access for authenticated users" on "public"."messages" as PERMISSIVE for

Mar 27, 2025

解决nextjs15使用useLocalStorage报错的问题

已经在组件中使用"use client"声明了,还是报错,错误如下: Unhandled Runtime Error Error: useLocalStorage is a client-only hook 1.解决方案1-使用 dynamic 导入 import dynamic from 'nex

OLDER

nginx设置多个允许跨域的请求源

NEWER

解决cloudflare saas回源不正常问题

Recently Updated

  • nextjs15使用better-sqlite3的连接报错问题
  • nextjs + clerk + supabase + realtime 实时监听数据库更改
  • 解决nextjs15使用useLocalStorage报错的问题
  • mac上使用nodejs appium控制chrome浏览器
  • 2024年终总结

Trending Tags

nginx acme 强制跳转HTTPS nodejs 代理 mac 神器 vue3 工具 docker

Contents

©2025 sunday. Some rights reserved.

Using the Halo theme Chirpy