Search code examples
reactjsflaskazure-web-app-servicegithub-actionssubdirectory

Github actions to deploy subdirectory flask project to Azure Web App


I have this folder structure:

Folder Structure

It is a react web app, with backend folders included like flask and nodejs.

Due to requirements I'm required to combine my backend towards my frontend code.

Hence I've tried several ways and modifying my github actions file and finally managed to got it to working.

This is my yml file.

# 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 - app-backend-flask

on:
  push:
    branches:
      - master
    paths:
      - "backend-flask/**"
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: ./backend-flask
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python version
        uses: actions/setup-python@v1
        with:
          python-version: "3.9"

      - 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: |
            backend-flask/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 }}
          tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID }}
          subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID }}

      - name: "Deploy to Azure Web App"
        uses: azure/webapps-deploy@v2
        id: deploy-to-webapp
        with:
          app-name: "app-backend-flask"
          slot-name: "Production"

As i just want to deploy my backend-flask/ folder to a azure web app, it takes a pretty long time.

Is waiting up to 22min normal? Deployment

My issue is almost the same as similar problem

I've tried using the solution by adding package name to backend-flask/ but

Deployment taking very long..

Else does my .yml have a problem?

After 40minutes it finally failed: Failed deployment


Solution

  • I created a simple app using Python and React, then deployed the Python subfolder to the Azure App Service via GitHub, and it was successfully deployed.

    When I used your GitHub workflow file, I encountered issues. I Change the workflow as shown below.

    .github/workflow :

    name: Build and deploy Python app to Azure Web App - kasampleflask
    on:
      push:
        branches:
          - main
      workflow_dispatch:
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - name: Set up Python version
            uses: actions/setup-python@v1
            with:
              python-version: '3.9'
          - name: Create and start virtual environment and Install Dependencies
            run: |
              python -m venv venv
              source venv/bin/activate
            working-directory: ./backend
          - name: Install dependencies
            run: pip install -r requirements.txt
            working-directory: ./backend     
          - name: Upload artifact for deployment jobs
            uses: actions/upload-artifact@v2
            with:
              name: python-app
              path: |
                ./backend 
                !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: ./backend
          - name: 'Deploy to Azure Web App'
            uses: azure/webapps-deploy@v2
            id: deploy-to-webapp
            with:
              app-name: 'kasampleflask'
              slot-name: 'Production'
              publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_<Secret_Key> }}
              package: ./backend         
    

    I highlighted the changes I made as shown below.

    enter image description here

    enter image description here

    Below is my File Structure.

    enter image description here

    Build and Deploy runs without any issues, as shown below.

    enter image description here

    enter image description here

    Azure App Service Output:

    enter image description here