将子目录 Flask 项目部署到 Azure Web App 的 Github 操作

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

我有这个文件夹结构:

Folder Structure

它是一个 React Web 应用程序,包含后端文件夹,例如 Flask 和 Nodejs。

由于要求,我需要将后端代码与前端代码结合起来。

因此,我尝试了多种方法并修改了我的 github actions 文件,终于成功让它工作了。

这是我的 yml 文件。

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

由于我只想将 backend-flask/ 文件夹部署到 azure web 应用程序,因此需要相当长的时间。

等待22分钟正常吗? Deployment

我的问题几乎与类似问题

相同

我尝试通过将包名称添加到 backend-flask/ 来使用该解决方案,但是

Deployment taking very long..

不然我的.yml有问题吗?

40分钟后终于失败:

reactjs flask azure-web-app-service github-actions subdirectory
1个回答
0
投票

我使用Python和React创建了一个简单的应用程序,然后通过GitHub将Python子文件夹部署到Azure App Service,并且部署成功。

当我使用您的 GitHub 工作流程文件时,我遇到了问题。我更改工作流程,如下所示。

.github/工作流程

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         

我突出显示了我所做的更改,如下所示。

enter image description here

enter image description here

以下是我的文件结构。

enter image description here

构建和部署运行没有任何问题,如下所示。

enter image description here

enter image description here

Azure 应用服务输出

enter image description here

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