先决条件
在创建 GitHub Actions 工作流程之前,首先需要完成以下设置步骤:
- 
创建 Azure 应用服务计划。 例如,可以使用 Azure CLI 创建新的应用服务计划: Bash az appservice plan create \ --resource-group MY_RESOURCE_GROUP \ --name MY_APP_SERVICE_PLAN \ --is-linux az appservice plan create \ --resource-group MY_RESOURCE_GROUP \ --name MY_APP_SERVICE_PLAN \ --is-linux在上述命令中,将 MY_RESOURCE_GROUP替换为你原有的 Azure 资源组,将MY_APP_SERVICE_PLAN替换为应用服务计划的新名称。有关使用 Azure CLI 的详细信息,请参阅 Azure 文档: - 有关身份验证,请参阅“使用 Azure CLI 登录”。
- 如果需要创建新的资源组,请参阅“az group”。
 
- 
创建 Web 应用。 例如,可以使用 Azure CLI 创建具有 Node.js 运行时的 Azure App Service Web 应用: Bash az webapp create \ --name MY_WEBAPP_NAME \ --plan MY_APP_SERVICE_PLAN \ --resource-group MY_RESOURCE_GROUP \ --runtime "NODE|14-lts"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 应用的新名称。
- 
配置 Azure 发布配置文件并创建 AZURE_WEBAPP_PUBLISH_PROFILE机密。使用发布配置文件生成 Azure 部署凭据。 有关详细信息,请参阅 Azure 文档中的“生成部署凭据”。 在 GitHub 存储库中,创建一个名为 AZURE_WEBAPP_PUBLISH_PROFILE的机密,其中包含发布配置文件的内容。 有关创建机密的详细信息,请参阅“在 GitHub Actions 中使用机密”。
- 
Optionally, configure a deployment environment. 环境用于描述常规部署目标,例如 production、staging或development。 当 GitHub Actions 工作流部署到某个环境时,该环境将显示在存储库的主页上。 可以使用环境来要求审批作业以继续,限制可以触发工作流的分支,使用自定义部署保护规则控制部署,或限制对机密的访问权限。 有关创建环境的详细信息,请参阅“管理部署环境”。
创建工作流程
完成先决条件后,可以继续创建工作流程。
以下示例工作流演程示在推送到 main 分支时,如何构建、测试 Node.js 项目并将其部署到 Azure 应用服务。
确保在工作流 env 中将 AZURE_WEBAPP_NAME 密钥设置为创建的 Web 应用的名称。 如果项目的路径不是仓库根路径,请将 AZURE_WEBAPP_PACKAGE_PATH 更改为你的项目路径。 如果使用的 Node.js 版本不是 10.x,请将 NODE_VERSION 更改为你使用的版本。
如果配置了部署环境,请将 environment 的值更改为环境的名称。 如果未配置环境 ,或者如果工作流位于专用存储库中并且你未使用 GitHub Enterprise Cloud,请删除 environment 密钥。
# 此工作流使用未经 GitHub 认证的操作。
# 它们由第三方提供,并受
# 单独的服务条款、隐私政策和支持
# 文档。
# GitHub 建议将操作固定到提交 SHA。
# 若要获取较新版本,需要更新 SHA。
# 还可以引用标记或分支,但该操作可能会更改而不发出警告。
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@v5
    - name: Set up Node.js
      uses: actions/setup-node@v4
      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@v4
      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@v5
      with:
        name: node-app
    - name: 'Deploy to Azure WebApp'
      id: deploy-to-webapp
      uses: azure/webapps-deploy@85270a1854658d167ab239bce43949edb336fa7c
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
        package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
# 此工作流使用未经 GitHub 认证的操作。
# 它们由第三方提供,并受
# 单独的服务条款、隐私政策和支持
# 文档。
# GitHub 建议将操作固定到提交 SHA。
# 若要获取较新版本,需要更新 SHA。
# 还可以引用标记或分支,但该操作可能会更改而不发出警告。
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@v5
    - name: Set up Node.js
      uses: actions/setup-node@v4
      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@v4
      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@v5
      with:
        name: node-app
    - name: 'Deploy to Azure WebApp'
      id: deploy-to-webapp
      uses: azure/webapps-deploy@85270a1854658d167ab239bce43949edb336fa7c
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
        package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
其他阅读材料
- 对于原始工作流模板,请参阅 GitHub Actions starter-workflows存储库中的azure-webapps-node.yml。
- 用于部署 Web 应用的操作是官方 Azure Azure/webapps-deploy操作。
- 有关部署到 Azure 的 GitHub 操作工作流的更多示例,请参阅 actions-workflow-samples 存储库。
- Azure Web 应用文档中的“在 Azure 中创建 Node.js Web 应用”快速入门演示了如何将 Visual Studio Code 与 Azure 应用服务扩展配合使用。