pythonazure-functionsgithub-actions

Azure Functions Python PIP issue


I have an azure functions app running on python@3.11

I need to use requests module, so at the top of the .py file I've added 'import requests'. I've also added requests to requirements.txt.

I have CI/CD configured with GitHub Actions. The yml is attached.

My understanding is MS should install the reqs in requirements.txt and make them available however, when I add import requests to the py file. No functions show in the azure portal. If I remoe the import then the functions are again visible.

Is it possible that the packages are being loaded but when the python function app starts it's starting from a different bin and as a result no packages are present? Or am I missing a step/approach with my yaml?

How should I correctly load the requirements.txt? is my flow correct?

name: Build and deploy Python project to Azure Function App - scubashackchat

on:
  push:
    branches:
      - master
  workflow_dispatch:

env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
  PYTHON_VERSION: '3.11' # set this to the python version to use (supports 3.6, 3.7, 3.8)

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Python version
        uses: actions/setup-python@v1
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - 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

      - name: Zip artifact for deployment
        run: zip release.zip ./* -r

      - name: Upload artifact for deployment job
        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-function.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: red
          tenant-id: red
          subscription-id: red

      - name: 'Deploy to Azure Functions'
        uses: Azure/functions-action@v1
        id: deploy-to-function
        with:
          app-name: 'scubashackchat'
          slot-name: 'Production'
          package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
          scm-do-build-during-deployment: true
          enable-oryx-build: true
          

Solution

  • You can refer this example. In short, You don't need to create virtual environment & install packages. You can use --target option of pip install to install the packages on a specific location(.python_packages/lib/site-packages) for the function visibility.

    - name: 'Resolve Project Dependencies Using Pip'
      shell: bash
      run: |
        pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
        python -m pip install --upgrade pip
        pip install -r requirements.txt --target=".python_packages/lib/site-packages"
        popd
    

    It's also worth noting that you will need to change your zip script to ensure that it includes hidden directories.

    - name: Zip artifact for deployment
      run: zip -r release.zip . -x "venv/*" ".git/*" ".github/*" ".gitignore"