avatar

sunday

Sunday's Blog

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

github actions 自动部署 standalone模式的nextjs

发表于 2024-11-8 更新于 2024-11- 8
作者 sunday 已删除用户
6~8 分钟 阅读

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/scp-action@v0.1.7
        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/scp-action@v0.1.7
        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/scp-action@v0.1.7
        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
许可协议:  CC BY 4.0
分享

相关文章

8月 12, 2025

nextjs15使用ai sdk的一些问题

目前作者使用的版本: "@ai-sdk/openai": "^2.0.10", "@ai-sdk/react": "^2.0.10", "ai": "^5.0.10", "@modelcontextprotocol/sdk": "^1.17.2", "next": "15.4.5", 1.strea

7月 1, 2025

nextjs使用three.js写一个3D模型的查看器Viewer

直接上代码 // components/GLBViewer.js "use client"; import { useEffect, useRef, useState } from "react"; import * as THREE from "three"; import { OrbitCon

5月 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 执行后会

下一篇

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

上一篇

解决cloudflare saas回源不正常问题

最近更新

  • ios18 swiftUI 开发的一些问题
  • Cursor IDE中开发IOS应用——支持热更新
  • nginx + acme 不占用80端口申请证书
  • 免费CDN 阿里云ESA 加速国内网站
  • nextjs15使用ai sdk的一些问题

热门标签

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

目录

©2025 sunday. 保留部分权利。

使用 Halo 主题 Chirpy