Azure

How to deploy to Azure Static Web Apps or Azure Functions with Nitro.

Azure Static Web Apps

How to deploy to Azure Static Web Apps with Nitro.

Azure - 图1 Support for serverless SSR build

Azure - 图2 Auto-detected when deploying

Azure - 图3 Minimal configuration required

Setup

Azure Static Web Apps are designed to be deployed continuously in a GitHub Actions workflow. By default, Nitro will detect this deployment environment and enable the azure preset.

Local preview

You can invoke a development environment to preview before deploying.

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

Deploy from CI/CD via GitHub Actions

When you link your GitHub repository to Azure Static Web Apps, a workflow file is added to the repository.

When you are asked to select your framework, select custom and provide the following information:

InputValue
app_location‘/‘
api_location‘.output/server’
output_location‘.output/public’

If you miss this step, you can always find the build configuration section in your workflow and update the build configuration:

.github/workflows/azure-static-web-apps-<RANDOM_NAME>.yml

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

Note

Pending an update in the Azure Static Web Apps workflow, you will also need to run the following in your root directory:

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

That’s it! Now Azure Static Web Apps will automatically deploy your Nitro-powered Nuxt application on push.

Demo

A live demo is available on https://icy-pond-008be3f03.1.azurestaticapps.net/.

Azure Functions

Azure - 图4 Support for serverless SSR build

Azure - 图5 No configuration required

Azure - 图6 Static assets served from Azure Function

Setup

nuxt.config.js|ts

  1. export default {
  2. nitro: {
  3. preset: 'azure-functions'
  4. }
  5. }

If you encounter any issues, please ensure you’re using a Node.js 14+ runtime. You can find more information about how to set the Node version in the Azure docs.

Local preview

Install Azure Functions Core Tools if you want to test locally.

You can invoke a development environment from the serverless directory.

  1. NITRO_PRESET=azure-functions yarn build
  2. cd .output
  3. func start

You can now visit http://localhost:7071/ in your browser and browse your site running locally on Azure Functions.

Deploy from your local machine

To deploy, just run the following command:

  1. # To publish the bundled zip file
  2. az functionapp deployment source config-zip -g <resource-group> -n <app-name> --src dist/deploy.zip
  3. # Alternatively you can publish from source
  4. cd dist && func azure functionapp publish --javascript <app-name>

Deploy from CI/CD via GitHub Actions

First, obtain your Azure Functions Publish Profile and add it as a secret to your GitHub repository settings following these instructions.

Then create the following file as a workflow:

.github/workflows/azure.yml

  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 }}

Optimizing Azure Functions

Consider turning on immutable packages to support running your app from the zip file. This can speed up cold starts.

Demo

A live demo is available on https://nitro-deployment.azurewebsites.net/.