0
votes

In my current solution, I have two projects: A Net Core 3.1 MVC project and an Azure Function App project. Both are hosted in Azure, but in different resources (Webapp in an AppService, and the Function App project has its own Function App resource.

-SolutionName
 |_ WebApp Project
 |_ WebApp.AzureFunction project

In my current Azure DevOps pipeline, I have defined a build and deploy for the webapp only. How can I add a step that will take only the Azure Function App and deploys it to its respective resource?

EDIT: Based on the answer below, I have edited the yaml. The pipeline succeeds, but the zip for the azure function is empty.

My current yaml file looks like this:

# vm-image, variables, building of webapp removed for brevity.           
steps:
- task: DotNetCoreCLI@2
  displayName: 'Build Project Azure Function: WebApplicationTest.AzFunction'
  inputs:
    projects: 'WebApplicationTest.AzFunction.PipelineTest/*.csproj'
    arguments: '--output $(System.DefaultWorkingDirectory)/publish_output --configuration Release'
    zipAfterPublish: false

- task: ArchiveFiles@2
  displayName: 'Archive Azure Function'
  inputs:
    rootFolderDirectory: '$(System.DefaultWorkingDirectory)/publish_output/'
    includeRootFolder: false
    ArchiveFile: '$(Build.ArtifactStagingDirectory)/functions/azfunction.zip'
    replaceExistingArchive: true

- task: PublishBuildArtifacts@1
  displayName: 'Publish Azure Function: WebApplicationTest.AzFunction.PipelineTest'
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)/functions'
    ArtifactName: 'functions'

#Deploy different artifact to different resources

# Deploy webapp step removed for brevity.

- task: AzureFunctionApp@1
  displayName: Deploy to Azure Function
  inputs:
    azureSubscription: $(azureSubscription)
    appType: functionApp
    appName: 'WebApplicationTest-AzFunction-PipelineTest'
    package: '$(Pipeline.Workspace)/**/azfunction.zip'

I think it needs another step and then take the Azure Function App project from the $(Build.ArtifactStagingDirectory) package, and deploy it to my Function App resource. What is the best way to do this?

1
why not have two pipelines?akb
Isn't it easier to have everything in one pipeline? What if my project has more Function Apps, I would need an additional pipeline every time one is added. That seems not a good solution maintenance-wise.CMorgan

1 Answers

1
votes

How can I add a step that will take only the Azure Function App and deploys it to its respective resource?

We could build and generate different artifacts for those two projects.

We can specify the full path of the project to be built and generate artifacts with different names for deployment to the perspective resource.

For example, I added comments to the code to help understand my ideals:

- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'

#build the Web App, MVC project, and generate the artifact MVC.zip under the path $(Build.ArtifactStagingDirectory)/MVC
- task: DotNetCoreCLI@2
  displayName: 'dotnet publish for MVC'
  inputs:
    command: 'publish'
    publishWebProjects: false
    projects: 'MVC/MVC/*.csproj'
    arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)/MVC'

- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)/MVC' 
    artifactName: 'MVC'

#build the Azure function App, AzureFunctionApp project, and generate the artifact function.zip under the path $(Build.ArtifactStagingDirectory)/function

- task: DotNetCoreCLI@2
  displayName: 'Build project for azure function'
  inputs:
    projects: MVC/AzureFunctionApp/AzureFunctionApp.csproj
    arguments: '--output publish_output --configuration Release'

- task: ArchiveFiles@2
  displayName: 'Archive files'
  inputs:
    rootFolderOrFile: 'publish_output/'
    includeRootFolder: false 
    archiveFile: '$(Build.ArtifactStagingDirectory)/function/function.zip'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: Function'
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/function'
    ArtifactName: Function

#Deploy different artifact to different resources

- task: AzureRmWebAppDeployment@4
  inputs:
    ConnectionType: 'AzureRM'
    azureSubscription: 'Subscription Name'
    appType: 'webApp'
    WebAppName: 'WebApp Name'
    package: '$(Build.ArtifactStagingDirectory)/**/MVC.zip'
    enableCustomDeployment: true
    DeploymentType: 'webDeploy'

- task: AzureFunctionApp@1
  displayName: Azure Function App Deploy
  inputs:
    azureSubscription: $(azureSubscription)
    appType: functionApp
    appName: $(appName)
    package: $(System.ArtifactsDirectory)/**/function.zip

The test result:

enter image description here