Search code examples
azure-devopsazure-functionsazure-pipelines-release-pipelineazure-pipelines-yamlbuild-pipeline

Azure DevOps Publish Azure Function not working


I am trying to create a pipeline in order to publish my code in an Azure Function. In order to do that I am using the following as reference: https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-azure-devops?tabs=dotnet-core%2Cyaml%2Ccsharp
However I got the following error:

Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version.

After struggling a little I got the following Build Pipeline yaml (I comment the code that cause the error):

trigger:
- none

pr:
- none

pool:
  vmImage: "windows-latest"

variables:
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  #output: "publish"
  #project: "*.csproj"
  solution: "**/*.sln"
  
steps:

- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

#- task: DotNetCoreCLI@2
#  inputs:
#    command: publish
#    arguments: "--configuration $(buildConfiguration) --output $(output)"
#    projects: $(project)
#    publishWebProjects: false
#    modifyOutputPath: false
#    zipAfterPublish: false

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(Build.ArtifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: ArchiveFiles@2
  displayName: "Zip Files"
  inputs:
    rootFolderOrFile: "$(Build.ArtifactStagingDirectory)"
    includeRootFolder: false
    archiveFile: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"
    artifactName: "drop"

which generates the following artifact: enter image description here that contains enter image description here

and Release Pipeline:

steps:
- task: AzureFunctionApp@1
  displayName: 'Azure Function App Deploy'
  inputs:
    azureSubscription: '<SubscriptionName>'
    appType: functionApp
    appName: <FunctionApp Name>
    deploymentMethod: zipDeploy

the execution of the release pipeline ends as Successful enter image description here However nothing is published on Azure enter image description here enter image description here

Do you know what I am missing?


Solution

  • I believe You are missing the publish step here, you are only building but not publishing. The zip file you have does not look right, after a publish you should be getting a whole bunch of *.dll files

    The code you commented out you need to be running after the build step. In an ideal flow it should be.

    This should be the ideal flow.

    1. Specify the .NET version you want
    2. Restore nuget packages
    3. Dotnet build
    4. Donet publish
    5. Archive files
    6. Publish the artifacts (the release will be from this artifacts)

    In the Release pipeline you should get that published artifact and then deploy to Azure

    Build

    - task: UseDotNet@2
      displayName: 'Use .NET Core sdk'
      inputs:
        packageType: 'sdk'
        version: '6.0.x'
    
    - task: DotNetCoreCLI@2
      displayName: Restore Packages
      inputs:
        command: restore
        projects: '**/*.csproj'
    
    - script: |
        dotnet build --configuration Release
      workingDirectory: '<path-to-your-project-tobuild>'
      displayName: "Build"
    
    - task: DotNetCoreCLI@2
      displayName: "Publish"
      inputs:
        command: publish
        arguments: '--configuration Release --output publish_output'
        projects: '<path-to-your-project-tobuild>'
        publishWebProjects: false
        modifyOutputPath: false
        zipAfterPublish: false
    
    - task: ArchiveFiles@2
      displayName: "Archive files"
      inputs:
        rootFolderOrFile: "$(System.DefaultWorkingDirectory)/publish_output"
        includeRootFolder: false
        archiveFile: "$(System.DefaultWorkingDirectory)/build.zip"
    
    - task: PublishBuildArtifacts@1
      displayName: "Publish Artifacts"
      inputs:
        PathtoPublish: '$(System.DefaultWorkingDirectory)/build.zip'
        artifactName: 'drop'
    

    and in your Release Pipeline

     - task: DownloadBuildArtifacts@0
            inputs:
              buildType: 'current'
              downloadType: 'single'
              artifactName: drop
              downloadPath: '$(System.ArtifactsDirectory)'
    
          - task: AzureRmWebAppDeployment@4
            inputs:
              ConnectionType: 'AzureRM'
              azureSubscription: '<your-sub>'
              appType: 'webApp'
              WebAppName: '<your-name>'
              packageForLinux: '$(System.ArtifactsDirectory)/**/*.zip'