Home > Back-end >  How can I use Azure DevOps CI/CD to deploy multiple Azure Functions
How can I use Azure DevOps CI/CD to deploy multiple Azure Functions

Time:03-14

Im doing a project with multiple Azure Functions in .NET 6 and Visual Studio 2022 and I want to deploy my functions to Azure via Azure Pipelines. I´ve got the repo in Azure DevOps and so far I been able to setup a Pipeline YAML file succesfully that looks like this:

trigger:
- main

pool:
  vmImage: windows-2022

steps:

- task: UseDotNet@2
  displayName: 'Install .NET Core sdk 6.x'
  inputs:
    version: 6.x
    includePreviewVersions: true

- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    feedsToUse: 'select'
    vstsFeed: *Hidden*

- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '**/*.csproj'
    arguments: '--output $(Build.BinariesDirectory)/publish_output --configuration Release'

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

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

- task: AzureKeyVault@2
  inputs:
    azureSubscription: *Hidden*
    KeyVaultName: *Hidden*'
    SecretsFilter: '*'
    RunAsPreJob: true

The repo looks like this:

Azure Repo (it also includes a .sln file but Business information that I dont want to show)

Every folder consist of an Azure Function and therefor a .csproj file (except for Shared folders, they are just class libraries), in my Azure Releases I want to have one for each Azure Function but I dont know how to acheive that, anyone have some experience about this and could share how you would do? Maybe there is another way to acheive this?

Example:

  1. Azure Function in VS project is named func-x-x
  2. Make changes and commit to repo
  3. Triggers pipeline and make a release to created Azure Function in Azure with same name

CodePudding user response:

In general, I think you want to do this in a Release pipeline, not a build pipeline. You are already publishing your build artifacts with the PublishBuildArtifacts@1 task. You can now setup a Release pipeline to consume these published artifacts and Release out into your environment. You can also set this up so its automatic the way you are describing.

The UI for Release pipelines isn't yaml based so you might find it easier to manage.

CodePudding user response:

Since you mentioned

Triggers pipeline and make a release to created Azure Function in Azure with same name

I think you want to deploy each of your azure functions to different function apps.

For example, if you have a solution with 3 function apps:

enter image description here

then you can use the below YAML to achieve what you want.

pool:
  name: Azure Pipelines
  demands:
  - msbuild
  - visualstudio

steps:
- task: NuGetToolInstaller@1
  displayName: 'Use NuGet 4.4.1'
  inputs:
    versionSpec: 4.4.1

- task: NuGetCommand@2
  displayName: 'NuGet restore'

- task: VSBuild@1
  displayName: 'Build solution **\*.sln'


- task: ArchiveFiles@2
  displayName: 'Archive $(System.DefaultWorkingDirectory)\FunctionDeployedByDevOps\bin\Debug\netcoreapp3.1\*'
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)\FunctionDeployedByDevOps\bin\Debug\netcoreapp3.1\*'
    archiveFile: '$(System.DefaultWorkingDirectory)\FunctionDeployedByDevOps\bin\Debug\netcoreapp3.1\$(Build.BuildId).zip'

- task: AzureFunctionApp@1
  displayName: 'Azure Function App Deploy: FunctionDeployedByDevOps'
  inputs:
    azureSubscription: 'testbowman_in_AAD'
    appType: functionApp
    appName: FunctionDeployedByDevOps
    package: '$(System.DefaultWorkingDirectory)\FunctionDeployedByDevOps\bin\Debug\netcoreapp3.1\$(Build.BuildId).zip'

- task: ArchiveFiles@2
  displayName: 'Archive $(System.DefaultWorkingDirectory)\FDBD2\bin\Debug\netcoreapp3.1\*'
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)\FDBD2\bin\Debug\netcoreapp3.1\*'
    archiveFile: '$(System.DefaultWorkingDirectory)\FDBD2\bin\Debug\netcoreapp3.1\$(Build.BuildId).zip'

- task: AzureFunctionApp@1
  displayName: 'Azure Function App Deploy: FDBD2'
  inputs:
    azureSubscription: 'testbowman_in_AAD'
    appType: functionApp
    appName: FDBD2
    package: '$(System.DefaultWorkingDirectory)\FDBD2\bin\Debug\netcoreapp3.1\$(Build.BuildId).zip'

- task: ArchiveFiles@2
  displayName: 'Archive $(System.DefaultWorkingDirectory)\FDBD3\bin\Debug\netcoreapp3.1\*'
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)\FDBD3\bin\Debug\netcoreapp3.1\*'
    archiveFile: '$(System.DefaultWorkingDirectory)\FDBD3\bin\Debug\netcoreapp3.1\$(Build.BuildId).zip'

- task: AzureFunctionApp@1
  displayName: 'Azure Function App Deploy: FDBD3'
  inputs:
    azureSubscription: 'testbowman_in_AAD'
    appType: functionApp
    appName: FDBD3
    package: '$(System.DefaultWorkingDirectory)\FDBD3\bin\Debug\netcoreapp3.1\$(Build.BuildId).zip'

By the way, if there is no special need, you can include multiple functions under a function app, so that it only needs to be deployed once.

If you are using Visual Studio, right Click your project, and select Add -> New Functions:

enter image description here

Please let me know if you have more concerns.

  • Related