SKY-BIRD Web Media
SKY-BIRD Web Media

Reputation: 1

Flask app deployed with GitHub Actions to Azure App Services not accessible despite successful deployment

I am trying to deploy a simple Flask application to Azure App Services using GitHub Actions. The deployment seems to be successful, and I can see from the application logs that Gunicorn starts successfully on port 80 without any errors:

This is Application Logs from Azure App Service

[2024-06-30 14:52:34 +0000] [80] [INFO] Starting gunicorn 22.0.0
[2024-06-30 14:52:34 +0000] [80] [INFO] Listening at: http://0.0.0.0:80 (80)
[2024-06-30 14:52:34 +0000] [80] [INFO] Using worker: sync
[2024-06-30 14:52:34 +0000] [81] [INFO] Booting worker with pid: 81
[2024-06-30 14:52:34 +0000] [82] [INFO] Booting worker with pid: 82
[2024-06-30 14:52:34 +0000] [83] [INFO] Booting worker with pid: 83
[2024-06-30 14:52:34 +0000] [84] [INFO] Booting worker with pid: 84

However, when I try to access the deployed Flask application via the web browser, I receive a Application Error" error. To troubleshoot, I have checked the Azure App Services logs and confirmed that Gunicorn is running on port 80. I logged in ssh and tried to re run the gunicorn cmd but I observe gunicorn is already running on port 80 SSH logs

GitHub Actions Workflow

name: Build and deploy Python app to Azure Web App

env:
  AZURE_WEBAPP_NAME: pythonshashi  # set this to your application's name
  PYTHON_VERSION: '3.12'               # 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@v3
        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 }}

app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, World! This is my Flask application running with Gunicorn on port 80."


@app.route('/about')
def about():
    return "This is the about page."

if __name__ == '__main__':
    app.run(debug=True)

To run this application after the deployment I added a startup.sh file also link this in App Service configuration>startup

startup.sh

#!/bin/bash

python3 -m pip install --upgrade pip
pip install -r requirements.txt


# Start Gunicorn server with your Flask application
gunicorn -b 0.0.0.0:80 -w 4 --timeout 600 app:app

I tried to debug it and get to know that If I removed the startup configuration from App Service and after the deployment, through SSH I manually ran the startup.sh and webpage starts working.

Upvotes: 0

Views: 291

Answers (1)

Sirra Sneha
Sirra Sneha

Reputation: 1197

The error you're encountering is related to configuration in your startup.sh file.

I created a project based on the provided codes and successfully deployed it to azure app service via GitHub actions.

I got the same error when I tried your startup.sh file, I've changed the startup.sh to the following

python  -m  pip  install  --upgrade  pip
pip  install  -r  requirements.txt
gunicorn  -b  0.0.0.0:${WEBSITES_PORT:-8000} -w  4  --timeout  600  app:app

Setup virtual environment

python -m venv venv
venv\Scripts\activate

This is my .yml file:

name: Build and deploy Python app to Azure Web App - sflask
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.12'
- name: Create and start virtual environment
run: |
python -m venv venv
source venv/bin/activate
- name: Install dependencies
run: pip install -r requirements.txt
- 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 }}
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: 'Deploy to Azure Web App'
uses: azure/webapps-deploy@v2
id: deploy-to-webapp
with:
app-name: 'sflask'
slot-name: 'Production'
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_28F2816178DA40D480C0843A3F17B173 }}

Before you deploy your app to Azure App Service, make sure to run this command in your project's root directory.

pip freeze > requirements.txt

I've successfully deployed the app via Github.

enter image description here

Here's the Output after deployment:

enter image description here

I don't know if this is normal behaviour of App service.

In flask applications, specifying a startup command is important. If you decide to include a startup command, you can either write it in a startup.sh file in your project or configure it directly in the startup command of the Azure App Service settings."

Upvotes: 0

Related Questions