我正在尝试使用 GitHub Actions 将一个简单的 Flask 应用程序部署到 Azure 应用服务。部署似乎成功了,从应用程序日志中可以看到Gunicorn在端口80上成功启动,没有任何错误:
这是来自 Azure 应用服务的应用程序日志
[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
但是,当我尝试通过 Web 浏览器访问已部署的 Flask 应用程序时,收到“应用程序错误”错误。为了排除故障,我检查了 Azure 应用服务日志并确认 Gunicorn 正在端口 80 上运行。 我登录 ssh 并尝试重新运行 Gunicorn cmd,但我观察到 Gunicorn 已经在端口 80 上运行 SSH 日志
GitHub 操作工作流程
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 }}
应用程序.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)
为了在部署后运行此应用程序,我添加了一个startup.sh 文件,还在应用服务配置>启动中链接此文件
启动.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
我尝试调试它并了解到,如果我从应用服务中删除了启动配置,并且在部署后,通过 SSH 我手动运行startup.sh并且网页开始工作。
您遇到的错误与您的
startup.sh
文件中的配置有关。
我根据提供的代码创建了一个项目,并通过 GitHub actions 成功将其部署到 azure 应用程序服务。
当我尝试你的
startup.sh
文件时,我遇到了同样的错误,
我已将 startup.sh
更改为以下
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
设置虚拟环境
python -m venv venv
venv\Scripts\activate
这是我的.yml 文件:
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 }}
在将应用程序部署到 Azure 应用服务之前,请确保在项目的根目录中运行此命令。
pip freeze > requirements.txt
我已通过 Github 成功部署了该应用程序。
这是部署后的输出:
我不知道这是否是App服务的正常行为。
在 Flask 应用程序中,指定启动命令很重要。如果您决定包含启动命令,可以将其写入项目中的
startup.sh
文件中,也可以直接在 Azure 应用服务设置的启动命令中进行配置。”