Introduction
This guide explains how to use GitHub Actions to build and deploy a Python project to Azure App Service.
Remarque
Si vos workflows GitHub Actions doivent accéder aux ressources d’un fournisseur de cloud qui prend en charge OpenID Connecter (OIDC), vous pouvez configurer vos workflows pour qu’ils s’authentifient directement auprès du fournisseur de cloud. Cela vous permet d’arrêter de stocker ces informations d’identification en tant que secrets de longue durée, et de fournir d’autres avantages en matière de sécurité. Pour plus d’informations, consultez « À propos du renforcement de la sécurité avec 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:
-
Créer un plan Azure App Service.
Par exemple, vous pouvez utiliser Azure CLI pour créer un plan App Service :
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
Dans la commande ci-dessus, remplacez
MY_RESOURCE_GROUP
par votre groupe de ressources Azure préexistant etMY_APP_SERVICE_PLAN
par un nouveau nom pour le plan App Service.Pour plus d’informations sur l’utilisation d’Azure CLI, consultez la documentation Azure :
- Pour l’authentification, consultez Se connecter avec Azure CLI.
- Si vous devez créer un groupe de ressources, consultez 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. -
Configurez un profil de publication Azure, et créez un secret
AZURE_WEBAPP_PUBLISH_PROFILE
.Générez vos informations d’identification de déploiement Azure à l’aide d’un profil de publication. Pour plus d’informations, consultez Générer les informations d’identification du déploiement dans la documentation Azure.
Dans votre dépôt GitHub, créez un secret nommé
AZURE_WEBAPP_PUBLISH_PROFILE
, qui contient le profil de publication. Pour plus d’informations sur la création de secrets, consultez 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. Les environnements sont utilisés pour décrire une cible de déploiement général comme
production
,staging
oudevelopment
. Quand un workflow GitHub Actions est déployé dans un environnement, l’environnement s’affiche dans la page principale du dépôt. Vous pouvez utiliser les environnements pour exiger l’approbation d’un travail, restreindre les branches pouvant déclencher un flux de travail, contrôler les déploiements avec des règles personnalisées de protection des déploiements ou limiter l’accès aux secrets. Pour plus d’informations sur la création d’environnements, consultez 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.
Si vous avez configuré un environnement de déploiement, remplacez la valeur environment
par le nom de votre environnement. Si vous n’avez pas configuré d’environnement ou si votre workflow se trouve dans un référentiel privé et que vous n’utilisez pas GitHub Enterprise Cloud, supprimez la clé environment
.
# Ce workflow utilise des actions qui ne sont pas certifiées par GitHub. # Elles sont fournies par un tiers et régies par # des conditions d’utilisation du service, une politique de confidentialité et un support distincts. # documentation en ligne. # GitHub recommande d’épingler les actions à un SHA de commit. # Pour obtenir une version plus récente, vous devez mettre à jour le SHA. # Vous pouvez également référencer une balise ou une branche, mais l’action peut changer sans avertissement. 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 }}
# Ce workflow utilise des actions qui ne sont pas certifiées par GitHub.
# Elles sont fournies par un tiers et régies par
# des conditions d’utilisation du service, une politique de confidentialité et un support distincts.
# documentation en ligne.
# GitHub recommande d’épingler les actions à un SHA de commit.
# Pour obtenir une version plus récente, vous devez mettre à jour le SHA.
# Vous pouvez également référencer une balise ou une branche, mais l’action peut changer sans avertissement.
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.