You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Pipeline部署NextJS至EC2时build文件夹未上传问题

NextJS应用通过AWS CodePipeline部署时,build文件夹未同步到EC2实例

我使用NextJS开发前端应用,代码托管在GitHub,通过AWS CodePipeline(GitHub→CodeBuild→CodeDeploy)将应用部署至Ubuntu系统的EC2实例。目前流水线的构建、部署步骤均显示成功,但CodeBuild阶段生成的build文件夹并未被部署到EC2,导致服务无法启动。

以下是相关配置文件:

buildspec.yml

version: 0.2

phases:

  install:
    runtime-versions:
      nodejs: 20
    commands:
      - echo "📦 Installing packages..."
      - npm install
      - echo "✅ Packages installed successfully; Current repository:"
      - ls -al

  build:
    commands:
      - echo "🚧 Starting building packages..."
      - npm run build

  post_build:
    commands:
      - echo "✅ Built successfully"
      - echo `date`
      - echo `Repository after build:`
      - ls -al

artifacts:
  files:
    - '**/*'
  discard-paths: no

appspec.yml

version: 0.0
os: linux

files:
  - source: /
    destination: /home/ubuntu/example

file_exists_behavior: OVERWRITE

hooks:
  ApplicationStart:
    - location: deploy/application_start.sh
      timeout: 300
      runas: root

next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  distDir: 'build'
};

module.exports = nextConfig;

application_start.sh

#!/bin/bash

cd /home/ubuntu/example

pm2 stop example_server
pm2 delete example_server
pm2 start npm --name "example_server" -- start

构建日志说明

构建日志显示,CodeBuild的post_build阶段执行ls -al时,build文件夹已确认生成。


内容的提问来源于stack exchange,提问作者nikita_trifan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 17:43:13