Python 应用程序在通过 GitHub Actions 部署到 Azure 应用服务时卡住了

问题描述 投票:0回答:1

我在通过 GitHub Actions 将 Python fastAPI 服务器部署到 Azure 应用服务时遇到了问题。我将代码链接到存储库,然后设置 CI 管道以在新提交上执行部署。问题是它永远不会完成部署,似乎卡在“部署”任务上(等待了 30 多分钟)。

此外,它在本地运行良好。 GitHub Actions 设置是在 Azure 门户上使用默认部署配置文件完成的(如下所示)。

可能是什么问题?

我有以下环境变量:

SCM_DO_BUILD_DURING_DEPLOYMENT = 1

这是一个示例服务器代码:

from fastapi import FastAPI

app = FastAPI()

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

这是requirements.txt的内容:

fastapi==0.108.0
uvicorn==0.25.0

应用程序通过 run.sh 脚本启动:

uvicorn app:app

这是从 Azure 门户部署中心生成的默认部署配置文件:

# 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'

      
python azure-web-app-service github-actions fastapi
1个回答
0
投票

我尝试了你的代码,它通过 GitHub Actions 成功部署到 Azure 应用服务。

确保

requirements.txt
文件中列出的所有依赖项与您正在使用的 Python 版本兼容,并且在部署过程中已成功安装。

仔细检查环境变量、客户端 ID 和客户端密码等机密是否正确并在部署过程中执行。

下面是我完整的yml文件。

工作流程文件

name: Build and deploy Python app to Azure Web App - kafastapiapp
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.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
      - 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: 'kafastapiapp'
          slot-name: 'Production'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_<SecretKey>}}

已成功部署到Azure,如下图。

enter image description here

为 Azure Web App 配置以下启动命令:

gunicorn --worker-class uvicorn.workers.UvicornWorker --timeout 600 --access-logfile '-' --error-logfile '-' app:app

如下图。

enter image description here

Azure 应用服务输出

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.