Search code examples
azureazure-devopsyamlazure-pipelines

How to create a YAML file to support .NET 6, .NET 7, .NET 8?


My project has multiple frameworks net6.0;net7.0;net8.0

I created a YAML file and everything works, but when I set it to .NET 8, meaning a single framework.

trigger:
- main

pool:
  vmImage: 'windows-latest'

variables:
  buildConfiguration: 'Release'

steps:
- script: echo 'Starting the build'
  displayName: 'Starting Build'

- script: dotnet restore
  displayName: 'Restore Dependencies'

- script: dotnet build --configuration $(buildConfiguration) --no-restore
  displayName: 'Build Project'

- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    publishWebProjects: true
    arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory) -property:TargetFramework=net8.0'
    zipAfterPublish: false
  displayName: 'Publish Artifact'


- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Build.ArtifactStagingDirectory)'
    artifact: 'myAppBuild'
    publishLocation: 'pipeline'
  displayName: 'Publish Build Artifact'

I need a solution that supports .NET 6, .NET 7, and .NET8


Solution

  • You can use an object type runtime parameter to pass all the three target frameworks to the build job.

    parameters:
    - name: TargetFrameworks
      type: object
      default:
        - 'net8.0'
        - 'net7.0'
        - 'net6.0'
    
    steps:
    - script: echo 'Starting the build'
      displayName: 'Starting Build'
    
    - script: dotnet restore
      displayName: 'Restore Dependencies'
    
    - script: dotnet build --configuration $(buildConfiguration) --no-restore
      displayName: 'Build Project'
    
    - ${{ each Framework in parameters.TargetFrameworks }}:
      - task: DotNetCoreCLI@2
        displayName: 'Publish Artifact - ${{ Framework }}'
        inputs:
          command: publish
          publishWebProjects: true
          arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)/${{ Framework }} -property:TargetFramework=${{ Framework }}'
          zipAfterPublish: false
    
    - task: PublishPipelineArtifact@1
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)'
        artifact: 'myAppBuild'
        publishLocation: 'pipeline'
      displayName: 'Publish Build Artifact'
    

    With this way, the pipeline will copy 3 dotnet publish tasks for the 3 target frameworks within the same job.