Introduction
This guide explains how to use GitHub Actions to build and deploy a Python project to Azure App Service.
Hinweis
Wenn deine GitHub Actions-Workflows auf Ressourcen eines Cloudanbieters zugreifen müssen, der OpenID Connect (OIDC) unterstützt, kannst du deine Workflows so konfigurieren, dass die Authentifizierung direkt beim Cloudanbieter erfolgt. Dadurch musst du diese Anmeldeinformationen nicht mehr als langlebige Geheimnisse speichern und profitierst zudem von weiteren Sicherheitsvorteilen. Weitere Informationen finden Sie unter Informationen zur Sicherheitshärtung mit OpenID Connect. and Configuring OpenID Connect in Azure.
Prerequisites
Before creating your GitHub Actions workflow, you will first need to complete the following setup steps:
-
Erstellen eines Azure App Service-Plans
Du kannst beispielsweise die Azure CLI verwenden, um einen neuen App Service-Plan zu erstellen:
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
Ersetze
MY_RESOURCE_GROUP
im obigen Befehl durch deine bereits vorhandene Azure-Ressourcengruppe undMY_APP_SERVICE_PLAN
durch einen neuen Namen für den App Service-Plan.Weitere Informationen zur Verwendung der Azure-Befehlszeilenschnittstelle findest du in der Azure-Dokumentation:
- Informationen zur Authentifizierung findest du unter Anmelden mit Azure CLI.
- Informationen zu Erstellen einer neuen Ressourcengruppe findest du unter az group.
-
Create a web app.
For example, you can use the Azure CLI to create an Azure App Service web app with a Python runtime:
Bash az webapp create \ --name MY_WEBAPP_NAME \ --plan MY_APP_SERVICE_PLAN \ --resource-group MY_RESOURCE_GROUP \ --runtime "python|3.8"
az webapp create \ --name MY_WEBAPP_NAME \ --plan MY_APP_SERVICE_PLAN \ --resource-group MY_RESOURCE_GROUP \ --runtime "python|3.8"
In the command above, replace the parameters with your own values, where
MY_WEBAPP_NAME
is a new name for the web app. -
Konfiguriere ein Azure-Veröffentlichungsprofil, und erstelle ein
AZURE_WEBAPP_PUBLISH_PROFILE
-Geheimnis.Generiere Anmeldeinformationen für deine Azure-Bereitstellung mithilfe eines Veröffentlichungsprofils. Weitere Informationen findest du unter Generieren von Anmeldeinformationen für die Bereitstellung in der Azure-Dokumentation.
Erstelle in deinem GitHub-Repository ein Geheimnis namens
AZURE_WEBAPP_PUBLISH_PROFILE
, das den Inhalt des Veröffentlichungsprofils enthält. Weitere Informationen zum Erstellen von Geheimnissen findest du unter Using secrets in GitHub Actions. -
Add an app setting called
SCM_DO_BUILD_DURING_DEPLOYMENT
and set the value to1
. -
Optionally, configure a deployment environment. Umgebungen werden verwendet, um ein allgemeines Bereitstellungsziel wie
production
,staging
oderdevelopment
zu beschreiben. Wenn ein GitHub Actions-Workflow in einer Umgebung bereitgestellt wird, wird die Umgebung auf der Hauptseite des Repositorys angezeigt. Du kannst Umgebungen verwenden, um für die Fortsetzung von Aufträgen eine Genehmigung zu erzwingen, einzuschränken, welche Branches einen Workflow auslösen können, Bereitstellungen mit benutzerdefinierten Bereitstellungsschutzregeln zu schützen oder den Zugriff auf Geheimnisse zu beschränken. Weitere Informationen zum Erstellen von Umgebungen findest du unter Managing environments for deployment.
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 Python 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 you use a version of Python other than 3.8
, change PYTHON_VERSION
to the version that you use.
Wenn du eine Bereitstellungsumgebung konfiguriert hast, ändere den Wert environment
in den Namen deiner Umgebung. Wenn du keine Umgebung konfiguriert hast oder wenn sich dein Workflow in einem privaten Repository befindet und du GitHub Enterprise Cloud nicht verwendest, lösche den environment
-Schlüssel.
# Dieser Workflow verwendet Aktionen, die nicht von GitHub zertifiziert sind. # Sie werden von einem Drittanbieter bereitgestellt und unterliegen # separaten Nutzungsbedingungen, Datenschutzbestimmungen und Support # Onlinedokumentation. # GitHub empfiehlt, Aktionen an einen Commit-SHA anzuheften. # Um eine neuere Version zu erhalten, musst du den SHA aktualisieren. # Du kannst auch auf ein Tag oder einen Branch verweisen, aber die Aktion kann sich ohne Vorwarnung ändern. 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@v4 - name: Set up Python version uses: actions/setup-python@v5 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@v4 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@v4 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@v4 with: name: python-app path: . - name: 'Deploy to Azure Web App' id: deploy-to-webapp uses: azure/webapps-deploy@85270a1854658d167ab239bce43949edb336fa7c with: app-name: ${{ env.AZURE_WEBAPP_NAME }} publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
# Dieser Workflow verwendet Aktionen, die nicht von GitHub zertifiziert sind.
# Sie werden von einem Drittanbieter bereitgestellt und unterliegen
# separaten Nutzungsbedingungen, Datenschutzbestimmungen und Support
# Onlinedokumentation.
# GitHub empfiehlt, Aktionen an einen Commit-SHA anzuheften.
# Um eine neuere Version zu erhalten, musst du den SHA aktualisieren.
# Du kannst auch auf ein Tag oder einen Branch verweisen, aber die Aktion kann sich ohne Vorwarnung ändern.
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@v4
- name: Set up Python version
uses: actions/setup-python@v5
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@v4
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@v4
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@v4
with:
name: python-app
path: .
- name: 'Deploy to Azure Web App'
id: deploy-to-webapp
uses: azure/webapps-deploy@85270a1854658d167ab239bce43949edb336fa7c
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
Additional resources
The following resources may also be useful:
- For the original workflow template, see
azure-webapps-python.yml
in the GitHub Actionsstarter-workflows
repository. - The action used to deploy the web app is the official Azure
Azure/webapps-deploy
action. - For more examples of GitHub Action workflows that deploy to Azure, see the actions-workflow-samples repository.