8
votes

I'm trying to configure a CI that will produces NuGet packages as artifacts on Azure DevOps (that will be later push to my NuGet server).

For this, I'm using Builds Pipelines on Azure DevOps, the YAML version.

I have 3 projects that should build packages. I'm using NuGetCommand@2 to accomplish this task :

- task: NuGetCommand@2
  inputs:
    command: pack
    packagesToPack: $(Build.SourcesDirectory)/src/HTS_MessageEngine.Communication/HTS_MessageEngine.Communication.csproj
    majorVersion: $(majorVersion)
    minorVersion: $(minorVersion)
    patchVersion: $(patchVersion)
    versioningScheme: byPrereleaseNumber

However, I have to duplicate this block 3 times, for each project. Is there a way to specify an array of project in packagesToPack parameter ? So far, version is the same for each package, so I don't need three different blocks...

Note : this 3 projects are all 3 NetStandard and properties for package building are stored in csproj directly

2

2 Answers

12
votes

you can use each function (if its available at all at this point in time):

# my-template.yml
parameters:
steps:
- ${{ each project in parameters.projects }}:
  - task: PublishBuildArtifacts@1
    displayName: 'Publish ${{ project }}
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)/${{ project }}.zip'
# ci.yml
steps:
- template: my-template.yml
  parameters:
    projects:
    - test1
    - test2

Github PR for this functionality: https://github.com/Microsoft/azure-pipelines-yaml/pull/2#issuecomment-452748467

4
votes

For above code I got this exception running a build:

my-template.yml (Line: 1, Col: 12): Unexpected value ''

but this works for me:

# my-template.yml
parameters:
- name: projects
  type: object
  default: {}
steps:
- ${{ each project in parameters.projects }}:
  - task: PublishBuildArtifacts@1
    displayName: 'Publish ${{ project }}
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)/${{ project }}.zip'

and then:

# ci.yml
steps:
- template: my-template.yml
  parameters:
    projects:
    - test1
    - test2