Python app stuck on deployment to Azure App Service via GitHub Actions

epicme 0 Reputation points
2024-05-07T16:06:10.3366667+00:00

Hi, I ran into an issue deploying my Python fastAPI server to an Azure App Service via GitHub Actions. I linked the code to a repository and then set up the CI pipeline to execute a deployment on new commits. The issue is that it never finishes deploying, seems to be stuck on the "deploy" task.

Furthermore, it works fine locally. The GItHub Actions setup was done on Azure Portal with the default deployment profile (shown below).

What could be the issue?

I have the following environment variables:

SCM_DO_BUILD_DURING_DEPLOYMENT = 1

Here is the server code:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello World!"}

This is the content of requirements.txt:

fastapi==0.108.0
uvicorn==0.25.0

The app is started via a run.sh script:

uvicorn app:app

And this is the default deployment profile generated from the Deployment Center on Azure Portal:

# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions
# More info on Python, GitHub Actions, and Azure App Service: https://aka.ms/python-webapps-actions

name: Build and deploy Python app to Azure Web App - test-app

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python version
        uses: actions/setup-python@v1
        with:
          python-version: '3.10'

      - name: Create and start virtual environment
        run: |
          python -m venv venv
          source venv/bin/activate
      
      - name: Install dependencies
        run: pip install -r requirements.txt
        
      # Optional: Add step to run tests here (PyTest, Django test suites, etc.)

      - name: Zip artifact for deployment
        run: zip release.zip ./* -r

      - name: Upload artifact for deployment jobs
        uses: actions/upload-artifact@v3
        with:
          name: python-app
          path: |
            release.zip
            !venv/

  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    permissions:
      id-token: write #This is required for requesting the JWT

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: python-app

      - name: Unzip artifact for deployment
        run: unzip release.zip

      
      - name: Login to Azure
        uses: azure/login@v1
        with:
          client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_CBD8C3026E1A4282B849F09360374194 }}
          tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_A6EA60031331436B85135D7D38DEA5C0 }}
          subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_342877BC8990412AAE8316D6F51A7355 }}

      - name: 'Deploy to Azure Web App'
        uses: azure/webapps-deploy@v2
        id: deploy-to-webapp
        with:
          app-name: 'test-app'
          slot-name: 'Production'
          
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,407 questions
Azure Startups
Azure Startups
Azure: A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.Startups: Companies that are in their initial stages of business and typically developing a business model and seeking financing.
240 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 15,866 Reputation points Microsoft Employee
    2024-05-09T02:45:47.25+00:00

    @epicme It seems like you’ve set up your GitHub Actions workflow correctly for deploying a Python FastAPI application to Azure App Service. However, if the deployment is stuck at the “deploy” task, there are a few potential issues to consider:

    1. SCM_DO_BUILD_DURING_DEPLOYMENT: This environment variable triggers the build process during the deployment phase. If the build process is taking too long or encountering errors, it could cause the deployment to hang. You can check the logs in the Azure Portal under your App Service’s “Deployment Center” to see if there are any build errors.
    2. Deployment Script: Sometimes, the default deployment script may not suit the specific needs of your application. You can customize the deployment script according to your application’s requirements. For example, you might need to modify the startup command or include additional steps for database migrations.
    3. Service Limits: Check if there are any service limits being hit, such as storage space or memory. Although less likely, it’s good to rule out these possibilities.
    4. Deployment Configuration: Ensure that the deployment configuration in the Azure Portal is correctly set up. This includes the deployment method, source control options, and any additional deployment settings.
    5. WebApp Configuration: Verify that the configuration settings of the Azure Web App match the requirements of your application, such as the correct Python version and any necessary application settings.
    6. Zip Deployment: If you’re using ZipDeploy, ensure that the zip file structure is correct and that all necessary files are included. The WEBSITE_RUN_FROM_PACKAGE setting can also be used to run the app directly from a zip file, which might help in certain scenarios.
    7. Permissions: Make sure that the service principal used for deployment has the correct permissions. The client ID, tenant ID, and subscription ID should have the necessary roles assigned for deployment.

    Let us know the outcome of the above suggestions and if we can assist you further.