Azure

如何将 Nuxt 部署到 Azure 静态 Web 应用或 Azure Functions中

Azure Functions

  • 支持无服务器构建
  • 无需配置
  • Azure Function 提供静态资源服务

设置

```ts [nuxt.config.js|ts] export default { nitro: { preset: ‘azure_functions’ } }

  1. ::: warning 提示
  2. 如果你遇到问题,请确保你使用的是Node.js 14+ 环境。 你可以在Azure文档中找到更多关于[`如何设置Node版本`](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=v2#setting-the-node-version)的信息。
  3. :::
  4. ### 本地预览
  5. 如果你想在本地测试,请安装[Azure Functions Core Tools](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local)。
  6. 您可以从无服务器目录启动开发环境。
  7. ```bash
  8. NITRO_PRESET=azure_functions yarn build
  9. cd .output
  10. func start

你现在可以在浏览器中访问 http://localhost:7071/ 来浏览在 Azure Functions 上本地运行的服务。

本机部署

要部署,只需运行以下命令:

  1. # 发布打包后的压缩文件
  2. az functionapp deployment source config-zip -g <resource-group> -n <app-name> --src dist/deploy.zip
  3. # 或者您也可以发布源代码
  4. cd dist && func azure functionapp publish --javascript <app-name>

通过 GitHub Actions 从 CI/CD 部署

首先,获取您的 Azure Functions 发布配置文件,然后按照这些说明将其作为密钥添加到您的 GitHub 存储库设置中。

然后创建以下文件作为工作流:

  1. name: azure
  2. on:
  3. push:
  4. branches:
  5. - main
  6. pull_request:
  7. branches:
  8. - main
  9. jobs:
  10. deploy:
  11. runs-on: ${{ matrix.os }}
  12. strategy:
  13. matrix:
  14. os: [ ubuntu-latest ]
  15. node: [ 14 ]
  16. steps:
  17. - uses: actions/setup-node@v2
  18. with:
  19. node-version: ${{ matrix.node }}
  20. - name: Checkout
  21. uses: actions/checkout@master
  22. - name: Get yarn cache directory path
  23. id: yarn-cache-dir-path
  24. run: echo "::set-output name=dir::$(yarn cache dir)"
  25. - uses: actions/cache@v2
  26. id: yarn-cache
  27. with:
  28. path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
  29. key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
  30. restore-keys: |
  31. ${{ runner.os }}-yarn-azure
  32. - name: Install Dependencies
  33. if: steps.cache.outputs.cache-hit != 'true'
  34. run: yarn
  35. - name: Build
  36. run: npm run build
  37. env:
  38. NITRO_PRESET: azure_functions
  39. - name: 'Deploy to Azure Functions'
  40. uses: Azure/functions-action@v1
  41. with:
  42. app-name: <your-app-name>
  43. package: .output/deploy.zip
  44. publish-profile: ${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }}

优化 Azure Functions

可以考虑 启用从压缩包运行 ,从而支持直接通过 zip 文件运行您的应用程序。这可以加速冷启动。

例子

https://nuxt-nitro.azurewebsites.net/ 提供了在线演示。

Azure 静态 Web 应用

::: warning 提示 Azure 静态 Web 应用当前环境默认为 Node.js 12.x ,这意味着它与 Nuxt Nitro 不兼容。目前,您可以改为通过 Azure Functions 进行部署。 :::

如何使用 Nuxt Nitro 将 Nuxt 部署到 Azure 静态 Web 应用。

  • 支持无服务器 SSR 构建
  • 部署时自动检测
  • 极少的配置

设置

Azure 静态 Web 应用程序旨在 GitHub Actions workflow中持续部署。 默认情况下,Nitro 将检测此部署环境并启用 azure 预设。

本地预览

您可以在部署前运行开发环境进行预览。

  1. NITRO_PRESET=azure yarn build
  2. npx @azure/static-web-apps-cli start .output/public --api-location .output/server

通过 GitHub Actions 进行 CI/CD 部署

将 GitHub 存储库链接到 Azure 静态 Web 应用时,会向存储库添加一个工作流文件。

当系统要求您选择框架时,请选择自定义并提供以下信息:

Input Value
app_location ‘/‘
api_location ‘.output/server’
output_location ‘.output/public’

如果您错过了这一步,您仍然可以在工作流程中找到构建配置部分并更新构建配置:

  1. ###### Repository/Build Configurations ######
  2. app_location: '/'
  3. api_location: '.output/server'
  4. output_location: '.output/public'
  5. ###### End of Repository/Build Configurations ######

说明

等待 Azure 静态 Web 应用工作流 中的更新,您还需要在根目录中运行以下命令:

  1. mkdir -p .output/server
  2. touch .output/server/.gitkeep
  3. git add -f .output/server/.gitkeep

就是这样!现在,Azure 静态 Web 应用程序将在推送时自动部署到由 Nitro 驱动的 Nuxt 应用程序。