Template repo for FastAPI. Includes CI/CD on Azure Web App using Github Actions. Uses Poetry for dependency management.
- run
git clone https://github.com/jmargutt/fastapi-template.git
- change code as needed
- add needed environment variables to
.env
file
Warning
Do not store credentials/passswords/keys in the code, use the .env
file instead.
This file will not be pushed to the repository, as it is listed in the .gitignore
file, so your credentials
won't be exposed.
- add needed packages to
pyproject.toml
- run
pip install poetry
to install poetry - run
poetry install
to install the packages (orpython -m poetry install
, ifpoetry
command is not found) - run
poetry run uvicorn main:app --reload
to start the server (orpython -m poetry run uvicorn main:app --reload
, ifpoetry
command is not found) - go to
http://127.0.0.1:8000/docs
and test if the app runs as expected.
- Create a new App Service Plan in Azure, or choose a pre-existing one.
- Create a new Web App in Azure:
- choose a meaningful name, e.g.
fastapi-template-jacopo
- select
Publish
:Docker Container
- select
Operating System
:Linux
- select
Region
:West Europe
- select the App Service Plan you created in step 1
- Create the Web App
- choose a meaningful name, e.g.
- Enable basic authentication
- Go to the Web App in the Azure portal
- Under
Settings
, selectConfiguration
->General settings
->Platform settings
- Set
SCM Basic Auth Publishing Credentials
toOn
- Set
FTP Basic Auth Publishing Credentials
toOn
- Save the changes
- Go back to the
Overview
and clickRestart
- Get a Web App Publish Profile, to deploy from GitHub
- Go to your app service in the Azure portal.
- On the Overview page, select
Download publish profile
. - Save the downloaded file. You'll use the contents of the file to create a GitHub secret.
- Create a new Azure Container Registry or choose a pre-existing one.
- Go to the registry ->
Access keys
-> EnableAdmin user
-> Copy the username and password
- Go to the registry ->
- Give permissions to the Web App to access the Container Registry
- Go to your app service in the Azure portal.
- Under
Configuration
, update theApplication settings
:DOCKER_REGISTRY_SERVER_URL
: the URL of the Azure Container Registry, e.g.fastapiregister.azurecr.io
DOCKER_REGISTRY_SERVER_USERNAME
: the username of the Azure Container Registry, e.g.fastapiregister
DOCKER_REGISTRY_SERVER_PASSWORD
: the password of the Azure Container Registry
Important
These Application settings
determine which environment variables are accessible by the web app.
If you change/add environment variables in the GitHub repository, don't forget to update the Web App Configuration
in the Azure portal.
- Create the GitHub secrets and variables, so that GitHub Actions can deploy to Azure
- Go to your GitHub repository
- Go to
Settings
->Secrets and variables
->Actions
->New repository secret
- Add the following repository secrets:
AZURE_WEBAPP_PUBLISH_PROFILE
: the contents of the downloaded file from step 3REGISTRY_PASSWORD
: the password of the Azure Container Registry
- Go to
Settings
->Secrets and variables
->Actions
->New repository variable
- Add the following repository variables:
AZURE_WEBAPP_NAME
: the name of your web app, e.g.fastapi-template-jacopo
REGISTRY_NAME
: the name of the Azure Container Registry, e.g.fastapiregister
- Push a change to the repository to trigger the GitHub Actions workflow.
- Wait 5-10 minutes, then go to
<my-api-name>.azurewebsites.net
and see the app running. - If the app is not running
- go to the
Actions
tab in your GitHub repository and check the logs of the failed workflow. - go to the
Overview
tab of your Web App in the Azure portal and checkDeployment logs
->Logs
.
- go to the
cp example.env .env
pip install poetry
poetry install
uvicorn main:app --reload