Web API As Azure Function - DevOps

Previous few article in this series we have discussed around how to develop and configure Web API as Azure Function.

In this article there is information around how to prepare function definition and API for deployment.

Build Pipeline

  • Build Project

  • Publish Project To Specific Directory ( e.g Final)

  • Copy HttpTrigger, SaveRecord and UserTopicProcessor directory to final publsh. (e.g Final)

trigger:
- main

pool:
  vmImage: ubuntu-latest

variables:
  - name: 'customHandler.description.defaultWorkerPath'
    value: 'WebAPI.dll'

stages:
 - stage: Build
   jobs:
   - job: Build
     steps:

      - task: UseDotNet@2
        inputs:
          packageType: 'sdk'
          useGlobalJson: true

      - task: DotNetCoreCLI@2
        displayName: Build Web API Project
        inputs:
          command: 'build'
          projects: '**/*.csproj'
          arguments: '--configuration release'

      - task: DotNetCoreCLI@2
        displayName: Publish Web API Project
        inputs:
          command: 'publish'
          projects: '**/*.csproj'
          arguments: '--configuration release --output $(Build.ArtifactStagingDirectory)/API'
          zipAfterPublish: false
          publishWebProjects: false

      - task: CmdLine@2
        displayName: Prepare Azure Function For Publish With API
        inputs:
          script: |
            echo Create Final Directory
            mkdir $(Build.ArtifactStagingDirectory)/Final
            echo Copy WebAPI To Final Directory
            cp -r $(Build.ArtifactStagingDirectory)/API/WebAPI $(Build.ArtifactStagingDirectory)/Final
            echo Copy Trigger To Final Directory
            cp -r $(Build.SourcesDirectory)/HttpTrigger $(Build.ArtifactStagingDirectory)/Final
            cp -r $(Build.SourcesDirectory)/SaveRecord $(Build.ArtifactStagingDirectory)/Final
            cp -r $(Build.SourcesDirectory)/UserTopicProcessor $(Build.ArtifactStagingDirectory)/Final
            echo Copy host.json
            cp $(Build.SourcesDirectory)/host.json $(Build.ArtifactStagingDirectory)/Final/host.json      
            echo Check Final
            cd $(Build.ArtifactStagingDirectory)/Final            
            tree

      - task: FileTransform@1
        displayName: Replace Token in json settings
        inputs:
          folderPath: '$(Build.ArtifactStagingDirectory)/Final'
          fileType: 'json'
          targetFiles: '**/host.json'

      - task: ArchiveFiles@1
        inputs:
          includeRootFolder: false
          archiveType: default
          rootFolder: $(Build.ArtifactStagingDirectory)/Final
          archiveFile: $(Build.ArtifactStagingDirectory)/Final.zip

      - task: PublishBuildArtifacts@1
        inputs:
          ArtifactName: drop
          PathtoPublish: $(Build.ArtifactStagingDirectory)/Final.zip 

 - stage: Deploy
   jobs:
   - job: Deploy
     steps:
     - task: DownloadBuildArtifacts@0
       inputs:
          artifactName: drop               

     - task: AzureFunctionApp@1
       inputs:
          azureSubscription: 'Azure Resource Manager Visual Studio Subscription'
          appType: functionApp
          appName: 'FunctionAppCustomHandler'
          deploymentMethod: auto                    
          package: '$(System.ArtifactsDirectory)/drop/Final.zip'
  • In above there is both Build and Deploy pipeline.

Following is the final structure of artifact.

image.png

Important Steps

  • Replace defaultWorkerPath in host.json. As during development we are using dotnet run and after publish dotnet WebAPI.dll to run compiled application.
variables:
  - name: 'customHandler.description.defaultWorkerPath'
    value: 'WebAPI.dll'

Deploy Azure Function

  • Following two tasks are related to deployment.
 - task: DownloadBuildArtifacts@0
       inputs:
          artifactName: drop               

     - task: AzureFunctionApp@1
       inputs:
          azureSubscription: 'Azure Resource Manager Visual Studio Subscription'
          appType: functionApp
          appName: 'FunctionAppCustomHandler'
          deploymentMethod: auto                    
          package: '$(System.ArtifactsDirectory)/drop/Final.zip'

In above appName and azureSubscription needs to replace with respective value related to your environment.

Result

image.png

Source code

  • It can be found at repository. Download

  • Look at azure-pipelines.yaml

Summary

In this series of article, there is enough information about Web API as Azure Function. It start from configuration to development to deployment.

There is also couple of point like output binding, input binding needs to explore in this context. I will try to explore that in near future and what it offer in context of Web API as Azure Function scenario of deployment.