将nextjs部署到azure,但容器一直失败

问题描述 投票:0回答:1

我正在尝试将我的 nextjs 应用程序部署到 azure web 应用程序,但无法 ping 通端口 8080,因此该网站无法工作。

我尝试在 azure env 和 package.json 中定义端口 8080,但这不起作用,我也尝试过自定义 docker 文件,但没有成功。我使用 github yaml 进行部署。我已经通过将 website_ports 设置为 8080 尝试了 azure 的环境,但没有成功。

这是我的 yaml 文件。

`名称:部署到 Azure 应用服务

在: 推: 分支机构: - 主要的 工作流程_调度:

环境: AZURE_WEBAPP_NAME:watkanikladen

工作: 建造: 运行:ubuntu-latest

steps:
  - uses: actions/checkout@v4

  - name: Setup Node.js
    uses: actions/setup-node@v4
    with:
      node-version: 20
      cache: "npm"

  - name: npm install and build
    run: |
      npm install --legacy-peer-deps
      npm run build

  - name: Copy static files
    run: |
      cp -R ./public ./.next/standalone/public
      cp -R ./.next/static ./.next/standalone/.next/static

  - name: Upload artifact for deployment job
    uses: actions/upload-artifact@v4
    with:
      name: app
      path: ./.next/standalone

部署: 运行:ubuntu-latest 需求:构建

environment:
  name: production
  url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

steps:
  - name: Download artifact from build job
    uses: actions/download-artifact@v4
    with:
      name: app

  - name: Deploy to Azure Web App
    id: deploy-to-webapp
    uses: azure/webapps-deploy@v3
    with:
      app-name: ${{ env.AZURE_WEBAPP_NAME }}
      publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
      package: .`

这是我的package.json

`{ “名称”:“测试天蓝色”, “版本”:“0.1.0”, “私人”:真实, “脚本”:{ "dev": "下一个 dev -p 8080", “构建”:“下一个构建”, "start": "下次开始-p 8080", "lint": "下一个 lint" }, “依赖项”:{ “反应”:“^19.0.0”, "react-dom": "^19.0.0", “下一个”:“15.1.0” }, “开发依赖项”:{ “打字稿”:“^5”, "@types/node": "^20", “@types/react”:“^19”, "@types/react-dom": "^19", "postcss": "^8", "tailwindcss": "^3.4.1", “eslint”:“^9”, “eslint-config-next”:“15.1.0”, “@eslint/eslintrc”:“^3” } }

`

reactjs azure docker next.js azure-web-app-service
1个回答
0
投票

我创建了一个示例 Next.js 应用程序,并使用 GitHub Actions 将其部署到 Azure Web 应用程序。

我也遇到了同样的问题。要解决此问题,请按照以下步骤操作。

  • 我将以下启动脚本添加到
    package.json
    文件中。
 "start": "node_modules/next/dist/bin/next start",

package.json:

{
  "name": "my-nextjs-app",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev --turbopack",
    "build": "next build",
    "start": "node_modules/next/dist/bin/next start",
    "lint": "next lint"
  },
  "dependencies": {
    "react": "^19.0.0",
    "react-dom": "^19.0.0",
    "next": "15.1.0"
  }
}
  • 并使用以下行更新了 GitHub 工作流程文件。
 - name: Copy static files
       run: |
         cp -R ./.next/static ./.next/standalone/.next/static
     - name: Upload artifact for deployment job
       uses: actions/upload-artifact@v4
       with:
         name: node-app
         path: .next/standalone
         include-hidden-files: true

工作流程文件

name: Build and deploy Node.js app to Azure Web App - kanextjsapp
on:
  push:
    branches:
      - main
  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Node.js version
        uses: actions/setup-node@v3
        with:
          node-version: '18.x'
      - name: npm install, build
        run: |
          npm install
          npm run build --if-present
      - name: Copy static files
        run: |
          cp -R ./.next/static ./.next/standalone/.next/static
      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v4
        with:
          name: node-app
          path: .next/standalone
          include-hidden-files: true
  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    permissions:
      id-token: write 
    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v4
        with:
          name: node-app
      - name: Login to Azure
        uses: azure/login@v2
        with:
          client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_6E6CEBFFAA06478480E9EA659E2F0064 }}
          tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_4FD3D0706EE046BEAFB4D4A12C00A56C }}
          subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_B0E79F48BB934FABB2AE9329442FFBE8 }}
      - name: 'Deploy to Azure Web App'
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v3
        with:
          app-name: 'kanextjsapp'
          slot-name: 'Production'
          package: .         

进行上述两项更改后,我成功地将应用程序部署到Azure。

Azure 输出

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.