Skip to main content

此版本的 GitHub Enterprise 已停止服务 2022-10-12. 即使针对重大安全问题,也不会发布补丁。 为了获得更好的性能、更高的安全性和新功能,请升级到最新版本的 GitHub Enterprise。 如需升级帮助,请联系 GitHub Enterprise 支持

Deploying PHP to Azure App Service

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

注意:GitHub Enterprise Server 目前不支持 GitHub 托管的运行器。 可以在 GitHub public roadmap 上查看有关未来支持计划的更多信息。

Introduction

This guide explains how to use GitHub Actions to build and deploy a PHP project to Azure App Service.

Prerequisites

Before creating your GitHub Actions workflow, you will first need to complete the following setup steps:

  1. 创建 Azure 应用服务计划。

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

    Shell
    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 文档:

  2. Create a web app.

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

    Shell
    az webapp create \
        --name MY_WEBAPP_NAME \
        --plan MY_APP_SERVICE_PLAN \
        --resource-group MY_RESOURCE_GROUP \
        --runtime "php|7.4"

    In the command above, replace the parameters with your own values, where MY_WEBAPP_NAME is a new name for the web app.

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

    使用发布配置文件生成 Azure 部署凭据。 有关详细信息,请参阅 Azure 文档中的“生成部署凭据”。

    在 GitHub 存储库中,创建一个名为 AZURE_WEBAPP_PUBLISH_PROFILE 的机密,其中包含发布配置文件的内容。 要详细了解如何创建机密,请参阅“� 密的机密”。

  4. Optionally, configure a deployment environment. 环境用于描述常规部署目� �,例如 productionstagingdevelopment。 当 GitHub Actions 工作流部署到某个环境时,该环境将显示在存储库的主页上。 � 可以使用环境来要求批准才能继续作业,限制哪些分支可以触发工作流,或限制对机密的访问。 有关创建环境的详细信息,请参阅“使用环境进行部署”。

Creating the workflow

Once you've completed the prerequisites, you can proceed with creating the workflow.

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

Ensure that you set AZURE_WEBAPP_NAME in the workflow env key to the name of the web app you created. If the path to your project is not the repository root, change AZURE_WEBAPP_PACKAGE_PATH to the path to your project. If you use a version of PHP other than 8.x, changePHP_VERSION to the version that you use.

如果配置了部署环境,请将 environment 的值更改为环境的名称。 如果未配置环境 ,请� 除 environment 密钥。

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

# GitHub 建议将操作固定到提交 SHA。
# 若要获取较新版本,需要更新 SHA。
# 还可以引用� �记或分支,但该操作可能会更改而不发出警告。

name: Build and deploy PHP app to Azure Web App

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
  PHP_VERSION: '8.x'                  # set this to the PHP version to use

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ env.PHP_VERSION }}

      - name: Check if composer.json exists
        id: check_files
        uses: andstor/file-existence-action@v1
        with:
          files: 'composer.json'

      - name: Get Composer Cache Directory
        id: composer-cache
        if: steps.check_files.outputs.files_exists == 'true'
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: Set up dependency caching for faster installs
        uses: actions/cache@v2
        if: steps.check_files.outputs.files_exists == 'true'
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-

      - name: Run composer install if composer.json exists
        if: steps.check_files.outputs.files_exists == 'true'
        run: composer validate --no-check-publish && composer install --prefer-dist --no-progress

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v2
        with:
          name: php-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: php-app

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

Additional resources

The following resources may also be useful: