How to Deploy Python Code from Azure Repository to Azure Function(Linux Based) App using Azure Pipeline

536 Views Asked by At

I have Durable Function written in Python 3.10 available in Azure Repository, I want to Build and Deploy a function to Azure Function App (Linux Based).

I've tried with below pipeline configuration which deploy functions to azure function app but it gives 500 Error when we use. Although If I deploy this same code using VS Code manually, it works fine as expected.

  • Function App : OS: Linux Runtime Stack : Python - 3.10

  • Code Located in Azure Repos

Requirement : Need to Deploy Durable Function to Azure Function App using Azure Pipelines.

Image_Repository-Code Structure

Configured Pipeline Code

- task: UsePythonVersion@0
  displayName: 'Use Python 3.10'
  inputs:
    versionSpec: 3.10

steps:
- script: |
   python -m venv antenv
   source antenv/bin/activate
   # Upgrade pip
   python -m pip install --upgrade pip
   
   # Verify that pip is up to date
   pip --version
   
   pip install -r $(System.DefaultWorkingDirectory)/[FOLDER1_ReposReference]/requirements.txt
  displayName: 'Command Line Script'
  
steps:
- task: ArchiveFiles@2
  displayName: 'Archive $(System.DefaultWorkingDirectory)/[FOLDER1_ReposReference]'
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)/[FOLDER1_ReposReference]'
    includeRootFolder: false
    archiveFile: '$(Build.ArtifactStagingDirectory)/durable_function.zip'
    
steps:
- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/durable_function.zip'
    
steps:
- task: AzureFunctionApp@2
  displayName: Deploy
  inputs:
    connectedServiceNameARM: 'SUBSCRIPTION_ID'
    appType: functionAppLinux
    appName: 'functionapp-name'
    package: '$(Build.ArtifactStagingDirectory)/*.zip'
    runtimeStack: 'PYTHON|3.10'```
1

There are 1 best solutions below

0
On

In order to Deploy Azure Function to Azure Function App successfully from Azure Devops YAML pipeline, Refer below:-

My Azure Repository with sample Function:-

enter image description here

Now, Select Set up build and select the Default Template like below:-

enter image description here

In the Pop up window Select Your subscription and click continue > Sign in to your account and select your Linux Function app like below:-

enter image description here

DevOps YAML pipeline generated like below, Change Default python version to 3.10:-

trigger:
- master

variables:
  
  azureSubscription: 'xxxx-9fe1-00dc49fd1540'

  # Function app name
  functionAppName: 'siliconfunc63'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

  # Working Directory
  workingDirectory: '$(System.DefaultWorkingDirectory)/'

stages:
- stage: Build
  displayName: Build stage

  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
    - bash: |
        if [ -f extensions.csproj ]
        then
            dotnet build extensions.csproj --runtime ubuntu.16.04-x64 --output ./bin
        fi
      workingDirectory: $(workingDirectory)
      displayName: 'Build extensions'

    - task: UsePythonVersion@0
      displayName: 'Use Python 3.10'
      inputs:
        versionSpec: 3.10 

    - bash: |
        pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
      workingDirectory: $(workingDirectory)
      displayName: 'Install application dependencies'

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(workingDirectory)'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      artifact: drop

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()

  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: 'development'
    pool:
      vmImage: $(vmImageName)

    strategy:
      runOnce:
        deploy:

          steps:
          - task: AzureFunctionApp@1
            displayName: 'Azure functions app deploy'
            inputs:
              azureSubscription: '$(azureSubscription)'
              appType: functionAppLinux
              appName: $(functionAppName)
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'

Output:-

enter image description here

enter image description here

Refer my SO thread answer here for more details on deploying Function via Devops.