Skip to main content

Deploying Node.js to Azure App Service

You can deploy your Node.js project to Azure App Service as part of your continuous deployment (CD) workflows.

简介

This guide explains how to use GitHub Actions to build, test, and deploy a Node.js project to Azure App Service.

Note: If your GitHub Actions workflows need to access resources from a cloud provider that supports OpenID Connect (OIDC), you can configure your workflows to authenticate directly to the cloud provider. This will let you stop storing these credentials as long-lived secrets and provide other security benefits. For more information, see "About security hardening with OpenID Connect" and "Configuring OpenID Connect in Azure."

基本要求

在创建 GitHub Actions 工作流程之前,首先需要完成以下设置步骤:

  1. 创建 Azure App Service 计划。

    例如,可以使用 Azure CLI 创建新的应用服务计划:

    Shell
    az appservice plan create \
       --resource-group MY_RESOURCE_GROUP \
       --name MY_APP_SERVICE_PLAN \
       --is-linux

    在上述命令中,将 MY_ResourcesURCE_GROUP 替换为您原有的 Azure 资源组,MY_APP_SERVICE_PLAN 替换为应用服务计划的新名称。

    请查看 Azure 文档以了解更多有关使用 Azure CLI 的信息:

  2. 创建 Web 应用。

    For example, you can use the Azure CLI to create an Azure App Service web app with a Node.js runtime:

    Shell
    az webapp create \
        --name MY_WEBAPP_NAME \
        --plan MY_APP_SERVICE_PLAN \
        --resource-group MY_RESOURCE_GROUP \
        --runtime "NODE|14-lts"

    在上面的命令中,将参数替换为您自己的值,其中 MY_WEBAPP_NAME 是 Web 应用的新名称。

  3. 配置 Azure 发布配置文件并创建 AZURE_WEBAPP_PUBLISH_PROFILE 机密。

    使用发布配置文件生成 Azure 部署凭据。 更多信息请参阅 Azure 文档中的“生成部署凭据”。

    在 GitHub 仓库中,创建一个名为 AZURE_WEBAPP_PUBLISH_PROFILE 的机密,其中包含发布配置文件的内容。 有关创建机密的更多信息,请参阅“加密密码”。

  4. Optionally, configure a deployment environment. Environments are used to describe a general deployment target like production, staging, or development. When a GitHub Actions workflow deploys to an environment, the environment is displayed on the main page of the repository. You can use environments to require approval for a job to proceed, restrict which branches can trigger a workflow, or limit access to secrets. For more information about creating environments, see "Using environments for deployment."

创建工作流程

完成先决条件后,可以继续创建工作流程。

The following example workflow demonstrates how to build, test, and deploy the Node.js project to Azure App Service when there is a push to the main branch.

确保在工作流程 env 中将 AZURE_WEBAPP_NAME 密钥设置为您创建的 web 应用程序名称。 If the path to your project is not the repository root, change AZURE_WEBAPP_PACKAGE_PATH to your project path. If you use a version of Node.js other than 10.x, change NODE_VERSION to the version that you use.

If you configured a deployment environment, change the value of environment to be the name of your environment. If you did not configure an environment or if your workflow is in a private repository and you do not use GitHub Enterprise Cloud, delete the environment key.

YAML
# 此工作流使用未经 GitHub 认证的操作。
# 它们由第三方提供,并受
# 单独的服务条款、隐私政策和支持
# 文档管理。

on:
  push:
    branches:
      - main

env:
  AZURE_WEBAPP_NAME: MY_WEBAPP_NAME   # set this to your application's name
  AZURE_WEBAPP_PACKAGE_PATH: '.'      # set this to the path to your web app project, defaults to the repository root
  NODE_VERSION: '14.x'                # set this to the node version to use

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2

    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: ${{ env.NODE_VERSION }}
        cache: 'npm'

    - name: npm install, build, and test
      run: |
        npm install
        npm run build --if-present
        npm run test --if-present
    - name: Upload artifact for deployment job
      uses: actions/upload-artifact@v2
      with:
        name: node-app
        path: .

  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

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

    - name: 'Deploy to Azure WebApp'
      id: deploy-to-webapp 
      uses: azure/webapps-deploy@0b651ed7546ecfc75024011f76944cb9b381ef1e
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
        package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

其他资源

以下资源也可能有用: