注: GitHub 托管的运行器目前在 GitHub Enterprise Server 上不受支持。 您可以在 GitHub 公共路线图 上查看有关未来支持计划的更多信息。
简介
This guide explains how to use GitHub Actions to build and deploy a Python project to Azure App Service.
基本要求
在创建 GitHub Actions 工作流程之前,首先需要完成以下设置步骤:
-
创建 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 的信息:
- For authentication, see "Sign in with Azure CLI."
- 如果需要创建新的资源组,请参阅“az 组”。
-
创建 Web 应用。
For example, you can use the Azure CLI to create an Azure App Service web app with a Python runtime:
Shell az webapp create \ --name MY_WEBAPP_NAME \ --plan MY_APP_SERVICE_PLAN \ --resource-group MY_RESOURCE_GROUP \ --runtime "python|3.8"在上面的命令中,将参数替换为您自己的值,其中
MY_WEBAPP_NAME是 Web 应用的新名称。 -
配置 Azure 发布配置文件并创建
AZURE_WEBAPP_PUBLISH_PROFILE机密。使用发布配置文件生成 Azure 部署凭据。 更多信息请参阅 Azure 文档中的“生成部署凭据”。
在 GitHub 仓库中,创建一个名为
AZURE_WEBAPP_PUBLISH_PROFILE的机密,其中包含发布配置文件的内容。 有关创建机密的更多信息,请参阅“� 密密� �”。 -
Add an app setting called
SCM_DO_BUILD_DURING_DEPLOYMENTand set the value to1.
创建工作流程
完成先决条件后,可以继续创建工作流程。
The following example workflow demonstrates how to build and deploy a Python project to Azure App Service when there is a push to the main branch.
确保在工作流程 env 中将 AZURE_WEBAPP_NAME 密钥设置为您创建的 web 应用程序名称。 If you use a version of Python other than 3.8, change PYTHON_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, delete the environment key.
# 此工作流使用未经 GitHub 认证的操作。 # 它们由第三方提供,并受 # 单独的服务条款、隐私政策和支持 # 文档管理。 name: Build and deploy Python app to Azure Web App env: AZURE_WEBAPP_NAME: MY_WEBAPP_NAME # set this to your application's name PYTHON_VERSION: '3.8' # set this to the Python version to use on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python version uses: actions/setup-python@v2.2.2 with: python-version: ${{ env.PYTHON_VERSION }} - name: Create and start virtual environment run: | python -m venv venv source venv/bin/activate - name: Set up dependency caching for faster installs uses: actions/cache@v2 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} restore-keys: | ${{ runner.os }}-pip- - name: Install dependencies run: pip install -r requirements.txt # Optional: Add a step to run tests here (PyTest, Django test suites, etc.) - name: Upload artifact for deployment jobs uses: actions/upload-artifact@v2 with: name: python-app path: | . !venv/ 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: python-app path: . - 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 }}其他资源
以下资源也可能有用:
- For the original starter workflow, see
azure-webapps-python.ymlin the GitHub Actionsstarter-workflowsrepository. - 用于部署 Web 应用的操作是正式的 Azure
Azure/webapps-deploy操作。 - For more examples of GitHub Action workflows that deploy to Azure, see the actions-workflow-samples repository.