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

GitHub Actions上传Next.js构建产物提示‘无文件路径’问题

问题

Next.js构建步骤执行正常,但在GitHub Actions流水线中上传.next/文件夹为artifact时,系统提示未找到指定路径的文件。

工作流配置

name: Deploy Pipeline
run-name: Deploy:${{ github.ref_name }} by @${{ github.actor }}

on:
  push:
    branches:
      - main
      - develop

jobs:
  build:
    name: Build Next.js
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '22.12.0'
      - name: Install pnpm
        run: npm install -g pnpm@9.15.0
      - name: Cache Dependencies
        uses: actions/cache@v4
        id: pnpm-cache
        with:
          path: |
            ~/.pnpm-store
            node_modules
          key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-
      - name: Install Dependencies
        run: pnpm install --frozen-lockfile
      - name: Build Next.js
        run: pnpm run build
        env:
          NODE_ENV: production
      - name: Upload Build Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: next-build
          path: .next/
          if-no-files-found: error

  deploy:
    name: Deploy to Cloudflare Pages
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
      - name: Download Build Artifacts
        uses: actions/download-artifact@v4
        with:
          name: next-build
      - name: Deploy to Cloudflare Pages
        uses: cloudflare/pages-action@v1
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          projectName: my-project-name
          directory: .next/
          gitHubToken: ${{ secrets.GITHUB_TOKEN }}

错误信息

Run actions/upload-artifact@v4
  with:
    name: next-build
    path: .next/
    if-no-files-found: error
    compression-level: 6
    overwrite: false
    include-hidden-files: false
Error: No files were found with the provided path: .next/. No artifacts will be uploaded.

已尝试使用${{ github.workspace }}/.next绝对路径、./.next相对路径,均无效;上传步骤前通过ls -la .next/确认文件夹及文件存在,参考教程步骤仍无法解决。

解决方案

以下是几个可能的原因和对应的解决办法:

  • 确认.next文件夹的实际生成位置
    部分Next.js项目(如monorepo结构)或通过next.config.js中distDir配置修改了构建输出目录。可在Build Next.js步骤后添加pwd && find . -name ".next" -type d命令,获取.next的绝对路径,上传步骤中使用该完整路径。

  • 调整上传路径的写法
    尝试将路径改为.next/**,匹配文件夹下所有文件和子目录;或使用.next(不带末尾斜杠),actions/upload-artifact会自动处理整个文件夹的上传。

  • 开启隐藏文件包含配置
    .next文件夹包含隐藏文件(如.next/BUILD_ID),当前上传步骤include-hidden-files设为false可能导致无法识别文件夹。显式设置include-hidden-files: true,确保隐藏文件被纳入检测范围。

  • 临时禁用缓存验证
    缓存依赖可能引发冲突,临时注释Cache Dependencies步骤,重新运行流水线,检查是否能正常生成并上传.next文件夹。

  • 检查pnpm构建命令的正确性
    确认package.json中scripts的build字段为"build": "next build",无额外路径参数或输出目录修改配置,确保pnpm run build确实执行了标准的Next.js构建流程。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 14:05:10